Next.js: Example Koa2

Created on 27 Feb 2017  路  3Comments  路  Source: vercel/next.js

Anyone have a simple example using Koa 2?

example

Most helpful comment

const n = next({ dev })
const handle = n.getRequestHandler()

n.prepare()
.then(() => {
  const app = new Koa()
  const router = new Router()

  router.get('*', async ctx => {
    await handle(ctx.req, ctx.res)
    ctx.respond = false
  })

  app.use(async (ctx, next) => {
    // Koa doesn't seems to set the default statusCode.
    // So, this middleware does that
    ctx.res.statusCode = 200
    await next()
  })

  app.use(router.routes())

  app.listen(3000)
})

All 3 comments

const n = next({ dev })
const handle = n.getRequestHandler()

n.prepare()
.then(() => {
  const app = new Koa()
  const router = new Router()

  router.get('*', async ctx => {
    await handle(ctx.req, ctx.res)
    ctx.respond = false
  })

  app.use(async (ctx, next) => {
    // Koa doesn't seems to set the default statusCode.
    // So, this middleware does that
    ctx.res.statusCode = 200
    await next()
  })

  app.use(router.routes())

  app.listen(3000)
})

@timneutkens as Node now has async/await, Koa 2.0.1 was just released 3 days ago 馃槃 I guess it would make sense to update the example to use this version.

True, I'll create a new ticket.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renatorib picture renatorib  路  3Comments

wagerfield picture wagerfield  路  3Comments

sospedra picture sospedra  路  3Comments

ghost picture ghost  路  3Comments

jesselee34 picture jesselee34  路  3Comments