Complete the code to import the cluster module in Node.js.
const cluster = require('[1]');
The cluster module is imported using require('cluster') in Node.js.
Complete the code to check if the current process is the master in a cluster.
if (cluster.[1]) { console.log('This is the master process'); }
isMaster which is deprecated in newer Node.js versions.isWorker which is the opposite of master.In Node.js v16+, cluster.isPrimary is used to check if the process is the master (primary) process.
Fix the error in the code to fork a worker process.
const worker = cluster.[1]();start() which does not exist in cluster.spawn() which is from child_process module, not cluster.The correct method to create a new worker process in the cluster module is fork().
Fill both blanks to create a simple HTTP server in a worker process.
if (cluster.isPrimary) { cluster.[1](); } else { const http = require('http'); http.createServer((req, res) => { res.writeHead(200); res.end(`Hello from worker ${cluster.worker.[2]`); }).listen(8000); }
isMaster instead of fork() to create workers.isWorker instead of id to identify workers.The master process forks workers using cluster.fork(). Each worker has an id property used here to identify it.
Fill all three blanks to handle worker exit and restart a new worker.
cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.[1] died with code ${code}`); console.log('Starting a new worker'); cluster.{{BLANK_3}}(); });
pid instead of id for worker identification.signal instead of code for exit code.fork() to restart a worker.When a worker dies, its id and exit code are logged. Then a new worker is started with cluster.fork().