Skip to content

Instantly share code, notes, and snippets.

@Maxim-Kolmogorov
Last active March 1, 2023 04:38
Show Gist options
  • Save Maxim-Kolmogorov/05fceceb73de0e883e04fca96d8cab65 to your computer and use it in GitHub Desktop.
Save Maxim-Kolmogorov/05fceceb73de0e883e04fca96d8cab65 to your computer and use it in GitHub Desktop.
Nuxt.js (v2) with Node.js clusters.
// Create cluster.js in application folder,
// install "npm i nuxt-start",
// before start do "npm run build".
// Start this via "node ./cluster.js"
const cluster = require('cluster')
const os = require('os')
const { loadNuxt } = require('nuxt-start')
const port = 3000
const main = async () => {
const nuxt = await loadNuxt({ for: 'start' })
await nuxt.listen(port)
.then(() => {
// eslint-disable-next-line no-console
console.log(`Worker ${cluster.worker.id} started`)
})
}
if (cluster.isMaster) {
const cpus = os.cpus().length
for (let i = 0; i < cpus; i++) {
cluster.fork()
}
cluster.on('exit', (worker, code) => {
// eslint-disable-next-line no-console
console.log(`Worker ${worker.id} finished with an error code: ${code}`)
cluster.fork()
})
} else {
main()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment