Clusters of Node.js processes can be used to run multiple instances of Node.js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.js instance.
The cluster module allows easy creation of child processes that all share server ports.
const cluster = require("cluster");const os = require("os");const process = require("process");const app = require("express")();const numCPUs = os.cpus().length;if (cluster.isPrimary) { console.log(`Primary $${process.pid} is running`); // Fork workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); }} else { app.get("/", (req, res) => { res.writeHead(200); res.end("hello world\n"); }); app.listen(8000); console.log(`Worker$$ {process.pid} started`);}
Running Node.js will now share port 8000 between the workers: