Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
NestJS - Increase response timeout for particular http endpoint

I just started to learn about NestJS and I am wondering how could I manipulate response timeout for particular endpoints?

I could do it on a server level like:

  const server = await app.listen(...);
  server.setTimeout(1800000)

or on endpoint, which looks bad:

  @Post('/test')
  public async import(...props, @Res() res: Response): Promise<string> {
    res.setTimeout(1800000)
  }

But how could I do that on controller or method level? I have tried to increase timeout on endpoint using interceptors like:

import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, take, timeout } from 'rxjs/operators';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {

    return next.handle().pipe(
      timeout(1800000),
      catchError(err => {
        if (err instanceof TimeoutError) {
          return throwError(() => new RequestTimeoutException());
        }
        return throwError(() => err);
      }),
    );
  };
};

And applying it on endpoint like:

  @Post('/test')
  @UseInterceptors(TimeoutInterceptor)
  public async import(...props, @Res() res: Response): Promise<string> {
    long running code...
  }

Although interceptor is triggered so I am able to log something the timeout does not seems to work at all :/

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Okay, if someone is curious this is what I have done:

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const response = context.switchToHttp().getResponse();
    response.setTimeout(600000)

    return next.handle();
  };
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  constructor(
    private readonly reflector: Reflector,
  ) {}

  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const response = context.switchToHttp().getResponse();
    const timeout = this.reflector.get<number>('request-timeout', context.getHandler()) || 60000;
    response.setTimeout(timeout )

    return next.handle();
  };
};
import { applyDecorators, SetMetadata, UseInterceptors } from '@nestjs/common';


const SetTimeout = (timeout: number) => SetMetadata('request-timeout', timeout);

export function SetRequestTimeout(timeout: number = 600000) {
  return applyDecorators(
    SetTimeout(timeout),
    UseInterceptors(TimeoutInterceptor),
  );
}

You might have to play a bit with providers (add the interceptor to it) But now you might only use @SetRequestTimeout() or @SetRequestTimeout(10000) for convenience.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda