1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/index.js

132 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-10-05 23:52:50 +00:00
import http from 'http'
import { resolve } from 'path'
import send from 'send'
import Router from './router'
import { render, renderJSON } from './render'
export default class Server {
2016-10-14 15:05:08 +00:00
constructor ({ dir = '.', dev = false, hotReloader }) {
2016-10-06 11:05:52 +00:00
this.dir = resolve(dir)
this.dev = dev
2016-10-14 15:05:08 +00:00
this.hotReloader = hotReloader
2016-10-05 23:52:50 +00:00
this.router = new Router()
this.http = http.createServer((req, res) => {
2016-10-09 09:25:38 +00:00
this.run(req, res)
.catch((err) => {
console.error(err)
res.status(500)
res.end('error')
2016-10-05 23:52:50 +00:00
})
})
}
async start (port) {
this.router.get('/_next/:path+', async (req, res, params) => {
2016-10-08 05:21:10 +00:00
const p = resolve(__dirname, '../client', (params.path || []).join('/'))
await this.serveStatic(req, res, p)
})
this.router.get('/static/:path+', async (req, res, params) => {
const p = resolve(this.dir, 'static', (params.path || []).join('/'))
await this.serveStatic(req, res, p)
2016-10-05 23:52:50 +00:00
})
2016-10-10 04:24:30 +00:00
this.router.get('/:path+.json', async (req, res) => {
await this.renderJSON(req, res)
2016-10-05 23:52:50 +00:00
})
2016-10-10 04:24:30 +00:00
this.router.get('/:path*', async (req, res) => {
await this.render(req, res)
2016-10-05 23:52:50 +00:00
})
2016-10-14 15:05:08 +00:00
if (this.hotReloader) {
await this.hotReloader.start()
}
2016-10-05 23:52:50 +00:00
await new Promise((resolve, reject) => {
this.http.listen(port, (err) => {
if (err) return reject(err)
resolve()
})
})
}
async run (req, res) {
const fn = this.router.match(req, res)
if (fn) {
await fn()
} else {
await this.render404(req, res)
}
}
2016-10-10 04:24:30 +00:00
async render (req, res) {
2016-10-15 19:49:42 +00:00
const { dir, dev } = this
2016-10-05 23:52:50 +00:00
let html
try {
2016-10-15 19:49:42 +00:00
html = await render(req.url, { req, res }, { dir, dev })
2016-10-05 23:52:50 +00:00
} catch (err) {
if (err.code === 'ENOENT') {
2016-10-10 04:24:30 +00:00
res.statusCode = 404
2016-10-09 09:25:38 +00:00
} else {
console.error(err)
2016-10-10 04:24:30 +00:00
res.statusCode = 500
2016-10-05 23:52:50 +00:00
}
2016-10-15 19:49:42 +00:00
html = await render('/_error', { req, res, err }, { dir, dev })
2016-10-05 23:52:50 +00:00
}
2016-10-09 09:25:38 +00:00
2016-10-10 04:24:30 +00:00
sendHTML(res, html)
2016-10-05 23:52:50 +00:00
}
2016-10-10 04:24:30 +00:00
async renderJSON (req, res) {
2016-10-15 19:49:42 +00:00
const { dir } = this
2016-10-05 23:52:50 +00:00
let json
try {
2016-10-15 19:49:42 +00:00
json = await renderJSON(req.url, { dir })
2016-10-05 23:52:50 +00:00
} catch (err) {
if (err.code === 'ENOENT') {
2016-10-09 09:25:38 +00:00
res.statusCode = 404
} else {
console.error(err)
res.statusCode = 500
2016-10-05 23:52:50 +00:00
}
2016-10-15 19:49:42 +00:00
json = await renderJSON('/_error.json', { dir })
2016-10-05 23:52:50 +00:00
}
2016-10-06 07:06:55 +00:00
const data = JSON.stringify(json)
2016-10-05 23:52:50 +00:00
res.setHeader('Content-Type', 'application/json')
2016-10-06 07:06:55 +00:00
res.setHeader('Content-Length', Buffer.byteLength(data))
res.end(data)
2016-10-05 23:52:50 +00:00
}
2016-10-10 04:24:30 +00:00
async render404 (req, res) {
const { dir, dev } = this
res.statusCode = 404
const html = await render('/_error', { req, res }, { dir, dev })
sendHTML(res, html)
}
2016-10-05 23:52:50 +00:00
serveStatic (req, res, path) {
return new Promise((resolve, reject) => {
2016-10-08 05:21:10 +00:00
send(req, path)
2016-10-05 23:52:50 +00:00
.on('error', (err) => {
if (err.code === 'ENOENT') {
2016-10-05 23:52:50 +00:00
this.render404(req, res).then(resolve, reject)
} else {
reject(err)
}
})
.pipe(res)
.on('finish', resolve)
})
}
}
2016-10-10 04:24:30 +00:00
function sendHTML (res, html) {
res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html)
}