Bird
0
0

You want to create a functional middleware that adds a custom header 'X-App-Version' with value '1.0' to every response. Which code correctly implements this?

hard📝 Application Q15 of 15
NestJS - Middleware
You want to create a functional middleware that adds a custom header 'X-App-Version' with value '1.0' to every response. Which code correctly implements this?
Afunction addVersion(req, res, next) { res.setHeader('X-App-Version', '1.0'); next(); }
Bfunction addVersion(req, res) { res.setHeader('X-App-Version', '1.0'); }
Cfunction addVersion(req, res, next) { next(); res.setHeader('X-App-Version', '1.0'); }
Dfunction addVersion(req, res, next) { req.setHeader('X-App-Version', '1.0'); next(); }
Step-by-Step Solution
Solution:
  1. Step 1: Confirm middleware signature and header setting

    Middleware must accept (req, res, next) and call next() after setting headers. res.setHeader is correct method.
  2. Step 2: Evaluate options

    function addVersion(req, res, next) { res.setHeader('X-App-Version', '1.0'); next(); } sets header then calls next() correctly. function addVersion(req, res) { res.setHeader('X-App-Version', '1.0'); } misses next parameter and call. function addVersion(req, res, next) { next(); res.setHeader('X-App-Version', '1.0'); } calls next() before setting header, so header may not be sent. function addVersion(req, res, next) { req.setHeader('X-App-Version', '1.0'); next(); } uses req.setHeader which is incorrect because req cannot set response headers.
  3. Final Answer:

    function addVersion(req, res, next) { res.setHeader('X-App-Version', '1.0'); next(); } -> Option A
  4. Quick Check:

    Set header before next() with correct method [OK]
Quick Trick: Set header before next() using res.setHeader [OK]
Common Mistakes:
  • Calling next() before setting headers
  • Omitting next parameter or call
  • Confusing req and res when setting headers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes