Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q6 of 15
Node.js - HTTP Module
What is wrong with this code snippet?
res.setHeader('Content-Type', 'text/html');
res.end('Hello');
res.setHeader('X-Test', '123');
Ares.end() must be called with no arguments
BContent-Type header value is invalid
CHeaders cannot be set after res.end() is called
DX-Test header name is reserved and cannot be used
Step-by-Step Solution
Solution:
  1. Step 1: Understand response lifecycle

    Once res.end() is called, the response is finished and headers cannot be modified.
  2. Step 2: Identify the error in code order

    The code tries to set a header after ending the response, which is invalid and will cause an error.
  3. Final Answer:

    Headers cannot be set after res.end() is called -> Option C
  4. Quick Check:

    Set headers before res.end() only = A [OK]
Quick Trick: Set all headers before calling res.end() [OK]
Common Mistakes:
  • Setting headers after response ended
  • Misunderstanding res.end() arguments
  • Thinking header names are restricted

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes