简体   繁体   中英

How to connect to gitHub api using nodejs from a secure network?

I am using the below code to get the repositories from gitHub. When I use it from Home network I am able to retrieve the list of repositories, but If I try to fetch the repo from other network it gives me this error: 'connect ECONNREFUSED'. I am new to nodejs, so still wondering how to go about solving this issue.

Any ideas?

var https = require("https");
var userName='xyz';
var options = {
host :"api.github.com",
path : '/users/'+userName+'/repos',
method : 'GET'
}

var request = https.request(options, function(response){
var body = '';
response.on('data',function(chunk){
    body+=chunk;
});
response.on('end',function(){
    var json = JSON.parse(body);
    var repos =[];
    json.forEach(function(repo){
        repos.push({
            name : repo.name,
            description : repo.description
        });
    });
    console.log('the repos are  '+ JSON.stringify(repos));
});

});
request.on('error', function(e) {
console.error('and the error is '+e);
});
request.end();

Change your options as

var options = {

host:'api.github.com',

path: '/users/' + username+ '/repos',

method: 'GET',

headers: {'user-agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'}

};

i think this would be OK

var options = {

host:'api.github.com',

path: '/users/' + username+ '/repos',

method: 'GET',

headers: {'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'}

};

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