需要安裝request模塊
npm install request
var request = require('request');
var fs = require('fs');
/*
* url 網(wǎng)絡(luò)文件地址
* filename 文件名
* callback 回調(diào)函數(shù)
*/
function downloadFile(uri,filename,callback){
var stream = fs.createWriteStream(filename);
request(uri).pipe(stream).on('close', callback);
}
var fileUrl = 'http://www.bookbao.cc/bookdown/%D3%F9%CC%EC%C9%F1%D7%E6.rar';
var filename = 'test.rar'; //文件名
downloadFile(fileUrl,filename,function(){
console.log(filename+'下載完畢');
});