From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Introduction to interceptors

Introduction to interceptors

Now, we are going to learn about one more important fundamental concept in NestJS, which is called interceptors. Interceptors basically are just like the middleware. As they intercept the request before it even reaches the route handlers, and they can also intercept the response. So it has a direct access to both the request and response. We can definitely say that they are like middleware but with more powerful capabilities. Using interceptors we can modify the request by adding certain data in it or we can validate the request to meet certain criteria and many other similar things like that. All this before it reaches the route handler in fact. The same thing for the responses as well, like caching or error handling, transformations, etc. So an interceptor is what lies between a client and the Nest API endpoint. And we can do anything we want with the request or response before it reaches the destination. Now basically, interceptors are executed after the middleware and guards…

Contents