From 68d48cb004ac2e0df34c143c82d96b2e12750f3f Mon Sep 17 00:00:00 2001 From: Arana Jhonny Date: Tue, 28 Feb 2017 13:49:58 -0700 Subject: [PATCH] Update Koa example for Koa 2 with async/await (#1317) --- examples/custom-server-koa/package.json | 4 ++-- examples/custom-server-koa/server.js | 26 ++++++++++++------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/examples/custom-server-koa/package.json b/examples/custom-server-koa/package.json index 652f8dc47d..a4e5926f62 100644 --- a/examples/custom-server-koa/package.json +++ b/examples/custom-server-koa/package.json @@ -6,8 +6,8 @@ "start": "NODE_ENV=production node server.js" }, "dependencies": { - "koa": "^1.2.4", - "koa-router": "^5.4.0", + "koa": "^2.0.1", + "koa-router": "^7.1.0", "next": "^2.0.0-beta", "react": "^15.4.2", "react-dom": "^15.4.2" diff --git a/examples/custom-server-koa/server.js b/examples/custom-server-koa/server.js index 8091ec371c..973ffa1ed0 100644 --- a/examples/custom-server-koa/server.js +++ b/examples/custom-server-koa/server.js @@ -11,26 +11,24 @@ app.prepare() const server = new Koa() const router = new Router() - router.get('/a', function *() { - app.render(this.req, this.res, '/b', this.query) - this.respond = false + router.get('/a', async ctx => { + await app.render(ctx.req, ctx.res, '/b', ctx.query) + ctx.respond = false }) - router.get('/b', function *() { - app.render(this.req, this.res, '/a', this.query) - this.respond = false + router.get('/b', async ctx => { + await app.render(ctx.req, ctx.res, '/a', ctx.query) + ctx.respond = false }) - router.get('*', function *() { - handle(this.req, this.res) - this.respond = false + router.get('*', async ctx => { + await handle(ctx.req, ctx.res) + ctx.respond = false }) - server.use(function *(next) { - // Koa doesn't seems to set the default statusCode. - // So, this middleware does that - this.res.statusCode = 200 - yield next + server.use(async (ctx, next) => { + ctx.res.statusCode = 200 + await next() }) server.use(router.routes())