user2316602 user2316602 - 10 days ago 14
Node.js Question

Node.js download multiple files

I need to download multiple files from urls. I have got list of them in the file. How should I do that? I already made it, but it's not working. I need to wain until last download is done before starting next wan. How can I do that?

Answer

You want to call the download function from the callback of the file before that. I threw together something, do not consider it pretty nor production ready, please ;-)

var http = require('http-get');

var files = { 'url' : 'local-location', 'repeat-this' : 'as often as you want' };

var MultiLoader = function (files, finalcb) {

        var load_next_file = function (files) {

                if (Object.keys(files) == 0) {
                        finalcb(null);
                        return;
                }   

                var nexturl = Object.keys(files)[0];
                var nextfnname = files[nexturl];

                console.log('will load ' + nexturl);
                http.get(nexturl, nextfnname, function (err, result) {
                        console.log('loaded ' + nexturl);
                        delete files[nexturl];
                        load_next_file(files);
                }); 
        };  

        load_next_file(JSON.parse(JSON.stringify(files)));

};

MultiLoader(files, function () { console.log('finalcb'); }); 

http-get is not a standard node module, you can install it via npm install http-get.

Comments