mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Update Koa example for Koa 2 with async/await (#1317)
This commit is contained in:
parent
46b5d6a445
commit
68d48cb004
|
@ -6,8 +6,8 @@
|
||||||
"start": "NODE_ENV=production node server.js"
|
"start": "NODE_ENV=production node server.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"koa": "^1.2.4",
|
"koa": "^2.0.1",
|
||||||
"koa-router": "^5.4.0",
|
"koa-router": "^7.1.0",
|
||||||
"next": "^2.0.0-beta",
|
"next": "^2.0.0-beta",
|
||||||
"react": "^15.4.2",
|
"react": "^15.4.2",
|
||||||
"react-dom": "^15.4.2"
|
"react-dom": "^15.4.2"
|
||||||
|
|
|
@ -11,26 +11,24 @@ app.prepare()
|
||||||
const server = new Koa()
|
const server = new Koa()
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
router.get('/a', function *() {
|
router.get('/a', async ctx => {
|
||||||
app.render(this.req, this.res, '/b', this.query)
|
await app.render(ctx.req, ctx.res, '/b', ctx.query)
|
||||||
this.respond = false
|
ctx.respond = false
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/b', function *() {
|
router.get('/b', async ctx => {
|
||||||
app.render(this.req, this.res, '/a', this.query)
|
await app.render(ctx.req, ctx.res, '/a', ctx.query)
|
||||||
this.respond = false
|
ctx.respond = false
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('*', function *() {
|
router.get('*', async ctx => {
|
||||||
handle(this.req, this.res)
|
await handle(ctx.req, ctx.res)
|
||||||
this.respond = false
|
ctx.respond = false
|
||||||
})
|
})
|
||||||
|
|
||||||
server.use(function *(next) {
|
server.use(async (ctx, next) => {
|
||||||
// Koa doesn't seems to set the default statusCode.
|
ctx.res.statusCode = 200
|
||||||
// So, this middleware does that
|
await next()
|
||||||
this.res.statusCode = 200
|
|
||||||
yield next
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.use(router.routes())
|
server.use(router.routes())
|
||||||
|
|
Loading…
Reference in a new issue