Bird
0
0

Given this middleware application code in a NestJS module:

medium📝 component behavior Q13 of 15
NestJS - Middleware
Given this middleware application code in a NestJS module:
consumer.apply(AuthMiddleware).forRoutes('users');

Which routes will have the AuthMiddleware applied?
AAll routes including '/users' and '/users/:id'
BOnly routes with path exactly '/users'
CAll routes in the application
DNo routes, because the path is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand forRoutes string behavior

    Passing 'users' applies middleware to all routes starting with '/users', including parameters.
  2. Step 2: Confirm middleware coverage

    This includes '/users', '/users/123', '/users/profile', etc., not the entire app.
  3. Final Answer:

    All routes including '/users' and '/users/:id' -> Option A
  4. Quick Check:

    forRoutes('users') covers prefix routes [OK]
Quick Trick: forRoutes('path') applies middleware to that path and subpaths [OK]
Common Mistakes:
  • Thinking only exact path matches get middleware
  • Assuming middleware applies globally without '*'
  • Believing invalid path disables middleware

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes