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:
parent
c1cefad2fd
commit
e8f39ad74c
|
@ -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 }
|
||||||
|
|
|
@ -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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,17 @@
|
||||||
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)
|
app
|
||||||
const pluginOptions = [
|
.prepare()
|
||||||
{
|
.then(async () => {
|
||||||
register: Good,
|
|
||||||
options: {
|
|
||||||
reporters: {
|
|
||||||
console: [{
|
|
||||||
module: 'good-console'
|
|
||||||
}, 'stdout']
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
app.prepare()
|
|
||||||
.then(() => {
|
|
||||||
server.connection({ port })
|
|
||||||
server.register(pluginOptions)
|
|
||||||
.then(() => {
|
|
||||||
server.route({
|
server.route({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
path: '/a',
|
path: '/a',
|
||||||
|
@ -39,17 +24,23 @@ app.prepare()
|
||||||
handler: pathWrapper(app, '/b')
|
handler: pathWrapper(app, '/b')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/_next/{p*}', /* next specific routes */
|
||||||
|
handler: nextHandlerWrapper(app)
|
||||||
|
})
|
||||||
|
|
||||||
server.route({
|
server.route({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
path: '/{p*}', /* catch all route */
|
path: '/{p*}', /* catch all route */
|
||||||
handler: defaultHandlerWrapper(app)
|
handler: defaultHandlerWrapper(app)
|
||||||
})
|
})
|
||||||
|
|
||||||
server.start().catch(error => {
|
try {
|
||||||
|
await server.start()
|
||||||
|
console.log(`> Ready on http://localhost:${port}`)
|
||||||
|
} catch (error) {
|
||||||
console.log('Error starting server')
|
console.log('Error starting server')
|
||||||
console.log(error)
|
console.log(error)
|
||||||
}).then(() => {
|
}
|
||||||
console.log(`> Ready on http://localhost:${port}`)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue