NodeJS 简单的文件下载功能实现示例

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])
上一篇:Node.js 向一个文件添加内容


下一篇:【JS 逆向百例】cnki 学术翻译 AES 加密分析