Next.js: Contoh Koa2

Dibuat pada 27 Feb 2017  ·  3Komentar  ·  Sumber: vercel/next.js

Adakah yang punya contoh sederhana menggunakan Koa 2?

example

Komentar yang paling membantu

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

Semua 3 komentar

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 karena Node sekarang memiliki async/menunggu, Koa 2.0.1 baru saja dirilis 3 hari yang lalu Saya kira masuk akal untuk memperbarui contoh untuk menggunakan versi ini.

Benar, saya akan membuat tiket baru.

Apakah halaman ini membantu?
0 / 5 - 0 peringkat