简体   繁体   English

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

[英]How download file from github.com whith NodeJs (https)

I'm trying to download a 7zip file from Github without success, with the following code: 我正在尝试从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);
});

this code don't download the file from Github, but if I change options to download Notepad++ with: 此代码不会从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
};

The script downloads the file without error, I test downloading from Github with wget: 脚本下载文件没有错误,我测试了如何使用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

And the download occurs without errors. 并且下载发生没有错误。

how can I solve this issue? 我该如何解决这个问题?

ps.: I'm sorry for my terrible english. ps:对不起,我的英语不好。

In your options, you use the following 在选项中,使用以下内容

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

Can you try 你能试一下吗

rejectUnauthorized : true,
requestCert        : false

I found a solution: this issue occurs because the URL has a redirect, a simple solution is install a module called follow-redirects and change: 我找到了一个解决方案:发生此问题是因为URL具有重定向,一个简单的解决方案是安装一个名为follow-redirects的模块并进行更改:

var https = require('https');

to

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法下载“https://github.com/OfficeDev/Office-Addin-TaskPane-JS/archive/yo-office.zip”的项目 zip 文件 - Unable to download project zip file for "https://github.com/OfficeDev/Office-Addin-TaskPane-JS/archive/yo-office.zip" node.js中的核心模块如何工作? (https://github.com/nodejs/node/blob/master/lib) - How do the core modules in node.js work? (https://github.com/nodejs/node/blob/master/lib) I have installed a node based API from https://github.com/opentok/interactive-broadcast-api, but how to enable HTTPS on this API? - I have installed a node based API from https://github.com/opentok/interactive-broadcast-api, but how to enable HTTPS on this API? 按照https://github.com/liferay/alloy-ui的指示进行npm install错误 - npm install error while following instructions from https://github.com/liferay/alloy-ui 如何在浏览器中使用“https://github.com/ovvn/dom-to-pdf” - How can I use `https://github.com/ovvn/dom-to-pdf` in a Browser 如何处理“已弃用,请使用https://github.com/pillarjs/path-to-regexp”消息 - how to deal with “DEPRECATED use https://github.com/pillarjs/path-to-regexp” message 从 nodejs 中的私有 github 存储库下载文件 - download a file from private github repository in nodejs Azure Pipelines:无法下载“https://github.com/sass/node-sass/releases/download/v4.13.0/win32-x64-83_binding.node” - Azure Pipelines: Cannot download "https://github.com/sass/node-sass/releases/download/v4.13.0/win32-x64-83_binding.node" 在本地运行 https://github.com/akella/webgl-mouseover-effects 代码 - Run https://github.com/akella/webgl-mouseover-effects code locally appmetrics(https://github.com/RuntimeTools/appmetrics/issues)请求和http事件不起作用 - appmetrics(https://github.com/RuntimeTools/appmetrics/issues) request and http event not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM