如何使用 NodeJs (https) 从 github.com 下载文件

Dan*_*ela 1 https github download node.js

我尝试从 Github 下载 7zip 文件,但没有成功,代码如下:

var fs    = require('fs');
var https = require('https');

options = { 
    host               : "github.com", 
    port               : 443,
    path               : "/msysgit/msysgit/releases/download/Git-1.9.5-preview20150319/PortableGit-1.9.5-preview20150319.7z",
    method             : 'GET',
    rejectUnauthorized : false,
    requestCert        : true,
    agent              : false
};

var file = fs.createWriteStream("installer.7z");

var request = https.get(options, function(response){
    response.pipe(file);


    file.on("finish", function(){
        file.close();
    });
});

request.end();

request.on('error', function(err){
    throw (err);
});
Run Code Online (Sandbox Code Playgroud)

此代码不会从 Github 下载文件,但如果我更改下载 Notepad++ 的选项:

options = { 
    host               : "notepad-plus-plus.org", 
    port               : 443,
    path               : "/repository/6.x/6.8/npp.6.8.bin.7z",
    method             : 'GET',
    rejectUnauthorized : false,
    requestCert        : true,
    agent              : false
};
Run Code Online (Sandbox Code Playgroud)

该脚本下载文件没有错误,我测试使用 wget 从 Github 下载:

wget --no-check-certificate --output-document="installer.7z" https://github.com/msysgit/msysgit/releases/download/Git-1.9.5-preview20150319/PortableGit-1.9.5-preview20150319.7z
Run Code Online (Sandbox Code Playgroud)

并且下载没有错误。

我该如何解决这个问题?

PS:我很抱歉我的英语很糟糕。

Dan*_*ela 5

我找到了一个解决方案:出现此问题是因为 URL 有重定向,一个简单的解决方案是安装一个名为follow-redirects 的模块并更改:

var https = require('https');
Run Code Online (Sandbox Code Playgroud)

var https = require('follow-redirects').https;
Run Code Online (Sandbox Code Playgroud)