Bird
0
0

What is the issue with this Node.js HTTP server code?

medium📝 Debug Q6 of 15
Node.js - HTTP Module
What is the issue with this Node.js HTTP server code?
import http from 'http';
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end(JSON.stringify({ message: 'Hi' }));
});
server.listen('port3000');
AJSON.stringify should not be used inside res.end()
BThe Content-Type header is incorrect for JSON responses
CThe port argument to listen() is invalid; it must be a number
DThe server.listen() method is missing a callback function
Step-by-Step Solution
Solution:
  1. Step 1: Check server.listen argument

    The argument 'port3000' is a string and not a valid port number.
  2. Step 2: Validate port type

    listen() expects a numeric port, e.g., 3000, not a string with letters.
  3. Final Answer:

    The port argument to listen() is invalid; it must be a number -> Option C
  4. Quick Check:

    Port must be numeric, not a string with letters [OK]
Quick Trick: Port must be a number, not a string [OK]
Common Mistakes:
  • Passing port as a string with letters
  • Confusing Content-Type header for JSON
  • Thinking callback is mandatory for listen()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes