Next.js: 例Koa2

作成日 2017年02月27日  ·  3コメント  ·  ソース: vercel/next.js

Koa 2を使用した簡単な例はありますか?

example

最も参考になるコメント

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

全てのコメント3件

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ノードにasync/awaitが追加されたため、Koa2.0.1は3日前にリリースされました😄このバージョンを使用するように例を更新するのは理にかなっていると思います。

確かに、新しいチケットを作成します。

このページは役に立ちましたか?
0 / 5 - 0 評価