Including timeout in Node.js http.get while getting large number of image downloads

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?

Answers


OK, there are at least two solutions to your problem. An easy one:

var request = http.get(options, function (res) {
    // other code goes here
});
request.setTimeout( 10000, function( ) {
    // handle timeout here
});

but might not be flexible enough. The more advanced one:

var timeout_wrapper = function( req ) {
    return function( ) {
        // do some logging, cleaning, etc. depending on req
        req.abort( );
    };
};

var request = http.get(options, function (res) {
    res.on('data', function (data) {
        file.write(data);
        // reset timeout
        clearTimeout( timeout );
        timeout = setTimeout( fn, 10000 );
    }).on('end', function () {
        // clear timeout
        clearTimeout( timeout );
        file.end();
        console.log(file_name + ' downloaded ');
        cb(null, file.path);
    }).on('error', function (err) {
        // clear timeout
        clearTimeout( timeout );
        console.log("Got error: " + err.message);
        cb(err, null);
    });
});

// generate timeout handler
var fn = timeout_wrapper( request );

// set initial timeout
var timeout = setTimeout( fn, 10000 );

Need Your Help

CSS translate not working in IE11 on SVG g

html css css3 internet-explorer svg

I want to move a group of svg elements using a G tag, but it isn't working in IE, even the latest version. It works in all other browsers. Please help.

Get url of current path in Terminal SSH

git ssh terminal

I am having trouble using git on my own server. I am having trouble where I add the origin path (remote add) as I am entering the wrong url. By finding out the correct path to the .git repository o...