Bird
0
0

Given the following code snippet, what will be logged when a worker process receives a request?

medium📝 component behavior Q4 of 15
Node.js - Cluster Module
Given the following code snippet, what will be logged when a worker process receives a request?
const cluster = require('cluster');
const http = require('http');

if (cluster.isPrimary) {
  cluster.fork();
} else {
  http.createServer((req, res) => {
    console.log('Worker handling request');
    res.end('Hello');
  }).listen(8000);
}
AWorker handling request
BPrimary handling request
CNo output
DError: cluster.fork() not called
Step-by-Step Solution
Solution:
  1. Step 1: Understand cluster roles

    The primary process forks a worker, which runs the server code.
  2. Step 2: Identify where console.log runs

    The console.log is inside the worker's HTTP server request handler, so it logs on each request.
  3. Final Answer:

    Worker handling request -> Option A
  4. Quick Check:

    Request logs = Worker handling request [OK]
Quick Trick: Workers handle requests and log messages [OK]
Common Mistakes:
  • Thinking primary handles requests
  • Expecting no output on requests
  • Assuming error without fork

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes