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-06 11:05:52 +00:00
|
|
|
constructor ({ dir = '.', dev = false }) {
|
|
|
|
this.dir = resolve(dir)
|
|
|
|
this.dev = dev
|
2016-10-05 23:52:50 +00:00
|
|
|
this.router = new Router()
|
|
|
|
|
|
|
|
this.http = http.createServer((req, res) => {
|
|
|
|
this.run(req, res).catch((err) => {
|
|
|
|
this.renderError(req, res, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async start (port) {
|
|
|
|
this.router.get('/_next/:path+', async (req, res, params) => {
|
|
|
|
const path = (params.path || []).join('/')
|
|
|
|
await this.serveStatic(req, res, path)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.router.get('/:path+.json', async (req, res, params) => {
|
|
|
|
const path = (params.path || []).join('/')
|
|
|
|
await this.renderJSON(req, res, path)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.router.get('/:path*', async (req, res, params) => {
|
|
|
|
const path = (params.path || []).join('/')
|
|
|
|
await this.render(req, res, path)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async render (req, res, path) {
|
2016-10-07 01:56:48 +00:00
|
|
|
const { dir, dev } = this
|
2016-10-05 23:52:50 +00:00
|
|
|
let html
|
|
|
|
try {
|
2016-10-07 01:56:48 +00:00
|
|
|
html = await render(path, req, res, { dir, dev })
|
2016-10-05 23:52:50 +00:00
|
|
|
} catch (err) {
|
|
|
|
if ('MODULE_NOT_FOUND' === err.code) {
|
|
|
|
return this.render404(req, res)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
res.setHeader('Content-Type', 'text/html')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(html))
|
|
|
|
res.end(html)
|
|
|
|
}
|
|
|
|
|
|
|
|
async renderJSON (req, res, path) {
|
2016-10-06 11:05:52 +00:00
|
|
|
const { dir } = this
|
2016-10-05 23:52:50 +00:00
|
|
|
let json
|
|
|
|
try {
|
2016-10-06 11:05:52 +00:00
|
|
|
json = await renderJSON(path, { dir })
|
2016-10-05 23:52:50 +00:00
|
|
|
} catch (err) {
|
|
|
|
if ('MODULE_NOT_FOUND' === err.code) {
|
|
|
|
return this.render404(req, res)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async render404 (req, res) {
|
|
|
|
res.writeHead(404)
|
|
|
|
res.end('Not Found')
|
|
|
|
}
|
|
|
|
|
|
|
|
async renderError (req, res, err) {
|
|
|
|
console.error(err)
|
|
|
|
res.writeHead(500)
|
|
|
|
res.end('Error')
|
|
|
|
}
|
|
|
|
|
|
|
|
serveStatic (req, res, path) {
|
|
|
|
const p = resolve(__dirname, '../client', path)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
send(req, p)
|
|
|
|
.on('error', (err) => {
|
|
|
|
if ('ENOENT' === err.code) {
|
|
|
|
this.render404(req, res).then(resolve, reject)
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.pipe(res)
|
|
|
|
.on('finish', resolve)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|