Skip to content

Commit 2c2d961

Browse files
impronunciabletimneutkens
authored andcommitted
Added Koa example (vercel#800)
* Added Koa example * Linted koa example
1 parent 0627378 commit 2c2d961

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ export default ({ url }) => (
356356
<ul>
357357
<li><a href="./examples/custom-server">Basic custom server</a></li>
358358
<li><a href="./examples/custom-server-express">Express integration</a></li>
359+
<li><a href="./examples/custom-server-koa">Koa integration</a></li>
359360
<li><a href="./examples/parameterized-routing">Parameterized routing</a></li>
360361
<li><a href="./examples/ssr-caching">SSR caching</a></li>
361362
</ul>

examples/custom-server-koa/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Custom Koa Server example
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js):
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-koa
10+
cd custom-server-koa
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 [Koa](http://koajs.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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "custom-server-koa",
3+
"scripts": {
4+
"dev": "node server.js",
5+
"build": "next build",
6+
"start": "NODE_ENV=production node server.js"
7+
},
8+
"dependencies": {
9+
"koa": "^1.2.4",
10+
"koa-router": "^5.4.0",
11+
"next": "^2.0.0-beta"
12+
}
13+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default () => <div>a</div>

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

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+
)

examples/custom-server-koa/server.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const Koa = require('koa')
2+
const next = require('next')
3+
const Router = require('koa-router')
4+
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 = new Koa()
12+
const router = new Router()
13+
14+
router.get('/a', function *() {
15+
app.render(this.req, this.res, '/b', this.query)
16+
this.respond = false
17+
})
18+
19+
router.get('/b', function *() {
20+
app.render(this.req, this.res, '/a', this.query)
21+
this.respond = false
22+
})
23+
24+
router.get('*', function *() {
25+
handle(this.req, this.res)
26+
this.respond = false
27+
})
28+
29+
server.use(router.routes())
30+
server.listen(3000, (err) => {
31+
if (err) throw err
32+
console.log('> Ready on http://localhost:3000')
33+
})
34+
})

0 commit comments

Comments
 (0)