Node.js下载多个文件

我需要从url下载多个文件。 我已经在文件中列出了他们的名单。 我该怎么做? 我已经做到了,但是不起作用。 我需要保持直到最后一次下载完成,然后才开始下一个wan。 我怎样才能做到这一点?

您希望在此之前从文件的callback中调用下载function。 我扔了一些东西,不认为它漂亮,也没有生产准备,请;-)

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不是标准的节点模块,你可以通过npm install http-getnpm install http-get

您可以在节点js中使用fs(var fs = require('fs');)来读取文件

 fs.readFile('<filepath>', "utf8", function (err, data) { if (err) throw err; console.log(data); });