This is the code which I have been using to download images from URLs:
http.get(options, function (res) {
res.on('data', function (data) {
file.write(data);
}).on('end', function () {
file.end();
console.log(file_name + ' downloaded ');
cb(null, file.path);
}).on('error', function (err) {
console.log("Got error: " + err.message);
cb(err, null);
});
});
How can I add a timeout for every request so that it doesn't stay on waiting for a response which is either large data or unresponsive?
OK, there are at least two solutions to your problem. An easy one:
but might not be flexible enough. The more advanced one: