user2316602 user2316602 - 8 months ago 102
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 Source

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) {

                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];



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

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