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

197 lines
5 KiB
JavaScript
Raw Normal View History

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-10-17 07:05:46 +00:00
this.hotReloader = hotReload ? new HotReloader(this.dir) : 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)
res.status(500)
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-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) {
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
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
2016-10-19 12:41:45 +00:00
const opts = { dir, dev }
2016-10-05 23:52:50 +00:00
let html
2016-10-19 12:41:45 +00:00
const err = this.getCompilationError(req.url)
if (err) {
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err }, opts)
} else {
try {
html = await render(req.url, ctx, opts)
} catch (err) {
const _err = this.getCompilationError('/_error')
if (_err) {
2016-10-19 12:41:45 +00:00
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err: _err }, opts)
} else {
if (err.code === 'ENOENT') {
res.statusCode = 404
} else {
console.error(err)
res.statusCode = 500
}
html = await render('/_error', { ...ctx, err }, opts)
2016-10-19 12:41:45 +00:00
}
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-19 12:41:45 +00:00
const opts = { dir }
2016-10-05 23:52:50 +00:00
let json
2016-10-19 12:41:45 +00:00
const err = this.getCompilationError(req.url)
if (err) {
res.statusCode = 500
json = await renderJSON('/_error-debug.json', opts)
json = { ...json, err: errorToJSON(err) }
} else {
try {
json = await renderJSON(req.url, opts)
} catch (err) {
const _err = this.getCompilationError('/_error.json')
if (_err) {
2016-10-19 12:41:45 +00:00
res.statusCode = 500
json = await renderJSON('/_error-debug.json', opts)
json = { ...json, err: errorToJSON(_err) }
} else {
if (err.code === 'ENOENT') {
res.statusCode = 404
} else {
console.error(err)
res.statusCode = 500
}
json = await renderJSON('/_error.json', opts)
2016-10-19 12:41:45 +00:00
}
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
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
const opts = { dir, dev }
let html
const err = this.getCompilationError('/_error')
if (err) {
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err }, opts)
} else {
res.statusCode = 404
html = await render('/_error', ctx, opts)
}
2016-10-10 04:24:30 +00:00
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-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)
}