[Express.js] Timeout Configuration Changes and Sample Code for Testing

Tadashi Shigeoka ·  Sat, November 2, 2019

I’ll introduce Express.js default timeout and how to change timeout settings with sample code.

Expressjs

Background: What's Express Default Timeout in ms?

I wondered “What’s Express default timeout in ms again?” so I checked the Node.js official documentation.

I also wrote sample code to test Express timeout behavior, which I’ll describe below.

Sample Code with Modified Express Timeout

[Sample codes] server.timeout, request.setTimeout(timeout[, callback]), response.setTimeout(msecs[, callback]) · Pull Request #4 · codenote-net/expressjs-sandbox

Express Default Timeout Testing

Timeout in milliseconds. Default: 120000 (2 minutes).

Quote from: server.timeout - HTTP | Node.js v12.13.0 Documentation

Execution result default timeout

$ npm run start

> [email protected] start /path/to/expressjs-sandbox
> node ./bin/www

1000 ms
2000 ms
3000 ms
.
.
.
118000 ms
119000 ms
GET /timeout/default - - ms - -
120000 ms

server.timeout Testing

Execution result server.timeout

$ npm run start

> [email protected] start /path/to/expressjs-sandbox
> node ./bin/www

server.timeout: (default) 120000 ms
server.timeout: (custom) 5000 ms
1000 ms
2000 ms
3000 ms
4000 ms
GET /timeout/default - - ms - -
5000 ms
6000 ms
7000 ms
.
.
.

request.setTimeout Testing

Execution result request.setTimeout

$ npm run start

> [email protected] start /path/to/expressjs-sandbox
> node ./bin/www

100 ms
200 ms
300 ms
400 ms
500 ms
600 ms
700 ms
800 ms
900 ms
GET /timeout/req_setTimeout - - ms - -
1000 ms

response.setTimeout Testing

$ npm run start

> [email protected] start /path/to/expressjs-sandbox
> node ./bin/www

100 ms
200 ms
300 ms
400 ms
500 ms
600 ms
700 ms
800 ms
900 ms
1000 ms
1100 ms
1200 ms
1300 ms
1400 ms
1500 ms
1600 ms
1700 ms
1800 ms
1900 ms
GET /timeout/res_setTimeout - - ms - -
2000 ms

That’s all from the Gemba.

Reference Information