如何从node.js中的URL同步下载文件

我正在尝试使用Flickr API和http.get()调用来在Flickr中下载一些文件。

我有图像url的数组,我使用“下载”function下载图片如果有大量的图像,大多是空文件。 我在这里find了下载代码。
请咨询如何解决这个问题。
提前致谢!

 for (i=1;i<100;i++){ filename= "./images/file"+i+".jpg"; download(photourl[i], filename,{}); } //End of for-loop ..... var download = function(url, dest, cb) { var file = fs.createWriteStream(dest); var request = http.get(url, function(response) { response.pipe(file); file.on('finish', function() { file.close(); //cb(); }); }); } 

PS然后最后有错误:events.js:72扔呃; //未处理的'错误'事件^错误:套接字挂在Socket.SocketOnEnd(作为onend)(http.js:1538:23)上的create.gangUpError(http.js:1442:15)Socket.g(events.js:在process.tickCallback(node.js:415:13)处的_stream_readable.js:910:16处的Socket.EventEmitter.emit(events.js:117:20)

我build议为此使用asynchronous模块:

 var i = 1, threads = 5; require('async').eachLimit(photourl, threads, function(url, next){ download(url, "./images/file"+(i++)+".jpg", next); }, function(){ console.log('finished'); }) 

并取消注释cb(); 在下载function