NodeJS 简单的文件下载功能实现示例
2018-08-05 本文已影响56人
关爱单身狗成长协会
simple.js
//node-fetch https://github.com/bitinn/node-fetch
var fetch = require("node-fetch");
var fs = require("fs");
function download(u, p) {
return fetch(u, {
method: 'GET',
headers: { 'Content-Type': 'application/octet-stream' },
}).then(res => res.buffer()).then(_ => {
fs.writeFile(p, _, "binary", function (err) {
console.log(err || p);
});
});
}
////////=======
var url = "https://nodejs.org/dist/v8.9.4/node-v8.9.4-win-x64.zip";
download(url, url.split("/").reverse()[0])