Bird
0
0

Consider this Node.js cluster code snippet:

medium📝 component behavior Q13 of 15
Node.js - Cluster Module
Consider this Node.js cluster code snippet:
const cluster = require('cluster');
const http = require('http');

if (cluster.isPrimary) {
  cluster.fork();
  cluster.fork();
} else {
  http.createServer((req, res) => {
    res.end('Worker ' + process.pid);
  }).listen(8000);
}

What will happen when you visit http://localhost:8000 multiple times?
AYou will see responses from different worker process IDs
BOnly one worker will handle all requests
CThe server will crash because of multiple forks
DYou will get a syntax error on startup
Step-by-Step Solution
Solution:
  1. Step 1: Understand cluster.fork creates workers

    Two workers are created, each running the HTTP server on port 8000.
  2. Step 2: Recognize load balancing behavior

    Requests are distributed between workers, so responses show different process IDs.
  3. Final Answer:

    You will see responses from different worker process IDs -> Option A
  4. Quick Check:

    Multiple workers share port, respond with different PIDs [OK]
Quick Trick: Multiple forks = multiple workers respond differently [OK]
Common Mistakes:
  • Thinking only one worker handles all requests
  • Assuming server crashes due to multiple forks
  • Expecting syntax errors from this code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes