0
0
NestJSframework~5 mins

Global middleware in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is global middleware in NestJS?
Global middleware in NestJS is a function that runs before every route handler in the application. It can modify requests, responses, or perform actions like logging for all incoming requests.
Click to reveal answer
beginner
How do you apply middleware globally in a NestJS application?
You apply middleware globally by using the app.use() method inside the main.ts file, before the app starts listening. For example: <br>app.use(yourMiddlewareFunction);
Click to reveal answer
intermediate
What is the difference between global middleware and route-specific middleware in NestJS?
Global middleware runs for every request to any route, while route-specific middleware runs only for the routes where it is applied. Global middleware is set in <code>main.ts</code>, route middleware is set in modules using <code>configure()</code> method of the module class implementing <code>NestModule</code>.
Click to reveal answer
beginner
Can global middleware in NestJS modify the request and response objects?
Yes, global middleware can modify both the request and response objects. This allows you to add properties, headers, or perform checks before the request reaches the route handler.
Click to reveal answer
beginner
Where in a NestJS project do you typically register global middleware?
Global middleware is typically registered in the main.ts file, right after creating the app instance and before calling app.listen().
Click to reveal answer
Where do you register global middleware in a NestJS app?
AIn the main.ts file using app.use()
BInside a controller method
CIn a service class
DIn the package.json file
What does global middleware affect in a NestJS app?
AOnly one specific route
BAll incoming requests to any route
COnly outgoing responses
DOnly static files
Which of these is a valid way to apply global middleware in NestJS?
Aservice.use(yourMiddlewareFunction);
Bcontroller.use(yourMiddlewareFunction);
Cmodule.use(yourMiddlewareFunction);
Dapp.use(yourMiddlewareFunction);
Can global middleware modify the request object in NestJS?
ANo, it can only read the request
BNo, it only modifies the response
CYes, it can add or change properties
DNo, middleware cannot access request
What is the main purpose of global middleware?
ATo run code before every route handler
BTo handle errors only
CTo define database models
DTo create UI components
Explain how to set up global middleware in a NestJS application and what it can be used for.
Think about where the app starts and how middleware fits in the request flow.
You got /4 concepts.
    Describe the difference between global middleware and route-specific middleware in NestJS.
    Consider the scope of middleware application.
    You got /4 concepts.