From e8f39ad74c70b6a292c1982349b9cce12e6701ed Mon Sep 17 00:00:00 2001 From: Tibor Vukovic Date: Tue, 5 Dec 2017 19:42:57 +0100 Subject: [PATCH] Update hapi example to support hapi@17 (#3357) --- examples/custom-server-hapi/next-wrapper.js | 26 +++--- examples/custom-server-hapi/package.json | 6 +- examples/custom-server-hapi/server.js | 87 +++++++++------------ 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/examples/custom-server-hapi/next-wrapper.js b/examples/custom-server-hapi/next-wrapper.js index d0238520..cc14179b 100644 --- a/examples/custom-server-hapi/next-wrapper.js +++ b/examples/custom-server-hapi/next-wrapper.js @@ -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 } diff --git a/examples/custom-server-hapi/package.json b/examples/custom-server-hapi/package.json index 91d8df5d..410c225d 100644 --- a/examples/custom-server-hapi/package.json +++ b/examples/custom-server-hapi/package.json @@ -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" } } diff --git a/examples/custom-server-hapi/server.js b/examples/custom-server-hapi/server.js index 9527d55b..d79e0cac 100644 --- a/examples/custom-server-hapi/server.js +++ b/examples/custom-server-hapi/server.js @@ -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) + } })