ExpressJs — Cluster Mode for Scaling
Node.js runs on a single thread by default. Express apps can use the Node.js 'cluster' module to run multiple instances across CPU cores, improving performance and handling more concurrent requests.
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const cpuCount = os.cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on('exit', () => cluster.fork());
} else {
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Clustered Express App'));
app.listen(3000);
}