user2334711
user2334711

Reputation: 201

NestJS - request timeout

How to set timeout for all requests and if timedout then respond with custom json?

I tried to use:

import * as timeout from 'connect-timeout';

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';

const port = process.env.PORT || 3020;

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);
  app.use(timeout('5s'));
  app.use(haltOnTimedOut);

  await app.listen(port);
} 
bootstrap();


function haltOnTimedOut (req, res, next) {
  if (!req.timedout){
      next();
  } else {
      res
        .status(408)
        .json({ status: 'error', data: 'timeout'})
  }
}

but with no luck.

Upvotes: 19

Views: 31464

Answers (4)

Ehsan Kazi
Ehsan Kazi

Reputation: 429

NestJS has a feature called Interceptors. Interceptors can be used for the purpose of forcing timeouts, they demonstrate it here, TimeoutInterceptor.

Suppose you have got your Interceptor in a file called timeout.interceptor.ts:

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

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(5000),
      catchError(err => {
        if (err instanceof TimeoutError) {
          return throwError(new RequestTimeoutException());
        }
        return throwError(err);
      }),
    );
  };
};

After this, you have to register it, which can be done in several ways. The global registration way is shown below:

const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new TimeoutInterceptor());

Upvotes: 11

Antony Judes
Antony Judes

Reputation: 241

To increase the nestjs - API application server request/response timeout, I did the following in main.js

const server = await app.listen(5000);
server.setTimeout(1800000); // 600,000=> 10Min, 1200,000=>20Min, 1800,000=>30Min

Upvotes: 24

Omkar Yadav
Omkar Yadav

Reputation: 543

You can pass a express instance to the NextFactory.create(module, expressInstance) so you can add the middleware to that express instance like

const expressInstance = express();
express.use(timeout('4'));
express.use((err, res, req, next) => req.jsonp(err)); // DON'T USE FOR PRODUCTION
const app = NestFactory.create(AppModule, express);

It should work.

Upvotes: 1

David R
David R

Reputation: 15637

Move your app.use(timeout('5s')); out of your bootstrap function and remove the else block from haltOnTimedOut function as well.

And try calling your bootstrap function as a middleware as given below,

app.use(boostrap, timeout('5s'), bodyParser.json(), haltOnTimedout, function (req, res, next) {
if (req.timedout) return
      res
        .status(408)
        .json({ status: 'error', data: 'timeout'})
});

Hope this helps!

Upvotes: -1

Related Questions