Skip to content

Commit 9506f15

Browse files
authoredMay 7, 2021
Update custom server examples (vercel#24814)
1 parent 33e09df commit 9506f15

File tree

17 files changed

+12
-197
lines changed

17 files changed

+12
-197
lines changed
 
+2-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Custom Express Server example
22

3-
Most of the time the default Next.js server will be enough but there are times you'll want to run your own server to customize routes or other kinds of app behavior. Next.js provides [Custom server and routing](https://github.com/vercel/next.js#custom-server-and-routing) options, so you can customize as much as you want.
3+
Most of the time the default Next.js server will be enough but there are times you'll want to run your own server to integrate into an existing application. Next.js provides [a custom server api](https://nextjs.org/docs/advanced-features/custom-server).
44

5-
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 express to build a custom router on top of Next.
6-
7-
This example demonstrates 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`.
5+
Because the Next.js server is a Node.js module you can combine it with any other part of the Node.js ecosystem. In this case we are using express.
86

97
## How to use
108

@@ -15,15 +13,3 @@ npx create-next-app --example custom-server-express custom-server-express-app
1513
# or
1614
yarn create next-app --example custom-server-express custom-server-express-app
1715
```
18-
19-
### Populate body property
20-
21-
Without the use of body parsing middleware `req.body` will return undefined. To get express to populate `req.body` you need to use middleware within server.js:
22-
23-
```js
24-
app.prepare().then(() => {
25-
const server = express()
26-
server.use(express.urlencoded({ extended: true }))
27-
server.use(express.json())
28-
})
29-
```

‎examples/custom-server-express/server.js

-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ const handle = app.getRequestHandler()
99
app.prepare().then(() => {
1010
const server = express()
1111

12-
server.get('/a', (req, res) => {
13-
return app.render(req, res, '/a', req.query)
14-
})
15-
16-
server.get('/b', (req, res) => {
17-
return app.render(req, res, '/b', req.query)
18-
})
19-
2012
server.all('*', (req, res) => {
2113
return handle(req, res)
2214
})

‎examples/custom-server-hapi/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Custom server using Hapi example
22

3-
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/vercel/next.js#custom-server-and-routing) so you can customize as much as you want.
3+
Most of the time the default Next.js server will be enough but there are times you'll want to run your own server to integrate into an existing application. Next.js provides [a custom server api](https://nextjs.org/docs/advanced-features/custom-server).
44

5-
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.
6-
7-
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`.
5+
Because the Next.js server is 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).
86

97
## How to use
108

‎examples/custom-server-hapi/server.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const next = require('next')
22
const Hapi = require('@hapi/hapi')
3-
const { pathWrapper, nextHandlerWrapper } = require('./next-wrapper')
3+
const { /* pathWrapper, */ nextHandlerWrapper } = require('./next-wrapper')
44

55
const port = parseInt(process.env.PORT, 10) || 3000
66
const dev = process.env.NODE_ENV !== 'production'
@@ -10,18 +10,6 @@ const server = new Hapi.Server({
1010
})
1111

1212
app.prepare().then(async () => {
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-
2513
server.route({
2614
method: 'GET',
2715
path: '/_next/{p*}' /* next specific routes */,

‎examples/custom-server-koa/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Custom Koa Server example
22

3-
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://nextjs.org/docs/advanced-features/custom-server) so you can customize as much as you want.
3+
Most of the time the default Next.js server will be enough but there are times you'll want to run your own server to integrate into an existing application. Next.js provides [a custom server api](https://nextjs.org/docs/advanced-features/custom-server).
44

5-
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 [Koa](https://koajs.com/) to build a custom router on top of Next.
6-
7-
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`.
5+
Because the Next.js server is a Node.js module you can combine it with any other part of the Node.js ecosystem. In this case we are using [Koa](https://koajs.com/).
86

97
## How to use
108

‎examples/custom-server-koa/server.js

-10
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ app.prepare().then(() => {
1111
const server = new Koa()
1212
const router = new Router()
1313

14-
router.get('/a', async (ctx) => {
15-
await app.render(ctx.req, ctx.res, '/a', ctx.query)
16-
ctx.respond = false
17-
})
18-
19-
router.get('/b', async (ctx) => {
20-
await app.render(ctx.req, ctx.res, '/b', ctx.query)
21-
ctx.respond = false
22-
})
23-
2414
router.all('(.*)', async (ctx) => {
2515
await handle(ctx.req, ctx.res)
2616
ctx.respond = false

‎examples/custom-server-polka/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Custom [Polka](https://github.com/lukeed/polka) Server example
22

3-
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/vercel/next.js#custom-server-and-routing) so you can customize as much as you want.
3+
Most of the time the default Next.js server will be enough but there are times you'll want to run your own server to integrate into an existing application. Next.js provides [a custom server api](https://nextjs.org/docs/advanced-features/custom-server).
44

5-
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 express to build a custom router on top of Next.
6-
7-
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`.
5+
Because the Next.js server is a Node.js module you can combine it with any other part of the Node.js ecosystem. In this case we are using Polka.
86

97
## How to use
108

‎examples/custom-server-polka/server.js

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ const handle = app.getRequestHandler()
99
app.prepare().then(() => {
1010
const server = polka()
1111

12-
server.get('/a', (req, res) => app.render(req, res, '/a', req.query))
13-
14-
server.get('/b', (req, res) => app.render(req, res, '/b', req.query))
15-
1612
server.all('*', (req, res) => handle(req, res))
1713

1814
server.listen(port, (err) => {
+1-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
{
2-
"env": {
3-
"development": {
4-
"presets": ["next/babel"]
5-
},
6-
"production": {
7-
"presets": ["next/babel"]
8-
},
9-
"test": {
10-
"presets": [["next/babel", { "preset-env": { "modules": "commonjs" } }]]
11-
}
12-
}
2+
"presets": ["next/babel"]
133
}

‎examples/custom-server-typescript/server/index.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ const handle = app.getRequestHandler()
1010
app.prepare().then(() => {
1111
createServer((req, res) => {
1212
const parsedUrl = parse(req.url!, true)
13-
const { pathname, query } = parsedUrl
14-
15-
if (pathname === '/a') {
16-
app.render(req, res, '/a', query)
17-
} else if (pathname === '/b') {
18-
app.render(req, res, '/b', query)
19-
} else {
20-
handle(req, res, parsedUrl)
21-
}
13+
handle(req, res, parsedUrl)
2214
}).listen(port)
2315

2416
// tslint:disable-next-line:no-console

‎examples/custom-server/.gitignore

-34
This file was deleted.

‎examples/custom-server/README.md

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
# Custom server example
2-
3-
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/vercel/next.js#custom-server-and-routing) so you can customize as much as you want.
4-
5-
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`.
6-
7-
## How to use
8-
9-
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
10-
11-
```bash
12-
npx create-next-app --example custom-server custom-server-app
13-
# or
14-
yarn create next-app --example custom-server custom-server-app
15-
```
1+
This example has been moved to the documentation: [https://nextjs.org/docs/advanced-features/custom-server](https://nextjs.org/docs/advanced-features/custom-server)

‎examples/custom-server/package.json

-16
This file was deleted.

‎examples/custom-server/pages/a.js

-3
This file was deleted.

‎examples/custom-server/pages/b.js

-3
This file was deleted.

‎examples/custom-server/pages/index.js

-18
This file was deleted.

‎examples/custom-server/server.js

-25
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.