Bird
0
0

What will be the value of the Content-Type header sent to the client after running this code snippet?

medium📝 component behavior Q13 of 15
Node.js - HTTP Module
What will be the value of the Content-Type header sent to the client after running this code snippet?
const http = require('http');
const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Content-Type', 'application/json');
  res.end('{}');
});
server.listen(3000);
A'text/html'
BBoth 'text/html' and 'application/json'
CNo Content-Type header is sent
D'application/json'
Step-by-Step Solution
Solution:
  1. Step 1: Understand header overwriting behavior

    Setting the same header twice overwrites the previous value in Node.js.
  2. Step 2: Identify final header value

    The second setHeader call sets 'Content-Type' to 'application/json', replacing 'text/html'.
  3. Final Answer:

    'application/json' -> Option D
  4. Quick Check:

    Last setHeader call wins [OK]
Quick Trick: Last setHeader call overwrites previous [OK]
Common Mistakes:
  • Thinking both headers are sent
  • Assuming first header stays
  • Believing no header is sent

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes