NodeJS

    How to create Intercepter in nestjs

     

    We utilize cookies that are essential for providing responses. By continuing to engage with our Center of Excellence topic, you consent to our use of cookies.


    Introduction

    In NestJS, interceptors serve functions such as modifying the incoming request, the outgoing response, logging, transformations, and caching. They share a lot of characteristics with middleware, although they are much more exact, such as transforming responses or adding more functionality.

    Follow these instructions to make and implement interceptors in NestJS:

    Step 1: Create an Interceptor

    You may create the file directly or use the NestJS CLI to create the interceptor.

    Type the following command into the CLI:

    nest g interceptor logging

    A new file called logging.interceptor.ts will now be generated.

    Step 2: Implement the Interceptor Logic

    For interceptors to work, interceptors need to implement the NestInterceptor interface that has an intercept() method. This method comes with two parameters: the context of the execution and a handler which lets one pass the request to the next function in the lifecycle.

    Here is all the code for the logging interceptor, which logs the request and the response:

     

    import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';import { Observable } from 'rxjs';import { tap } from 'rxjs/operators';@Injectable()export class LoggingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { const now = Date.now(); console.log('Before the handler...'); return next .handle() .pipe( tap(() => console.log(`After the handler... ${Date.now() - now}ms`)), ); }}

     

    In this case, the intercept method measures time, and can therefore be used for performance monitoring.

    Step 3: Apply the Interceptor

    An interceptor can be applied at the method level, the controller level, or globally.

    Globally: In main.ts:

    import { LoggingInterceptor } from './logging.interceptor';import { AppModule } from './app.module';async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalInterceptors(new LoggingInterceptor()); await app.listen(3000);}bootstrap();

     

    Controller Level: For all methods within a controller, you set the interceptor.

    import { Controller, UseInterceptors, Get } from '@nestjs/common';import { LoggingInterceptor } from './logging.interceptor';@Controller('example')@UseInterceptors(LoggingInterceptor)export class ExampleController { @Get() getExample() { return 'This is an example'; }}

     

    Method Level: Applying the interceptor to a particular route.

    @Get('example')@UseInterceptors(LoggingInterceptor)getExample() { return 'This is an example';}

    Contact Us

    Comment

    Share

    facebook
    LinkedIn
    Twitter
    Mail
    NodeJS

    Related Center Of Excellence