【问题标题】:How download file from github.com whith NodeJs (https)如何使用 NodeJs (https) 从 github.com 下载文件
【发布时间】:2015-10-15 10:02:21
【问题描述】:

我正在尝试从 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);
});

此代码不会从 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
};

脚本下载文件没有错误,我用 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

并且下载没有错误。

我该如何解决这个问题?

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

【问题讨论】:

    标签: node.js github https download


    【解决方案1】:

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

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

    你可以试试

    rejectUnauthorized : true,
    requestCert        : false
    

    【讨论】:

      【解决方案2】:

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

      var https = require('https');
      

      var https = require('follow-redirects').https;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 2020-11-08
        • 2015-10-18
        • 1970-01-01
        • 2017-08-27
        相关资源
        最近更新 更多