Bird
0
0

You want to apply LoggerMiddleware only to POST requests on the /orders route in NestJS. Which approach correctly applies middleware only to POST /orders requests?

hard📝 Application Q15 of 15
NestJS - Middleware
You want to apply LoggerMiddleware only to POST requests on the /orders route in NestJS. Which approach correctly applies middleware only to POST /orders requests?
AApply middleware globally and skip non-POST requests inside middleware
BUse consumer.apply(LoggerMiddleware).forRoutes('orders'); and check method inside middleware
CUse consumer.apply(LoggerMiddleware).forRoutes('orders'); without method filtering
DUse consumer.apply(LoggerMiddleware).forRoutes({ path: 'orders', method: RequestMethod.POST });
Step-by-Step Solution
Solution:
  1. Step 1: Understand method filtering in middleware

    NestJS allows specifying method with an object: { path, method } in forRoutes.
  2. Step 2: Identify correct syntax for POST-only middleware

    Using consumer.apply(...).forRoutes({ path: 'orders', method: RequestMethod.POST }) applies middleware only to POST /orders.
  3. Final Answer:

    Use consumer.apply(LoggerMiddleware).forRoutes({ path: 'orders', method: RequestMethod.POST }); -> Option D
  4. Quick Check:

    Use object with path and method in forRoutes for method filtering [OK]
Quick Trick: Use forRoutes({path, method}) to target HTTP methods [OK]
Common Mistakes:
  • Applying middleware to all methods without filtering
  • Checking method inside middleware instead of in forRoutes
  • Applying middleware globally and filtering inside middleware

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes