Skip to content

Commit de8ecbb

Browse files
vorillaztimneutkens
authored andcommitted
Added Fastify example (vercel#3034)
1 parent df5b96a commit de8ecbb

6 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/custom-server-fastify)
2+
3+
# Custom Fastify Server example
4+
5+
## How to use
6+
7+
Download the example [or clone the repo](https://github.com/zeit/next.js):
8+
9+
```bash
10+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/custom-server-fastify
11+
cd custom-server-fastify
12+
```
13+
14+
Install it and run:
15+
16+
```bash
17+
npm install
18+
npm run dev
19+
```
20+
Fastify
21+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
22+
23+
```bash
24+
now
25+
```
26+
27+
## The idea behind the example
28+
29+
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.
30+
31+
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 [Fastify](https://github.com/fastify/fastify) to build a custom router on top of Next.
32+
33+
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "custom-server-fastify",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "node server.js",
6+
"build": "next build",
7+
"start": "NODE_ENV=production node server.js"
8+
},
9+
"dependencies": {
10+
"fastify": "^0.29.2",
11+
"next": "latest",
12+
"react": "^15.4.2",
13+
"react-dom": "^15.4.2"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => <div>a</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fastify = require('fastify')
2+
const Next = require('next')
3+
4+
const port = parseInt(process.env.PORT, 10) || 3000
5+
const dev = process.env.NODE_ENV !== 'production'
6+
const app = Next({ dev })
7+
const handle = app.getRequestHandler()
8+
9+
app.prepare()
10+
.then(() => {
11+
const server = fastify()
12+
13+
server.get('/a', (req, res) => {
14+
return app.render(req.req, res.res, '/b', req.query)
15+
})
16+
17+
server.get('/b', (req, res) => {
18+
return app.render(req.req, res.res, '/b', req.query)
19+
})
20+
21+
server.get('/*', (req, res) => {
22+
return handle(req.req, res.res)
23+
})
24+
25+
server.listen(port, (err) => {
26+
if (err) throw err
27+
console.log(`> Ready on http://localhost:${port}`)
28+
})
29+
})

0 commit comments

Comments
 (0)