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) =>
app.renderToHTML(raw.req, raw.res, pathName, query, opts)
.then(hapiReply)
const { parse } = require('url')
const defaultHandlerWrapper = app => {
const nextHandlerWrapper = app => {
const handler = app.getRequestHandler()
return ({ raw, url }, hapiReply) =>
handler(raw.req, raw.res, url)
.then(() => {
hapiReply.close(false)
})
return async ({ raw, url }, h) => {
await handler(raw.req, raw.res, url)
return h.close
}
}
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"
},
"dependencies": {
"hapi": "^16.1.0",
"hapi": "^17.1.1",
"next": "latest",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"good": "^7.1.0",
"good-console": "^6.2.0"
"react-dom": "^16.0.0"
}
}

View file

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