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因为 Node 现在有 async/await,Koa 2.0.1 才在 3 天前发布😄 我想更新示例以使用此版本是有意义的。

没错,我会创建一张新票。

此页面是否有帮助?
0 / 5 - 0 等级