0
0
Node.jsframework~10 mins

Master and worker processes in Node.js - Interactive Code 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'
Acluster
Bhttp
Cfs
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' or 'os' instead of 'cluster' to import the cluster module.
2fill in blank
medium

Complete the code to check if the current process is the master process.

Node.js
if (cluster.[1]) {
  console.log('This is the master process');
}
Drag options to blanks, or click blank then click option'
AisMaster
BisPrimary
CisMain
DisWorker
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isWorker' or 'isMaster' instead of 'isPrimary'.
3fill in blank
hard

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

Node.js
if (cluster.isPrimary) {
  cluster.[1]();
}
Drag options to blanks, or click blank then click option'
Afork
Bstart
Cspawn
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'spawn' which are not cluster methods.
4fill in blank
hard

Fill both blanks to listen for 'message' events in the master and worker processes.

Node.js
if (cluster.isPrimary) {
  const worker = cluster.fork();
  worker.[1]('message', (msg) => {
    console.log('Master received:', msg);
  });
} else {
  process.[2]('message', (msg) => {
    console.log('Worker received:', msg);
  });
}
Drag options to blanks, or click blank then click option'
Aon
Bsend
Cemit
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' or 'emit' to listen for messages instead of 'on'.
5fill in blank
hard

Fill all three blanks to send a message from the master to the worker and reply back.

Node.js
if (cluster.isPrimary) {
  const worker = cluster.fork();
  worker.[1]({ cmd: 'start' });
  worker.[2]('message', (msg) => {
    console.log('Master got reply:', msg);
  });
} else {
  process.[3]('message', (msg) => {
    if (msg.cmd === 'start') {
      process.send({ reply: 'done' });
    }
  });
}
Drag options to blanks, or click blank then click option'
Asend
Bon
Cemit
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' or 'listen' instead of 'send' or 'on'.