0
0
NestJSframework~5 mins

Functional middleware in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is functional middleware in NestJS?
Functional middleware in NestJS is a simple function that takes a request, response, and next callback to process HTTP requests before they reach route handlers.
Click to reveal answer
beginner
How do you define a functional middleware in NestJS?
You define it as a function with parameters (req, res, next) where you can modify the request or response, then call next() to pass control to the next middleware or route handler.
Click to reveal answer
intermediate
How do you apply functional middleware to routes in NestJS?
You apply functional middleware inside the configure() method of a module by using the MiddlewareConsumer's apply() method and specifying the routes with forRoutes().
Click to reveal answer
beginner
What is the role of the next() function in functional middleware?
The next() function passes control to the next middleware or route handler. Without calling next(), the request will not continue and may hang.
Click to reveal answer
intermediate
Can functional middleware in NestJS be asynchronous? How?
Yes, functional middleware can be asynchronous by returning a Promise or using async/await syntax. You must still call next() after async operations complete.
Click to reveal answer
What parameters does a functional middleware function in NestJS receive?
Areq, res, next
Brequest, response, handler
Creq, res, handler
Drequest, response, nextHandler
Where do you apply functional middleware in a NestJS application?
AInside the service classes
BInside the main.ts bootstrap file
CInside the configure() method of a module
DInside the controller methods
What happens if you forget to call next() in functional middleware?
AThe request will hang and not proceed
BThe request will automatically continue
CAn error is thrown immediately
DThe response is sent automatically
Can functional middleware modify the request object?
AOnly the response can be modified
BYes, it can add or change properties on req
CNo, req is read-only
DMiddleware cannot access req
How do you specify which routes a functional middleware applies to?
ABy placing middleware inside the controller
BBy decorating the middleware function
CBy naming the middleware after the route
DUsing forRoutes() method on MiddlewareConsumer
Explain how to create and apply a functional middleware in NestJS.
Think about the middleware function signature and where it is registered.
You got /5 concepts.
    Describe the importance of calling next() in functional middleware and what happens if you don't.
    Consider the flow of request processing.
    You got /4 concepts.