0
0
Node.jsframework~10 mins

Load balancing between workers 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'
A"http"
B"os"
C"fs"
D"cluster"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'cluster' to import the cluster module.
Confusing 'os' module with 'cluster'.
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'
AisWorker
BisMaster
CisPrimary
DisMain
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isWorker' instead of 'isMaster'.
Using 'isPrimary' which is from newer Node.js versions, but here we use 'isMaster'.
3fill in blank
hard

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

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

Fill both blanks to create a simple HTTP server in a worker that listens on port 3000.

Node.js
const http = require('http');

http.createServer((req, res) => {
  res.writeHead(200, [1]);
  res.end('Hello from worker');
}).listen([2]);
Drag options to blanks, or click blank then click option'
A{"Content-Type": "text/plain"}
B3000
C200
D8080
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of an object for headers.
Using port 8080 instead of 3000 as asked.
5fill in blank
hard

Fill all three blanks to log when a worker exits and fork a new one.

Node.js
cluster.on('[1]', (worker, code, signal) => {
  console.log(`Worker [2] died with code: ${code}, signal: ${signal}`);
  cluster.[3]();
});
Drag options to blanks, or click blank then click option'
Aexit
Bid
Cfork
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' event instead of 'exit'.
Using 'worker.pid' instead of 'worker.id'.
Using 'disconnect()' instead of 'fork()' to create a new worker.