The phrase "interceptors" is not a standard feature built into Node.js, although it is frequently associated with the ability to intercept and manage requests or responses, particularly when making HTTP queries. Interceptor functionality is often provided by particular libraries, such as Axios, which is commonly used in Node.js applications to make HTTP requests.
Interceptors in Node.js allow you to run code or do certain actions before or after a request is delivered or received. You can define interceptors for requests and responses in Axios.
In Express.js, the manipulation of requests and responses is mainly done through middleware. Although the term "interceptors" is not used directly, the functionality is similar. Middleware can be used to intercept requests before they reach route handlers and to intercept responses before, they are sent to the client.
The concept of interceptors is often used in specific libraries or design patterns to perform tasks such as data manipulation, error handling, authorization, etc. We can include interceptors in event handlers, promises, middlewares or in HTTP requests.
Event handlers can be thought of as functions that are executed in response to the emission of certain events. These handlers can be considered "interceptors" in the sense that they intercept and respond to specific events. In node they can be implemented as follows:
The .then() method acts as an "interceptor" to handle successful resolution of the promise. Additional actions can be taken with the result of the promise and return a new value if necessary.
The .catch() method acts as an "interceptor" to handle any errors that occur during the execution of the promise. You can perform specific error handling actions within this block.
These methods (then and catch) allow you to chain actions and handle results or errors in a more structured way compared to traditional nested callbacks. While they are not specifically called "interceptors," they serve a similar role in allowing specific events in the lifecycle of a promise to be intercepted and handled.
In the context of Express.js, middleware acts similar to interceptors, allowing you to perform specific tasks before or after a request reaches a route handler. Middleware are functions that have access to the request (req), response (res) objects, and the next function that passes execution to the next middleware in the stack.
The first middleware runs before each request and displays information about the request in the console. The request is then passed to the corresponding route handler (app.get('/') in this case). After the response is sent to the client, the second middleware runs and displays information about the response in the console.
These middlewares act as interceptors in the sense that they allow specific actions to be performed before or after the execution of the route handlers. You can have multiple middlewares, and their declaration order is important, as it determines the order in which they are executed. Additionally, middleware can be used for specific tasks, such as authentication, error handling, data analysis, etc.
In the context of Node.js and HTTP requests, the term "interceptors" is often associated with libraries such as Axios, which allow you to intercept and modify HTTP requests and responses. This is especially useful for adding custom functionality such as header manipulation, error handling, authentication, and more.
axios.interceptors.request.use adds an interceptor for all outgoing HTTP requests. You can modify the request settings before it is sent.
axios.interceptors.response.use adds an interceptor for all incoming HTTP responses. You can modify the response data before it is delivered to the code that made the request.
These interceptors give you complete control over HTTP requests and responses and are useful for applying common logic throughout the application, such as handling authentication tokens, error handling, logging, etc.
Node.js is one of the most used technologies today for web development and at Rootstack we have a team of experts with up-to-date knowledge.