0
0
Node.jsframework~10 mins

Cluster vs reverse proxy decision 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 create a cluster that forks worker processes.

Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < [1]; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Hello from worker ' + process.pid);
  }).listen(8000);
}
Drag options to blanks, or click blank then click option'
Aprocess
Bcluster
Chttp
DnumCPUs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cluster' or 'http' instead of the CPU count variable.
2fill in blank
medium

Complete the code to set up a simple reverse proxy using the 'http-proxy' library.

Node.js
const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});

const server = http.createServer((req, res) => {
  proxy.web(req, res, { target: [1] });
});

server.listen(8000);
Drag options to blanks, or click blank then click option'
A'http://localhost:8000'
B'https://example.com'
C'http://localhost:3000'
D'http://127.0.0.1:8080'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same port as the proxy server or an unrelated URL.
3fill in blank
hard

Fix the error in the cluster code to properly listen on a port in each worker.

Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Worker ' + process.pid);
  }).listen([1]);
}
Drag options to blanks, or click blank then click option'
A'8000'
B8000
Cprocess.pid
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.pid or a string instead of a number for the port.
4fill in blank
hard

Fill both blanks to create a cluster that logs when a worker exits and forks a new one.

Node.js
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < [1]; i++) {
    cluster.fork();
  }

  cluster.on('[2]', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });
}
Drag options to blanks, or click blank then click option'
AnumCPUs
Bexit
Cdisconnect
Donline
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong event names like 'disconnect' or 'online' for worker death.
5fill in blank
hard

Fill all three blanks to create a reverse proxy that logs requests and forwards them to a backend.

Node.js
const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});

const server = http.createServer((req, res) => {
  console.log('Request URL:', [1]);
  proxy.web(req, res, { target: [2] });
});

server.listen([3]);
Drag options to blanks, or click blank then click option'
Areq.url
B'http://localhost:4000'
C8080
Dreq.headers
Attempts:
3 left
💡 Hint
Common Mistakes
Logging headers instead of URL, wrong target URL, or wrong listen port.