A. Middleware class must be decorated with @Injectable
B. Middleware is applied globally without specifying routes
C. Missing route argument in forRoutes causes a runtime error
D. MiddlewareConsumer is not imported correctly
Solution
Step 1: Check forRoutes usage
The method forRoutes requires at least one argument specifying routes; empty call is invalid.
Step 2: Identify error type
Calling forRoutes() without arguments causes a runtime error because no routes are defined.
Final Answer:
Missing route argument in forRoutes causes a runtime error -> Option C
Quick Check:
forRoutes() needs routes argument [OK]
Hint: Always provide routes to forRoutes() [OK]
Common Mistakes:
Leaving forRoutes empty
Assuming middleware applies globally without routes
Ignoring missing @Injectable on middleware
5. 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
A. Apply middleware globally and skip non-POST requests inside middleware
B. Use consumer.apply(LoggerMiddleware).forRoutes('orders'); and check method inside middleware
C. Use consumer.apply(LoggerMiddleware).forRoutes('orders'); without method filtering
D. Use consumer.apply(LoggerMiddleware).forRoutes({ path: 'orders', method: RequestMethod.POST });
Solution
Step 1: Understand method filtering in middleware
NestJS allows specifying method with an object: { path, method } in forRoutes.
Step 2: Identify correct syntax for POST-only middleware
Using consumer.apply(...).forRoutes({ path: 'orders', method: RequestMethod.POST }) applies middleware only to POST /orders.
Final Answer:
Use consumer.apply(LoggerMiddleware).forRoutes({ path: 'orders', method: RequestMethod.POST }); -> Option D
Quick Check:
Use object with path and method in forRoutes for method filtering [OK]
Hint: 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.