sac Dahal sac Dahal - 2 months ago 19
Javascript Question

Accessing github repository using nodejs

I wanted to access github repositories using github api in node.`

var https = require("https");
var userName='sacdh22';
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)'}
}

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 =[];
console.log(json);
//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();


When i console.log(body) I get the following output.

{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}
{ message: 'Not Found',
documentation_url: 'https://developer.github.com/v3' }


Can any one tell me what is going wrong here, my guess is that the path is not set properly, so how can I set it?

Answer

sacdh22 is not a username on Github. When you try to access a list of their repositories, Github gives you a Not Found error. Change the username to a real one and you will get very different results.

Comments