Bird
0
0

You want to set multiple headers at once in a Node.js HTTP response. Which approach correctly sets Content-Type to application/json and Cache-Control to no-store?

hard📝 Application Q8 of 15
Node.js - HTTP Module
You want to set multiple headers at once in a Node.js HTTP response. Which approach correctly sets Content-Type to application/json and Cache-Control to no-store?
Ares.setHeader({'Content-Type': 'application/json', 'Cache-Control': 'no-store'});
Bres.setHeader('Content-Type', 'application/json'); res.setHeader('Cache-Control', 'no-store');
Cres.headers = { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' };
Dres.writeHead({ 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
Step-by-Step Solution
Solution:
  1. Step 1: Understand setHeader usage

    res.setHeader() sets one header at a time; it does not accept an object.
  2. Step 2: Identify correct multiple header setting

    Calling setHeader twice sets both headers correctly. res.writeHead({ 'Content-Type': 'application/json', 'Cache-Control': 'no-store' }); uses writeHead without a status code, which causes an error since status code is required.
  3. Final Answer:

    res.setHeader('Content-Type', 'application/json'); res.setHeader('Cache-Control', 'no-store'); -> Option B
  4. Quick Check:

    Set headers one by one with setHeader() = C [OK]
Quick Trick: Call setHeader separately for each header [OK]
Common Mistakes:
  • Passing object to setHeader()
  • Assigning headers directly to res.headers
  • Confusing writeHead() with setHeader()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes