Skip to content

Commit 68d48cb

Browse files
aranajhonnytimneutkens
authored andcommitted
Update Koa example for Koa 2 with async/await (#1317)
1 parent 46b5d6a commit 68d48cb

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

examples/custom-server-koa/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"start": "NODE_ENV=production node server.js"
77
},
88
"dependencies": {
9-
"koa": "^1.2.4",
10-
"koa-router": "^5.4.0",
9+
"koa": "^2.0.1",
10+
"koa-router": "^7.1.0",
1111
"next": "^2.0.0-beta",
1212
"react": "^15.4.2",
1313
"react-dom": "^15.4.2"

examples/custom-server-koa/server.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,24 @@ app.prepare()
1111
const server = new Koa()
1212
const router = new Router()
1313

14-
router.get('/a', function *() {
15-
app.render(this.req, this.res, '/b', this.query)
16-
this.respond = false
14+
router.get('/a', async ctx => {
15+
await app.render(ctx.req, ctx.res, '/b', ctx.query)
16+
ctx.respond = false
1717
})
1818

19-
router.get('/b', function *() {
20-
app.render(this.req, this.res, '/a', this.query)
21-
this.respond = false
19+
router.get('/b', async ctx => {
20+
await app.render(ctx.req, ctx.res, '/a', ctx.query)
21+
ctx.respond = false
2222
})
2323

24-
router.get('*', function *() {
25-
handle(this.req, this.res)
26-
this.respond = false
24+
router.get('*', async ctx => {
25+
await handle(ctx.req, ctx.res)
26+
ctx.respond = false
2727
})
2828

29-
server.use(function *(next) {
30-
// Koa doesn't seems to set the default statusCode.
31-
// So, this middleware does that
32-
this.res.statusCode = 200
33-
yield next
29+
server.use(async (ctx, next) => {
30+
ctx.res.statusCode = 200
31+
await next()
3432
})
3533

3634
server.use(router.routes())

0 commit comments

Comments
 (0)