nuxt.render(req, res)

You can use Nuxt as a middleware with nuxt.render for your Node.js server.


Example with Express:

  1. const { loadNuxt, build } = require('nuxt')
  2. const app = require('express')()
  3. const isDev = process.env.NODE_ENV !== 'production'
  4. const port = process.env.PORT || 3000
  5. async function start() {
  6. // We get Nuxt instance
  7. const nuxt = await loadNuxt(isDev ? 'dev' : 'start')
  8. // Render every route with Nuxt
  9. app.use(nuxt.render)
  10. // Build only in dev mode with hot-reloading
  11. if (isDev) {
  12. build(nuxt)
  13. }
  14. // Listen the server
  15. app.listen(port, '0.0.0.0')
  16. console.log('Server listening on `localhost:' + port + '`.')
  17. }
  18. start()

nuxt.render(req, res) - 图1

It’s recommended to call nuxt.render at the end of your middlewares since it will handle the rendering of your web application and won’t call next()