DUse app.useGlobalMiddleware(LoggerMiddleware) with method filter
Step-by-Step Solution
Solution:
Step 1: Understand route filtering in middleware
NestJS allows filtering middleware by path and HTTP method using forRoutes with an object.
Step 2: Apply middleware globally but only for POST
Using forRoutes({ path: '*', method: RequestMethod.POST }) applies middleware to all paths but only POST requests.
Step 3: Evaluate other options
Use consumer.apply(LoggerMiddleware).forRoutes('*') and check req.method inside middleware applies middleware on all methods, not just POST. Apply middleware only on POST routes manually, no global option is manual, not global. Use app.useGlobalMiddleware(LoggerMiddleware) with method filter is invalid syntax.
Final Answer:
Use consumer.apply(LoggerMiddleware).forRoutes({ path: '*', method: RequestMethod.POST }) -> Option C
Quick Check:
forRoutes with method filter = selective global middleware [OK]
Quick Trick:Use forRoutes({ path: '*', method: RequestMethod.POST }) for selective global [OK]
Common Mistakes:
Applying middleware globally without method filter