Bird
0
0

You wrote this code snippet to create a cluster but it crashes immediately:

medium📝 Debug Q14 of 15
Node.js - Cluster Module
You wrote this code snippet to create a cluster but it crashes immediately:
const cluster = require('cluster');
cluster.fork();
require('http').createServer((req, res) => res.end('Hello')).listen(3000);
What is the likely cause?
AThe worker process does not listen on a port
BMissing a callback function in cluster.fork()
CNot checking cluster.isMaster before forking
DNo error handling for server creation
Step-by-Step Solution
Solution:
  1. Step 1: Analyze cluster usage

    The code calls cluster.fork() without checking if (cluster.isMaster). Both master and worker processes fork additional processes and attempt to bind to port 3000, causing port conflicts (EADDRINUSE) and crashes.
  2. Step 2: Identify the problem

    The missing if (cluster.isMaster) check before forking leads to repeated forking and server creation attempts, causing the crash.
  3. Final Answer:

    Not checking cluster.isMaster before forking -> Option C
  4. Quick Check:

    Check cluster.isMaster before fork [OK]
Quick Trick: Always check cluster.isMaster before forking [OK]
Common Mistakes:
  • Calling cluster.fork() without isMaster check
  • Assuming fork needs a callback
  • Ignoring server listen port

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes