API: nuxt.render(req, res)

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

Example with Express:

  1. const { Nuxt, Builder } = require('nuxt')
  2. const app = require('express')()
  3. const isProd = (process.env.NODE_ENV === 'production')
  4. const port = process.env.PORT || 3000
  5. // We instantiate Nuxt.js with the options
  6. const config = require('./nuxt.config.js')
  7. config.dev = !isProd
  8. const nuxt = new Nuxt(config)
  9. // Render every route with Nuxt.js
  10. app.use(nuxt.render)
  11. // Build only in dev mode with hot-reloading
  12. if (config.dev) {
  13. new Builder(nuxt).build()
  14. .then(listen)
  15. } else {
  16. listen()
  17. }
  18. function listen () {
  19. // Listen the server
  20. app.listen(port, '0.0.0.0')
  21. console.log('Server listening on `localhost:' + port + '`.')
  22. }

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