Last active
March 1, 2023 04:38
-
-
Save Maxim-Kolmogorov/05fceceb73de0e883e04fca96d8cab65 to your computer and use it in GitHub Desktop.
Nuxt.js (v2) with Node.js clusters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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