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

时间:2015-07-24 16:28:01

标签: node.js github https downloading

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

[HttpPost]
public IHttpActionResult Send(SendMessage msg)
{

}

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

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);
});

脚本没有错误地下载文件,我用wget:

测试从Github下载
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
};

下载没有错误。

我该如何解决这个问题?

ps。:对不起我糟糕的英语。

2 个答案:

答案 0 :(得分:0)

在您的选项中,您使用以下

rejectUnauthorized : false, //this says reject unauthorised user
requestCert        : true, //this says request a certificate. You don't provide one.

你能试试吗

rejectUnauthorized : true,
requestCert        : false

答案 1 :(得分:0)

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

var https = require('https');

var https = require('follow-redirects').https;
相关问题