Bird
0
0

How can NestJS middleware add custom properties to the request object so that downstream handlers can access them?

hard📝 Application Q9 of 15
NestJS - Middleware
How can NestJS middleware add custom properties to the request object so that downstream handlers can access them?
ABy attaching new properties directly to the req object inside middleware
BBy modifying the response object instead of the request
CBy returning a new request object from middleware
DBy calling next() with additional arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand request object mutability

    The req object is mutable and shared through middleware and handlers.
  2. Step 2: Add properties directly

    Middleware can add custom fields like req.user or req.timestamp.
  3. Final Answer:

    By attaching new properties directly to the req object inside middleware -> Option A
  4. Quick Check:

    Modify req object to pass data downstream [OK]
Quick Trick: Add properties directly to req object [OK]
Common Mistakes:
  • Trying to replace req object entirely
  • Modifying res instead of req
  • Passing data via next() arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes