0
0
Node.jsframework~10 mins

Why clustering matters for performance in Node.js - Test Your Understanding

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

Complete the code to create a cluster using Node.js cluster module.

Node.js
const cluster = require('cluster');
if (cluster.isMaster) {
  console.log('Master process is running');
  cluster.fork();
} else {
  console.log('Worker process [1]');
}
Drag options to blanks, or click blank then click option'
Aforked
Bstarted
Crunning
Dcreated
Attempts:
3 left
💡 Hint
Common Mistakes
Using past tense like 'started' or 'created' which is less common here.
2fill in blank
medium

Complete the code to get the number of CPU cores for clustering.

Node.js
const os = require('os');
const numCPUs = os.[1]().length;
console.log(`Number of CPUs: ${numCPUs}`);
Drag options to blanks, or click blank then click option'
Acpus
BcpuCount
CcpuCores
DnumCpus
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'cpuCount' or 'numCpus'.
3fill in blank
hard

Fix the error in the code to properly fork workers for each CPU core.

Node.js
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();
  }
}
Drag options to blanks, or click blank then click option'
Aos.cpus().length
BcpuCount
Ccluster.cpus
DcpuCount.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using os.cpus().length directly in the loop condition causes repeated calls.
4fill in blank
hard

Fill both blanks to handle worker exit and restart a new worker.

Node.js
cluster.on('exit', (worker, code, signal) => {
  console.log(`Worker [1] died`);
  cluster.[2]();
});
Drag options to blanks, or click blank then click option'
Aprocess
Bfork
Crestart
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'restart' which is not a cluster method.
5fill in blank
hard

Fill all three blanks to create a simple HTTP server in each worker.

Node.js
const http = require('http');
if (cluster.isWorker) {
  http.createServer((req, res) => {
    res.writeHead([1], {'Content-Type': [2]);
    res.end([3]);
  }).listen(8000);
}
Drag options to blanks, or click blank then click option'
A200
B'text/plain'
C'Hello from worker!'
D'application/json'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status codes or content types like 'application/json'.