Node.js下载多个文件

use*_*602 1 file download node.js

我需要从网址下载多个文件.我在文件中列出了它们.我该怎么办?我已经做到了,但它没有用.在开始下一个wan之前,我需要一直到下次完成下载.我怎样才能做到这一点?

thr*_*qon 5

您希望在此之前从文件的回调中调用下载函数.请把一些东西扔在一起,不要把它看得很漂亮也不要生产准备好,请;-)

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'); }); 
Run Code Online (Sandbox Code Playgroud)

http-get不是标准节点模块,您可以通过它安装它npm install http-get.