Christopher Mellor
Christopher Mellor

Reputation: 444

Set request time limit in NodeJS

I am trying to reproduce a couple of errors that happen when requests fail on the server. I want to temporarily set a time limit on all requests to keep them from being able to execute successfully. Is something like this possible? I am using Node and Express.

Upvotes: 1

Views: 2499

Answers (1)

mihai
mihai

Reputation: 38533

If you initialize express using the http server, you can use server.timeout.
It sets the timeout for all incoming requests.

const app = express()
const http = require('http')
const server = http.createServer(app)
server.timeout = 10; //milliseconds
server.listen(3000)

There is also a server.setTimeout function that receives a callback. The callback gets executed for every request that times out.

server.setTimeout(20, () => {
  console.log('request timed out');
})

Upvotes: 2

Related Questions