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])

你可能感兴趣的:(NodeJS 简单的文件下载功能实现示例)