Bird
0
0

Which of the following is the correct syntax to apply a middleware named LoggerMiddleware to all routes in a NestJS module?

easy📝 Syntax Q12 of 15
NestJS - Middleware
Which of the following is the correct syntax to apply a middleware named LoggerMiddleware to all routes in a NestJS module?
Aconsumer.use(LoggerMiddleware).forRoutes('/');
Bconsumer.apply(LoggerMiddleware).forRoutes('/');
Cconsumer.apply(LoggerMiddleware).forRoutes('*');
Dconsumer.apply(LoggerMiddleware).toRoutes('*');
Step-by-Step Solution
Solution:
  1. Step 1: Recall NestJS middleware syntax

    The correct method to apply middleware is consumer.apply(...).forRoutes(...).
  2. Step 2: Identify correct route pattern for all routes

    Using '/' applies middleware to all routes; '*' is invalid.
  3. Final Answer:

    consumer.apply(LoggerMiddleware).forRoutes('/'); -> Option B
  4. Quick Check:

    Use apply + forRoutes('/') for all routes [OK]
Quick Trick: Use apply() with forRoutes('/') to cover all routes [OK]
Common Mistakes:
  • Using use() instead of apply()
  • Using toRoutes() which is invalid
  • Using '*' instead of '/' for all routes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes