Middleware in NestJS is a function that runs before the route handler. It can modify the request or response objects or end the request-response cycle.
Click to reveal answer
beginner
How do you create a middleware class in NestJS?
Create a class that implements the <code>NestMiddleware</code> interface and define a <code>use(req, res, next)</code> method.
Click to reveal answer
intermediate
How do you apply middleware to routes in NestJS?
Use the configure() method in a module that implements NestModule and call consumer.apply(MiddlewareClass).forRoutes('route').
Click to reveal answer
beginner
What is the role of the next() function in NestJS middleware?
The next() function passes control to the next middleware or route handler. Without calling it, the request will not proceed.
Click to reveal answer
intermediate
Can NestJS middleware be asynchronous? How?
Yes, middleware can be asynchronous by making the use() method async and using await inside it before calling next().
Click to reveal answer
Which interface must a NestJS middleware class implement?
AMiddlewareHandler
BMiddlewareInterface
CNestMiddleware
DRouteMiddleware
✗ Incorrect
NestJS middleware classes must implement the NestMiddleware interface to define the use() method.
Where do you apply middleware in a NestJS application?
AInside a service class
BDirectly inside a controller method
CIn the main.ts bootstrap file only
DIn the configure() method of a module implementing NestModule
✗ Incorrect
Middleware is applied in the configure() method of a module that implements NestModule using the consumer object.
What happens if you forget to call next() in middleware?
AThe request will hang and not reach the route handler
BThe request will skip the middleware
CThe server will crash
DThe middleware will run twice
✗ Incorrect
Without calling next(), the request does not proceed to the next middleware or route handler, causing it to hang.
Which of these is NOT a valid way to apply middleware in NestJS?
AAdding middleware directly in the controller decorator
B. class MyMiddleware { handle(req, res, next) { next(); } }
C. function MyMiddleware(req, res, next) { next(); }
D. class MyMiddleware { use(req, res, next) { next(); } }
Solution
Step 1: Recall NestJS middleware pattern
Middleware must be a class with a method named use that takes req, res, next.
Step 2: Check options
Only class MyMiddleware { use(req, res, next) { next(); } } defines a class with a use method correctly. class MyMiddleware { handle(req, res, next) { next(); } } uses handle, which is invalid. Options C and D define functions, not classes.
Final Answer:
class MyMiddleware { use(req, res, next) { next(); } } -> Option D
Quick Check:
Middleware class must have use() method [OK]
Hint: Middleware class must have a use() method [OK]
Common Mistakes:
Using method name other than use
Defining middleware as a function instead of class
Forgetting to call next() inside use
3. Given this middleware class, what will be logged when a request to '/some-path' hits the route?
In NestJS, you can specify route and HTTP method using an object with path and method in forRoutes.
Step 2: Evaluate options
Use consumer.apply(MyMiddleware).forRoutes({ path: '/login', method: RequestMethod.POST }) correctly uses { path: '/login', method: RequestMethod.POST }. Use consumer.apply(MyMiddleware).forRoutes('/login') and check method inside middleware applies middleware to all methods on '/login', requiring manual filtering. Apply middleware globally and filter POST requests inside middleware applies globally, which is less efficient. NestJS does not support method-specific middleware is false.
Final Answer:
Use consumer.apply(MyMiddleware).forRoutes({ path: '/login', method: RequestMethod.POST }) -> Option B
Quick Check:
Use forRoutes with path and method object [OK]
Hint: Use forRoutes with path and method object for specific HTTP verbs [OK]
Common Mistakes:
Applying middleware to all methods unintentionally
Not importing RequestMethod enum
Assuming method-specific middleware is unsupported
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.