2016-10-05 23:52:50 +00:00
|
|
|
import http from 'http'
|
2016-10-19 12:41:45 +00:00
|
|
|
import { resolve, join } from 'path'
|
|
|
|
import { parse } from 'url'
|
2016-10-05 23:52:50 +00:00
|
|
|
import send from 'send'
|
|
|
|
import Router from './router'
|
2016-10-19 12:41:45 +00:00
|
|
|
import { render, renderJSON, errorToJSON } from './render'
|
2016-10-17 07:05:46 +00:00
|
|
|
import HotReloader from './hot-reloader'
|
2016-10-19 12:41:45 +00:00
|
|
|
import { resolveFromList } from './resolve'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
|
|
|
export default class Server {
|
2016-10-17 07:05:46 +00:00
|
|
|
constructor ({ dir = '.', dev = false, hotReload = false }) {
|
2016-10-06 11:05:52 +00:00
|
|
|
this.dir = resolve(dir)
|
|
|
|
this.dev = dev
|
2016-11-18 07:26:17 +00:00
|
|
|
this.hotReloader = hotReload ? new HotReloader(this.dir, this.dev) : null
|
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)
|
2016-11-24 14:03:16 +00:00
|
|
|
res.statusCode = 500
|
2016-10-17 00:00:17 +00:00
|
|
|
res.end('error')
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-17 07:07:41 +00:00
|
|
|
|
|
|
|
this.defineRoutes()
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start (port) {
|
2016-10-17 07:07:41 +00:00
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.http.listen(port, (err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defineRoutes () {
|
2016-11-28 00:15:56 +00:00
|
|
|
this.router.get('/_next/commons.js', async (req, res, params) => {
|
|
|
|
const p = join(this.dir, '.next/commons.js')
|
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
})
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
this.router.get('/_next/:path+', async (req, res, params) => {
|
2016-10-19 12:58:08 +00:00
|
|
|
const p = join(__dirname, '..', 'client', ...(params.path || []))
|
2016-10-08 05:21:10 +00:00
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.router.get('/static/:path+', async (req, res, params) => {
|
2016-10-19 12:58:08 +00:00
|
|
|
const p = join(this.dir, 'static', ...(params.path || []))
|
2016-10-08 05:21:10 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async run (req, res) {
|
2016-11-23 18:32:49 +00:00
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.run(req, res)
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
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-11-04 08:05:03 +00:00
|
|
|
const { pathname, query } = parse(req.url, true)
|
|
|
|
const ctx = { req, res, pathname, query }
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
const compilationErr = this.getCompilationError(req.url)
|
|
|
|
if (compilationErr) {
|
|
|
|
await this.doRender(res, 500, '/_error-debug', { ...ctx, err: compilationErr })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.doRender(res, 200, req.url, ctx)
|
|
|
|
} catch (err) {
|
|
|
|
const compilationErr2 = this.getCompilationError('/_error')
|
|
|
|
if (compilationErr2) {
|
|
|
|
await this.doRender(res, 500, '/_error-debug', { ...ctx, err: compilationErr2 })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.code !== 'ENOENT') {
|
|
|
|
console.error(err)
|
|
|
|
const url = this.dev ? '/_error-debug' : '/_error'
|
|
|
|
await this.doRender(res, 500, url, { ...ctx, err })
|
|
|
|
return
|
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
|
|
|
try {
|
2016-11-24 14:03:16 +00:00
|
|
|
await this.doRender(res, 404, '/_error', { ...ctx, err })
|
|
|
|
} catch (err2) {
|
|
|
|
if (this.dev) {
|
|
|
|
await this.doRender(res, 500, '/_error-debug', { ...ctx, err: err2 })
|
2016-11-03 15:12:37 +00:00
|
|
|
} else {
|
2016-11-24 14:03:16 +00:00
|
|
|
throw err2
|
2016-10-19 12:41:45 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async doRender (res, statusCode, url, ctx) {
|
|
|
|
const { dir, dev } = this
|
|
|
|
|
|
|
|
// need to set statusCode before `render`
|
|
|
|
// since it can be used on getInitialProps
|
|
|
|
res.statusCode = statusCode
|
2016-10-09 09:25:38 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
const html = await render(url, ctx, { dir, dev })
|
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-11-24 14:03:16 +00:00
|
|
|
const compilationErr = this.getCompilationError(req.url)
|
|
|
|
if (compilationErr) {
|
|
|
|
await this.doRenderJSON(res, 500, '/_error-debug.json', compilationErr)
|
|
|
|
return
|
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
try {
|
|
|
|
await this.doRenderJSON(res, 200, req.url)
|
|
|
|
} catch (err) {
|
|
|
|
const compilationErr2 = this.getCompilationError('/_error.json')
|
|
|
|
if (compilationErr2) {
|
|
|
|
await this.doRenderJSON(res, 500, '/_error-debug.json', compilationErr2)
|
|
|
|
return
|
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
await this.doRenderJSON(res, 404, '/_error.json')
|
|
|
|
} else {
|
|
|
|
console.error(err)
|
|
|
|
await this.doRenderJSON(res, 500, '/_error.json')
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async doRenderJSON (res, statusCode, url, err) {
|
|
|
|
const { dir } = this
|
|
|
|
const json = await renderJSON(url, { dir })
|
|
|
|
if (err) {
|
|
|
|
json.err = errorToJSON(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))
|
2016-11-24 14:03:16 +00:00
|
|
|
res.statusCode = statusCode
|
2016-10-06 07:06:55 +00:00
|
|
|
res.end(data)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 04:24:30 +00:00
|
|
|
async render404 (req, res) {
|
2016-11-04 08:05:03 +00:00
|
|
|
const { pathname, query } = parse(req.url, true)
|
|
|
|
const ctx = { req, res, pathname, query }
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
const compilationErr = this.getCompilationError('/_error')
|
|
|
|
if (compilationErr) {
|
|
|
|
await this.doRender(res, 500, '/_error-debug', { ...ctx, err: compilationErr })
|
|
|
|
return
|
2016-11-03 15:12:37 +00:00
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
try {
|
|
|
|
await this.doRender(res, 404, '/_error', ctx)
|
|
|
|
} catch (err) {
|
|
|
|
if (this.dev) {
|
|
|
|
await this.doRender(res, 500, '/_error-debug', { ...ctx, err })
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
}
|
|
|
|
|
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) => {
|
2016-10-17 00:00:17 +00:00
|
|
|
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-19 12:41:45 +00:00
|
|
|
|
|
|
|
getCompilationError (url) {
|
|
|
|
if (!this.hotReloader) return
|
|
|
|
|
|
|
|
const errors = this.hotReloader.getCompilationErrors()
|
|
|
|
if (!errors.size) return
|
|
|
|
|
|
|
|
const p = parse(url || '/').pathname.replace(/\.json$/, '')
|
|
|
|
const id = join(this.dir, '.next', 'bundles', 'pages', p)
|
|
|
|
const path = resolveFromList(id, errors.keys())
|
|
|
|
if (path) return errors.get(path)[0]
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|