Recall & Review
beginner
What is middleware in NestJS?
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?
✗ Incorrect
NestJS middleware classes must implement the NestMiddleware interface to define the use() method.
Where do you apply middleware in a NestJS application?
✗ 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?
✗ 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?
✗ Incorrect
Middleware cannot be added directly in controller decorators; it must be applied via the module's configure() or globally.
Can middleware modify the response object before sending it?
✗ Incorrect
Middleware can modify both request and response objects before passing control along.
Explain how to create and apply middleware in a NestJS module.
Think about the class, interface, and where you connect middleware to routes.
You got /5 concepts.
Describe the role of the next() function in NestJS middleware and what happens if it is not called.
Consider how middleware controls the flow of a request.
You got /4 concepts.