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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With