Overview - Global middleware
What is it?
Global middleware in NestJS is a function that runs before every route handler in the application. It can inspect, modify, or log requests and responses globally. This means it applies to all incoming HTTP requests without needing to attach it to each route individually. Middleware helps manage common tasks like authentication, logging, or request parsing.
Why it matters
Without global middleware, developers would have to add the same logic to every route, which is repetitive and error-prone. Global middleware centralizes common behaviors, making the app easier to maintain and consistent. It improves developer productivity and ensures that important checks or transformations happen uniformly across the app.
Where it fits
Before learning global middleware, you should understand basic NestJS concepts like modules, controllers, and providers. After mastering global middleware, you can explore more advanced topics like guards, interceptors, and exception filters, which also handle request lifecycle but at different stages.