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

Update hapi example to support hapi@17 (#3357)

This commit is contained in:
Tibor Vukovic 2017-12-05 19:42:57 +01:00 committed by Tim Neutkens
parent c1cefad2fd
commit e8f39ad74c
3 changed files with 57 additions and 62 deletions

View file

@ -1,13 +1,19 @@
const pathWrapper = (app, pathName, opts) => ({ raw, query }, hapiReply) => const { parse } = require('url')
app.renderToHTML(raw.req, raw.res, pathName, query, opts)
.then(hapiReply)
const defaultHandlerWrapper = app => { const nextHandlerWrapper = app => {
const handler = app.getRequestHandler() const handler = app.getRequestHandler()
return ({ raw, url }, hapiReply) => return async ({ raw, url }, h) => {
handler(raw.req, raw.res, url) await handler(raw.req, raw.res, url)
.then(() => { return h.close
hapiReply.close(false) }
})
} }
module.exports = { pathWrapper, defaultHandlerWrapper } const defaultHandlerWrapper = app => async ({ raw: { req, res }, url }) => {
const { pathname, query } = parse(url, true)
return app.renderToHTML(req, res, pathname, query)
}
const pathWrapper = (app, pathName, opts) => async ({ raw, query, params }) => {
return app.renderToHTML(raw.req, raw.res, pathName, { ...query, ...params }, opts)
}
module.exports = { pathWrapper, defaultHandlerWrapper, nextHandlerWrapper }

View file

@ -7,11 +7,9 @@
"start": "NODE_ENV=production node server.js" "start": "NODE_ENV=production node server.js"
}, },
"dependencies": { "dependencies": {
"hapi": "^16.1.0", "hapi": "^17.1.1",
"next": "latest", "next": "latest",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0"
"good": "^7.1.0",
"good-console": "^6.2.0"
} }
} }

View file

@ -1,55 +1,46 @@
const next = require('next') const next = require('next')
const Hapi = require('hapi') const Hapi = require('hapi')
const Good = require('good') const { pathWrapper, defaultHandlerWrapper, nextHandlerWrapper } = require('./next-wrapper')
const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper')
const port = parseInt(process.env.PORT, 10) || 3000 const port = parseInt(process.env.PORT, 10) || 3000
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({
port
// add request logging (optional) })
const pluginOptions = [
{ app
register: Good, .prepare()
options: { .then(async () => {
reporters: { server.route({
console: [{ method: 'GET',
module: 'good-console' path: '/a',
}, 'stdout'] handler: pathWrapper(app, '/a')
} })
}
} server.route({
] method: 'GET',
path: '/b',
app.prepare() handler: pathWrapper(app, '/b')
.then(() => { })
server.connection({ port })
server.register(pluginOptions) server.route({
.then(() => { method: 'GET',
server.route({ path: '/_next/{p*}', /* next specific routes */
method: 'GET', handler: nextHandlerWrapper(app)
path: '/a', })
handler: pathWrapper(app, '/a')
}) server.route({
method: 'GET',
server.route({ path: '/{p*}', /* catch all route */
method: 'GET', handler: defaultHandlerWrapper(app)
path: '/b', })
handler: pathWrapper(app, '/b')
}) try {
await server.start()
server.route({ console.log(`> Ready on http://localhost:${port}`)
method: 'GET', } catch (error) {
path: '/{p*}', /* catch all route */ console.log('Error starting server')
handler: defaultHandlerWrapper(app) console.log(error)
}) }
server.start().catch(error => {
console.log('Error starting server')
console.log(error)
}).then(() => {
console.log(`> Ready on http://localhost:${port}`)
})
})
}) })