Bird
0
0

You want to apply two middleware, AuthMiddleware and LoggerMiddleware, only to POST requests on the /orders route. Which code correctly applies both middleware in NestJS?

hard📝 Application Q8 of 15
NestJS - Middleware
You want to apply two middleware, AuthMiddleware and LoggerMiddleware, only to POST requests on the /orders route. Which code correctly applies both middleware in NestJS?
Aconsumer.apply([AuthMiddleware, LoggerMiddleware]).forRoutes('orders');
Bconsumer.apply(AuthMiddleware).apply(LoggerMiddleware).forRoutes('orders');
Cconsumer.apply(AuthMiddleware, LoggerMiddleware).forRoutes({ path: 'orders', method: RequestMethod.POST });
Dconsumer.use(AuthMiddleware, LoggerMiddleware).forRoutes('orders');
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to apply multiple middleware

    consumer.apply() accepts multiple middleware as separate arguments.
  2. Step 2: Specify route and method correctly

    Use forRoutes() with an object specifying path and method to limit to POST /orders.
  3. Final Answer:

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

    Multiple middleware in apply(), route+method in forRoutes() [OK]
Quick Trick: Pass multiple middleware as separate args to apply(), specify route+method in forRoutes() [OK]
Common Mistakes:
  • Chaining apply() calls instead of passing multiple middleware
  • Using use() instead of apply()
  • Passing middleware as array inside apply()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes