Bird
0
0

Consider this NestJS middleware application:

medium📝 component behavior Q5 of 15
NestJS - Middleware
Consider this NestJS middleware application:
consumer.apply(AuthMiddleware).forRoutes({ path: 'admin', method: RequestMethod.GET });

Which requests will trigger AuthMiddleware?
AOnly POST requests to /admin
BAll requests to /admin
CAll GET requests to any route
DOnly GET requests to /admin
Step-by-Step Solution
Solution:
  1. Step 1: Analyze route and method filter

    forRoutes with object specifying path and method applies middleware only to that method on that path.
  2. Step 2: Match method and path

    Here, only GET requests to /admin trigger the middleware.
  3. Final Answer:

    Only GET requests to /admin -> Option D
  4. Quick Check:

    Middleware applies to specified method and path only [OK]
Quick Trick: Use object with path and method to limit middleware to specific HTTP methods [OK]
Common Mistakes:
  • Assuming middleware applies to all methods on the path
  • Confusing GET with POST methods
  • Thinking middleware applies globally to all GET requests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes