Complete the code to create a cluster using Node.js cluster module.
const cluster = require('cluster'); if (cluster.isMaster) { console.log('Master process is running'); cluster.fork(); } else { console.log('Worker process [1]'); }
The worker process logs 'running' to indicate it is active.
Complete the code to get the number of CPU cores for clustering.
const os = require('os'); const numCPUs = os.[1]().length; console.log(`Number of CPUs: ${numCPUs}`);
The correct method to get CPU info is os.cpus().
Fix the error in the code to properly fork workers for each CPU core.
const cluster = require('cluster'); const os = require('os'); if (cluster.isMaster) { const cpuCount = os.cpus().length; for (let i = 0; i < [1]; i++) { cluster.fork(); } }
os.cpus().length directly in the loop condition causes repeated calls.The variable cpuCount holds the number of CPUs and should be used in the loop.
Fill both blanks to handle worker exit and restart a new worker.
cluster.on('exit', (worker, code, signal) => { console.log(`Worker [1] died`); cluster.[2](); });
When a worker process dies, log 'process' and fork a new worker.
Fill all three blanks to create a simple HTTP server in each worker.
const http = require('http'); if (cluster.isWorker) { http.createServer((req, res) => { res.writeHead([1], {'Content-Type': [2]); res.end([3]); }).listen(8000); }
The server responds with status 200, content type text/plain, and a simple text message.
