rsnext/examples/custom-server-hapi
2020-08-13 04:43:46 +00:00
..
pages Fixes Hapi custom server example (#15292) 2020-07-21 13:27:39 -04:00
public Fixes Hapi custom server example (#15292) 2020-07-21 13:27:39 -04:00
.gitignore Added .gitignore to examples that are deployed to vercel (#15127) 2020-07-16 10:52:23 -04:00
next-wrapper.js removed renderToHTML from ssr-caching and custom-server-hapi in examples (#16138) 2020-08-13 04:43:46 +00:00
package.json Move cross-env from devDependencies to dependencies (#9580) 2019-12-01 10:36:27 +01:00
README.md Suggest npx over npm init (#13637) 2020-06-01 17:36:57 +00:00
server.js Fixes Hapi custom server example (#15292) 2020-07-21 13:27:39 -04:00

Custom server using Hapi 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 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 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.

How to use

Using create-next-app

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example custom-server-hapi custom-server-hapi-app
# or
yarn create next-app --example custom-server-hapi custom-server-hapi-app

Download manually

Download the example:

curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/custom-server-hapi
cd custom-server-hapi

Install it and run:

npm install
npm run dev
# or
yarn
yarn dev