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

Return a promise from getRequestHandler execution, await _serveStatic in serveStaticWithGzip, update custom-server-hapi to complete request lifecycle (#1099)

This commit is contained in:
sfhardman 2017-02-13 15:12:32 +13:00 committed by Guillermo Rauch
parent b220193167
commit dbc2ceefde
4 changed files with 51 additions and 27 deletions

View file

@ -2,7 +2,12 @@ const pathWrapper = (app, pathName, opts) => ({ raw, query }, hapiReply) =>
app.renderToHTML(raw.req, raw.res, pathName, query, opts) app.renderToHTML(raw.req, raw.res, pathName, query, opts)
.then(hapiReply) .then(hapiReply)
const defaultHandlerWrapper = app => ({ raw, url }, hapiReply) => const defaultHandlerWrapper = app => {
app.run(raw.req, raw.res, url) const handler = app.getRequestHandler()
return ({ raw, url }, hapiReply) =>
handler(raw.req, raw.res, url)
.then(() => {
hapiReply.close(false)
})
}
module.exports = { pathWrapper, defaultHandlerWrapper } module.exports = { pathWrapper, defaultHandlerWrapper }

View file

@ -8,6 +8,8 @@
"hapi": "^16.1.0", "hapi": "^16.1.0",
"next": "^2.0.0-beta", "next": "^2.0.0-beta",
"react": "^15.4.2", "react": "^15.4.2",
"react-dom": "^15.4.2" "react-dom": "^15.4.2",
"good": "^7.1.0",
"good-console": "^6.2.0"
} }
} }

View file

@ -1,37 +1,54 @@
const next = require('next') const next = require('next')
const Hapi = require('hapi') const Hapi = require('hapi')
const Good = require('good')
const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper') const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper')
const dev = process.env.NODE_ENV !== 'production' const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev }) const app = next({ dev })
const server = new Hapi.Server() const server = new Hapi.Server()
// add request logging (optional)
const pluginOptions = [
{
register: Good,
options: {
reporters: {
console: [{
module: 'good-console'
}, 'stdout']
}
}
}
]
app.prepare() app.prepare()
.then(() => { .then(() => {
server.connection({ port: 3000 }) server.connection({ port: 3000 })
server.register(pluginOptions)
.then(() => {
server.route({
method: 'GET',
path: '/a',
handler: pathWrapper(app, '/a')
})
server.route({ server.route({
method: 'GET', method: 'GET',
path: '/a', path: '/b',
handler: pathWrapper(app, '/a') handler: pathWrapper(app, '/b')
}) })
server.route({ server.route({
method: 'GET', method: 'GET',
path: '/b', path: '/{p*}', /* catch all route */
handler: pathWrapper(app, '/b') handler: defaultHandlerWrapper(app)
}) })
server.route({ server.start().catch(error => {
method: 'GET', console.log('Error starting server')
path: '/{p*}', /* catch all route */ console.log(error)
handler: defaultHandlerWrapper(app) }).then(() => {
}) console.log('> Ready on http://localhost:3000')
})
server.start().catch(error => {
console.log('Error starting server')
console.log(error)
}).then(() => {
console.log('> Ready on http://localhost:3000')
}) })
}) })

View file

@ -42,7 +42,7 @@ export default class Server {
throw new Error('Please provide a parsed url to `handle` as third parameter. See https://github.com/zeit/next.js#custom-server-and-routing for an example.') throw new Error('Please provide a parsed url to `handle` as third parameter. See https://github.com/zeit/next.js#custom-server-and-routing for an example.')
} }
this.run(req, res, parsedUrl) return this.run(req, res, parsedUrl)
.catch((err) => { .catch((err) => {
if (!this.quiet) console.error(err) if (!this.quiet) console.error(err)
res.statusCode = 500 res.statusCode = 500
@ -252,7 +252,7 @@ export default class Server {
} }
async serveStaticWithGzip (req, res, path) { async serveStaticWithGzip (req, res, path) {
this._serveStatic(req, res, () => { await this._serveStatic(req, res, () => {
return serveStaticWithGzip(req, res, path) return serveStaticWithGzip(req, res, path)
}) })
} }