Whats the difference between interceptor vs middleware vs filter in nest js
As you've already described in the question, these three are very similar concepts, and it can be challenging to decide in many cases, depending on your preference.But I can outline the differences:InterceptorsInterceptors can access the request/response before and after calling the route handler.Registrationdirectly in controller classes with controller or method scopeGlobal scope in ExamplesLoggingInterceptor: Request before the route handler and after its result. Measure the time required.ResultMapping: Convert to or wrap the result in a response object: → ConclusionCompared to middleware, I prefer registering closer to the route handler. However, there are some limitations—for example, when you send a library-specific response object from the route handler, you cannot set the response code or modify the response using interceptors; see documentation. MiddlewareMiddleware is called only before calling the route handler. You can access the response object but not the result of the route handler. They essentially implement middleware functionality.RegistrationWithin modules, selecting relevant routes is highly flexible (using wildcards, by method, etc.)Global scope in ExamplesFrontendMiddleware: Redirect all routes except API to ; see this threadYou can use any existing Express middleware. There are many libraries, such as or ConclusionMiddleware registration is highly flexible—for example, applying to all routes except one. However, since they are registered within modules, when reviewing their methods, you might not realize they apply to your controllers. You can also leverage all existing Express middleware libraries, which is great.Exception FiltersException filters are called after the route handler and interceptors. They are the last place to modify the response before it is sent.Registrationdirectly in controller classes with controller or method scopeGlobal scope in your ExamplesUnauthorizedFilter: Map to user-friendly messagesNotFoundFilter: Map all not-found routes (not part of your API) to your .ConclusionThe primary use case for exception filters is providing understandable error messages (hiding technical details). However, there are creative uses: when providing a single-page application, all routes except API routes should typically be redirected to . Here, you can redirect to . Some might find this clever, while others might consider it old-fashioned. Your choice. ;-)So the execution order is: middleware -> interceptors -> route handler -> interceptors -> exception filters (if an exception is thrown).For these three tools, you can inject other dependencies (such as services, etc.) into their constructors.