0
0
Node.jsframework~10 mins

How cluster module works in Node.js - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the cluster module in Node.js.

Node.js
const cluster = require('[1]');
Drag options to blanks, or click blank then click option'
Anet
Bhttp
Cfs
Dcluster
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'cluster' to import the cluster module.
Forgetting to use require() to import the module.
2fill in blank
medium

Complete the code to check if the current process is the master in a cluster.

Node.js
if (cluster.[1]) {
  console.log('This is the master process');
}
Drag options to blanks, or click blank then click option'
AisPrimary
BisMaster
CisWorker
DisMain
Attempts:
3 left
💡 Hint
Common Mistakes
Using isMaster which is deprecated in newer Node.js versions.
Using isWorker which is the opposite of master.
3fill in blank
hard

Fix the error in the code to fork a worker process.

Node.js
const worker = cluster.[1]();
Drag options to blanks, or click blank then click option'
Aspawn
Bstart
Cfork
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() which does not exist in cluster.
Using spawn() which is from child_process module, not cluster.
4fill in blank
hard

Fill both blanks to create a simple HTTP server in a worker process.

Node.js
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);
}
Drag options to blanks, or click blank then click option'
Afork
BisMaster
Cid
DisWorker
Attempts:
3 left
💡 Hint
Common Mistakes
Using isMaster instead of fork() to create workers.
Using isWorker instead of id to identify workers.
5fill in blank
hard

Fill all three blanks to handle worker exit and restart a new worker.

Node.js
cluster.on('exit', (worker, code, signal) => {
  console.log(`Worker ${worker.[1] died with code ${code}`);
  console.log('Starting a new worker');
  cluster.{{BLANK_3}}();
});
Drag options to blanks, or click blank then click option'
Aid
Bfork
Ccode
Dpid
Attempts:
3 left
💡 Hint
Common Mistakes
Using pid instead of id for worker identification.
Using signal instead of code for exit code.
Not calling fork() to restart a worker.