Bird
0
0

You want to add middleware that logs request time only for POST requests before handlers. How should you apply it in NestJS?

hard📝 Application Q8 of 15
NestJS - Middleware
You want to add middleware that logs request time only for POST requests before handlers. How should you apply it in NestJS?
AApply middleware only after the route handler
BUse middleware to modify response after handler finishes
CCall next() only for GET requests inside middleware
DUse middleware with a condition checking req.method === 'POST' before calling next()
Step-by-Step Solution
Solution:
  1. Step 1: Understand middleware conditional logic

    Middleware can check req.method and run logic only for POST requests.
  2. Step 2: Correct placement and flow

    Middleware must run before handlers and call next() to continue processing.
  3. Final Answer:

    Use middleware with a condition checking req.method === 'POST' before calling next() -> Option D
  4. Quick Check:

    Conditional middleware runs before handlers [OK]
Quick Trick: Check req.method inside middleware to filter requests [OK]
Common Mistakes:
  • Applying middleware after handlers
  • Calling next() only for wrong methods
  • Modifying response instead of request

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes