- Version: 7.4.0
- Platform: MacOS 10.11.6
- Subsystem: http
Since node.js 6.8.0, http.request supports timeout option. (api-doc)
I test this feature with below code:
'use strict';
var http = require('http');
const server = http.createServer((req, res) => {
console.log(Date.now(), 'Server get incoming request');
setTimeout(() => { // delay 10 seconds
console.log(Date.now(), 'Server send response');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello');
res.end();
}, 10000);
});
server.listen(8000);
const req = http.get({
hostname: '127.0.0.1',
port: 8000,
path: '/',
timeout: 100 // timeout in 0.1 second
}, function (response) {
// not expected
console.log(Date.now(), 'Get response from server');
process.exit();
});
req.on('error', (err) => {
console.log(Date.now(), 'Error', err);
process.exit();
});
Output I got:
1490270444601 'Server get incoming request'
1490270454604 'Server send response'
1490270454608 'Get response from server'
The response was delayed 10 second to send and I expected a ETIMEOUT on http client. But it didn't.
What's more, I read the file change in #8101 which is PR adding timeout to http.request, the test case seems not testing http.request's timeout option.