简体   繁体   中英

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:

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:

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 --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.

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:

var https = require('https');

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM