nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

Can I use nuxt.js with Koa

Open ihaichao opened this issue 9 years ago • 7 comments

When I use app.use(nuxt.render), it throws an error says app.use() requires a generator function Are there any other ways to use nuxt.js with Koa?

This question is available on Nuxt.js community (#c210)

ihaichao avatar Feb 13 '17 08:02 ihaichao

a middleware for koa would need to be created. most of the time, you can wrap the express middleware to match what koa needs. checkout https://github.com/koajs/koa/blob/master/docs/guide.md

uptownhr avatar Feb 14 '17 01:02 uptownhr

Hi, It works fine with this code: app.js

var app = require('koa')()
var Nuxt = require('nuxt')

var config = require('./nuxt.config.js')
config.dev = !(app.env === 'production')

var nuxt = new Nuxt(config)

// Build only in dev mode
if (config.dev) {
  nuxt.build()
  .catch((error) => {
    console.error(error) // eslint-disable-line no-console
    process.exit(1)
  })
}

app.use(function * () {
  this.status = 200 // koa defaults to 404 when it sees that status is unset
  yield nuxt.render(this.req, this.res)
})

app.listen(3000)

alexchopin avatar Feb 14 '17 13:02 alexchopin

@alexchopin @Atinux koa is not recommended to use req.end()

When using nuxt.render and koa-session, it often throws errors

Can't set headers after they are sent.

Almost all koa session modules can not be used

chen-zeyu avatar Apr 30 '17 16:04 chen-zeyu

@chen-zeyu, thank you so much for contributing this fact! Have been knocking my head against this error all day, since the root call is obscured by all of the ES7 shim junk. Have you managed to find a workaround for this? I agree, calling res.end() is incredibly presumptuous for a middleware-based framework.

dkushner avatar Jul 14 '17 03:07 dkushner

See #1206 for a workaround for Nuxt 1.0 API changes.

IlyaSemenov avatar Jul 29 '17 14:07 IlyaSemenov

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Nov 05 '18 04:11 lock[bot]