nuxt-community / express-template

Starter template for Nuxt 2 with Express.
https://codesandbox.io/s/github/nuxt-community/express-template
1.25k stars 239 forks source link

Concurrency in express #190

Closed ryansosin closed 3 years ago

ryansosin commented 3 years ago

How can I leverage concurrency for my Express instance on a multi-core server in this setup? Since Express is never set to listen, I am not sure where I would leverage the Cluster API or a tool like Throng. Thanks!

farnabaz commented 3 years ago

In your case, instead of using express as middleware for Nuxt, you can use Nuxt as middleware for express.

You can create server.js to start express server (using Cluster API) and register Nuxt as part of it.

// server.js
const { loadNuxt, build } = require('nuxt')

const app = require('express')()
const isDev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000

async function start() {
  // We get Nuxt instance
  const nuxt = await loadNuxt(isDev ? 'dev' : 'start')

  // Render every route with Nuxt.js
  app.use(nuxt.render)

  // Build only in dev mode with hot-reloading
  if (isDev) {
    build(nuxt)
  }
  // Listen the server
  app.listen(port, '0.0.0.0')
  console.log('Server listening on `localhost:' + port + '`.')
}

start()

Checkout Docs

ryansosin commented 3 years ago

Nice! I will give that a shot. Thanks for the direction.