rsnext/examples/custom-server-hapi
Akuma Isaac Akuma 7631c1cac5
Added query object as property to url object (#18340)
`url` object is an instance of `url.URL` which has no property called `query` and it's causing an error with Next image-optimizer as you can see with the error log below.

```log
TypeError: Cannot destructure property 'url' of 'parsedUrl.query' as it is undefined.
    at imageOptimizer (/[redacted]/node_modules/next/next-server/server/image-optimizer.ts:51:11)
    at Object.fn (/[redacted]/node_modules/next/next-server/server/next-server.ts:617:11)
    at Router.execute (/[redacted]/node_modules/next/next-server/server/router.ts:200:40)
    at DevServer.run (/[redacted]/node_modules/next/next-server/server/next-server.ts:1017:41)
    at DevServer.run (/[redacted]/node_modules/next/server/next-dev-server.ts:406:18)
    at DevServer.handleRequest (/[redacted]/node_modules/next/next-server/server/next-server.ts:437:14)
    at /[redacted]/server/next-wrapper.ts:9:5
```
2020-11-12 21:23:58 +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 Added query object as property to url object (#18340) 2020-11-12 21:23:58 +00:00
package.json Ensure all examples are MIT licensed (#16691) 2020-08-29 22:32:35 -04:00
README.md Simplify example usage instructions (#16678) 2020-08-29 22:22:02 -04: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

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