The code below aborts the request if it takes longer than 10 seconds duration.
Is it possible to set a socket based timeout on the request using http module?
Also, should I be using a socket and request timeout?
var abortRequestError;
var timeout = 10000;
var requestObject = {
host: 'xx.xx.xx.xx',
path: '/api',
port: 10000,
method: 'GET'
};
var req = http.request(requestObject, function(res) {
var responseBody = '';
res.on('data', function(data) {
responseBody += data;
});
res.on('end', function() {
console.log(abortRequestError);
});
}).on('error', function(error) {
error = abortRequestError ? abortRequestError : error;
console.log(error);
}).setTimeout(timeout, function() {
abortRequestError = new Error('Request timed out');
req.abort();
});