Skip to content

Commit ec302aa

Browse files
sfhardmanrauchg
authored andcommitted
Add custom server example using Hapi (vercel#712)
1 parent f3ee901 commit ec302aa

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

examples/custom-server-hapi/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Custom server using Hapi example
3+
4+
## How to use
5+
6+
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]:
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/custom-server-hapi
10+
cd custom-server-hapi
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
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.
29+
30+
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.
31+
32+
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`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { Readable } = require('stream')
2+
3+
function pathWrapper (app, pathName, opts) {
4+
return function (hapiRequest, hapiReply) {
5+
return new Promise((resolve, reject) => {
6+
app.render(hapiRequest.raw.req, hapiRequest.raw.res, pathName, hapiRequest.query, opts)
7+
.catch(error => {
8+
reject(error)
9+
})
10+
.then(() => {
11+
return hapiReply(new Readable().wrap(hapiRequest.raw.res))
12+
})
13+
})
14+
}
15+
}
16+
17+
function defaultHandlerWrapper (app) {
18+
return function (hapiRequest, hapiReply) {
19+
return new Promise((resolve, reject) => {
20+
app.run(hapiRequest.raw.req, hapiRequest.raw.res)
21+
.catch(error => {
22+
reject(error)
23+
})
24+
.then(() => {
25+
return hapiReply(new Readable().wrap(hapiRequest.raw.res))
26+
})
27+
})
28+
}
29+
}
30+
31+
module.exports = {
32+
pathWrapper,
33+
defaultHandlerWrapper
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"scripts": {
3+
"dev": "node server.js",
4+
"build": "next build",
5+
"start": "NODE_ENV=production node server.js"
6+
},
7+
"dependencies": {
8+
"hapi": "^16.1.0",
9+
"next": "^2.0.0-beta"
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <div>a</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <div>b</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
import Link from 'next/link'
3+
4+
export default () => (
5+
<ul>
6+
<li><Link href='/b' as='/a'><a>a</a></Link></li>
7+
<li><Link href='/a' as='/b'><a>b</a></Link></li>
8+
</ul>
9+
)

examples/custom-server-hapi/server.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const next = require('next')
2+
const Hapi = require('hapi')
3+
const { pathWrapper, defaultHandlerWrapper } = require('./next-wrapper')
4+
5+
const dev = process.env.NODE_ENV !== 'production'
6+
const app = next({ dev })
7+
const server = new Hapi.Server()
8+
9+
app.prepare()
10+
.then(() => {
11+
server.connection({ port: 3000 })
12+
13+
server.route({
14+
method: 'GET',
15+
path: '/a',
16+
handler: pathWrapper(app, '/a')
17+
})
18+
19+
server.route({
20+
method: 'GET',
21+
path: '/b',
22+
handler: pathWrapper(app, '/b')
23+
})
24+
25+
server.route({
26+
method: 'GET',
27+
path: '/{p*}', /* catch all route */
28+
handler: defaultHandlerWrapper(app)
29+
})
30+
31+
server.start().catch(error => {
32+
console.log('Error starting server')
33+
console.log(error)
34+
}).then(() => {
35+
console.log('> Ready on http://localhost:3000')
36+
})
37+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy