Bird
0
0

Find the bug in this code snippet:

medium📝 Debug Q7 of 15
Node.js - HTTP Module
Find the bug in this code snippet:
import http from 'http';
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end();
  res.end('Hello');
});
server.listen(8080);
AServer.listen port 8080 is invalid
BMissing res.write before res.end
CContent-Type should be application/json
DCalling res.end twice causes an error
Step-by-Step Solution
Solution:
  1. Step 1: Review res.end usage

    res.end ends the response; calling it twice is invalid and causes errors.
  2. Step 2: Check other parts

    res.write is optional before res.end, Content-Type is valid, and port 8080 is valid.
  3. Final Answer:

    Calling res.end twice causes an error -> Option D
  4. Quick Check:

    res.end called once only = D [OK]
Quick Trick: Call res.end only once per response [OK]
Common Mistakes:
  • Calling res.end multiple times
  • Thinking res.write is mandatory
  • Confusing Content-Type values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes