{

Difference between Interceptor middleware and Filters in Nestjs


This tutorial talks about the usage of interceptors, middleware and filters in NestJS.

NestJS uses the below layer for the request and response life cycle.

Each request to the nestjs applicaiton executes the flow of functions or objects as given below Routehandler-Controller - Service- DAO- Database

There are interceptors, middlewares, and Exception filters that executes before and after of above components

What is Interceptor in NestJS

Execution life cycle Interceptors are functions, called before and after route handlers. It has access to Request and response objects which includes the result of Route handler execution.

Configuration: We can configure the Interceptors in the following

  • In the controller class or method level using @UseInterceptors decorator
  • Globally that applies to all route handlers using app.useGlobalInterceptors() in main.ts.

Use cases:

  • Request and response modification
  • Logger for the time taken for request and response execution
  • Transform the result from functions and route handler
  • Custom logical functionality configuration before and after method execution

What is Middleware in NestJS

Execution life cycle

Middleware contains functions that are called before Route handlers. These have access to Http Request and Http Response objects. These are similar to Express Middleware. It does not have access to the result of route handler execution ie response returned from the controller.

You can configure multiple middleware in Request and response cycles. Middleware functions logic execution done, and Calling next() function in current middleware denotes that delegates the request to next middleware in the life cycle of request.

These are called before the request reached to nestjs controller. Configuration: Middlewares can configure either Globally or based on route wildcards and specific routes or by methods etc..

Middleware functions can be used in the following use cases.

  • Authentication and Authrization functionality such as token validation
  • request Body parser
  • Request and Response body Modification

What are Exception Filters in nestJS

Execution life cycle

Filters also called exception filters to handle customized exceptions for the user. These are called after-route hanlders and interceptors.

Configuration: We can configure the Exception filters in the following

  • In the controller class or method level using the @UseFilters decorator
  • Globally that applies to all route handlers using app.useGlobalFilters() in main.ts.

use cases

  • Handles Unauthorized path logic
  • 404 exception handler
  • server error handler
  • Central Global exception handler

Here is the execution flow of request and response in NestJS application

Middleware->Interceptors->Route handlers->Controllers->Interceptors-> Filters
                                                        |
                                                    Services

Conclusion

Learned interceptors, middilewares, and filters and their execution flow in request response flow.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.