16

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.

user2334711
  • 171
  • 1
  • 1
  • 3

4 Answers4

23

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
David Buck
  • 3,752
  • 35
  • 31
  • 35
Antony Judes
  • 231
  • 2
  • 3
12

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());
Ehsan Kazi
  • 388
  • 3
  • 8
  • The problem with this approach is that a lot of code can run before interceptors (eg your guards or middleware), and you still want your timeout to apply to those. – roim Apr 16 '21 at 01:16
1

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.

Omkar Yadav
  • 523
  • 1
  • 6
  • 14
  • Even with my solution timeout works but I'm unable to change default response from html to json I provied. – user2334711 Mar 14 '18 at 12:59
  • https://github.com/expressjs/timeout/blob/master/index.js#L82 If you check this code this returning the error to the middleware which is handled by the the error handler. You can write function like this `(err, res, req, next) => { req.jsonp(err) }` so it can be resolved. NOTE: Dont use this in production use your own error handler – Omkar Yadav Mar 20 '18 at 05:27
-1

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!

David R
  • 14,711
  • 7
  • 54
  • 72