Bird
0
0

What will be the output headers if the following code runs in a Node.js HTTP server?

medium📝 component behavior Q4 of 15
Node.js - HTTP Module
What will be the output headers if the following code runs in a Node.js HTTP server?
res.setHeader('X-Custom', '123');
res.setHeader('Content-Type', 'text/plain');
res.end('Hello');
A{ 'Content-Type': 'text/plain' }
B{ 'X-Custom': '123', 'Content-Type': 'text/plain' }
C{ 'X-Custom': '123' }
D{}
Step-by-Step Solution
Solution:
  1. Step 1: Identify headers set before response ends

    The code sets two headers: 'X-Custom' with '123' and 'Content-Type' with 'text/plain'.
  2. Step 2: Confirm headers sent with response

    Both headers remain set when res.end() is called, so both are sent to the client.
  3. Final Answer:

    { 'X-Custom': '123', 'Content-Type': 'text/plain' } -> Option B
  4. Quick Check:

    Headers set before end = A [OK]
Quick Trick: All set headers before res.end() are sent [OK]
Common Mistakes:
  • Assuming only Content-Type is sent
  • Thinking headers reset after first setHeader call
  • Believing headers are sent only after write()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes