From 4341bc4f65c3240193b527a6173546b53d2563ca Mon Sep 17 00:00:00 2001 From: sfhardma Date: Tue, 10 Jan 2017 11:38:01 +1300 Subject: [PATCH] Add custom server example using Hapi --- examples/custom-server-hapi/README.md | 32 ++++++++++++++++++ examples/custom-server-hapi/nextWrapper.js | 34 +++++++++++++++++++ examples/custom-server-hapi/package.json | 11 ++++++ examples/custom-server-hapi/pages/a.js | 3 ++ examples/custom-server-hapi/pages/b.js | 3 ++ examples/custom-server-hapi/pages/index.js | 9 +++++ examples/custom-server-hapi/server.js | 39 ++++++++++++++++++++++ 7 files changed, 131 insertions(+) create mode 100644 examples/custom-server-hapi/README.md create mode 100644 examples/custom-server-hapi/nextWrapper.js create mode 100644 examples/custom-server-hapi/package.json create mode 100644 examples/custom-server-hapi/pages/a.js create mode 100644 examples/custom-server-hapi/pages/b.js create mode 100644 examples/custom-server-hapi/pages/index.js create mode 100644 examples/custom-server-hapi/server.js diff --git a/examples/custom-server-hapi/README.md b/examples/custom-server-hapi/README.md new file mode 100644 index 00000000000000..a6e4d7785f28f4 --- /dev/null +++ b/examples/custom-server-hapi/README.md @@ -0,0 +1,32 @@ + +# Custom server using Hapi example + +## How to use + +Download the example (or clone the repo)[https://github.com/zeit/next.js.git]: + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/custom-server-hapi +cd custom-server-hapi +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +Most of the times the default Next server will be enough but sometimes you want to run your own server to customize routes or other kind of the app behavior. Next provides a [Custom server and routing](https://github.com/zeit/next.js#custom-server-and-routing) so you can customize as much as you want. + +Because the Next.js server is just a node.js module you can combine it with any other part of the node.js ecosystem. in this case we are using [Hapi](https://hapijs.com) to build a custom router on top of Next. + +The example shows a server that serves the component living in `pages/a.js` when the route `/b` is requested and `pages/b.js` when the route `/a` is accessed. This is obviously a non-standard routing strategy. You can see how this custom routing is being made inside `server.js`. \ No newline at end of file diff --git a/examples/custom-server-hapi/nextWrapper.js b/examples/custom-server-hapi/nextWrapper.js new file mode 100644 index 00000000000000..5e3aeae6f3f4ce --- /dev/null +++ b/examples/custom-server-hapi/nextWrapper.js @@ -0,0 +1,34 @@ +const { Writable, Readable } = require('stream'); + +function pathWrapper(app, pathName, opts) { + return function(hapiRequest, hapiReply) { + return new Promise((resolve, reject) => { + app.render(hapiRequest.raw.req, hapiRequest.raw.res, pathName, hapiRequest.query, opts) + .catch(error => { + reject(error); + }) + .then(() => { + return(hapiReply(new Readable().wrap(hapiRequest.raw.res))); + }); + }); + } +} + +function defaultHandlerWrapper(app) { + return function(hapiRequest, hapiReply) { + return new Promise((resolve, reject) => { + app.run(hapiRequest.raw.req, hapiRequest.raw.res) + .catch(error => { + reject(error); + }) + .then(() => { + return(hapiReply(new Readable().wrap(hapiRequest.raw.res))); + }); + }); + } +} + +module.exports = { + pathWrapper, + defaultHandlerWrapper +}; diff --git a/examples/custom-server-hapi/package.json b/examples/custom-server-hapi/package.json new file mode 100644 index 00000000000000..19ffdd23327709 --- /dev/null +++ b/examples/custom-server-hapi/package.json @@ -0,0 +1,11 @@ +{ + "scripts": { + "dev": "node server.js", + "build": "next build", + "start": "NODE_ENV=production node server.js" + }, + "dependencies": { + "hapi": "^16.1.0", + "next": "^2.0.0-beta" + } +} diff --git a/examples/custom-server-hapi/pages/a.js b/examples/custom-server-hapi/pages/a.js new file mode 100644 index 00000000000000..c5359797af81c2 --- /dev/null +++ b/examples/custom-server-hapi/pages/a.js @@ -0,0 +1,3 @@ +import React from 'react' + +export default () =>
a
diff --git a/examples/custom-server-hapi/pages/b.js b/examples/custom-server-hapi/pages/b.js new file mode 100644 index 00000000000000..9bde4d9dc56bcd --- /dev/null +++ b/examples/custom-server-hapi/pages/b.js @@ -0,0 +1,3 @@ +import React from 'react' + +export default () =>
b
diff --git a/examples/custom-server-hapi/pages/index.js b/examples/custom-server-hapi/pages/index.js new file mode 100644 index 00000000000000..d044fc1e2c0b6c --- /dev/null +++ b/examples/custom-server-hapi/pages/index.js @@ -0,0 +1,9 @@ +import React from 'react' +import Link from 'next/link' + +export default () => ( + +) diff --git a/examples/custom-server-hapi/server.js b/examples/custom-server-hapi/server.js new file mode 100644 index 00000000000000..002d3aaac423b9 --- /dev/null +++ b/examples/custom-server-hapi/server.js @@ -0,0 +1,39 @@ +const { parse } = require('url') +const next = require('next') +const Hapi = require('hapi'); +const { pathWrapper, defaultHandlerWrapper } = require('./nextWrapper'); + +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const server = new Hapi.Server(); + +app.prepare() +.then(() => { + server.connection({ port: 3000 }) + + 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:3000') + }); + +})