96
rated 0 times
[
101]
[
5]
/ answers: 1 / hits: 7087
/ 2 Years ago, thu, may 19, 2022, 12:00:00
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 :/
More From » node.js