Wednesday, January 18, 2012

node.js GitHub API search repositories by keyword example

Not long time ago I started learning JavaScript and node.js. Today I found this node.js module called node-github. I had fun while played with it. And here it goes one little sniped for searching GitHub source repositories by keywords.

var GitHubApi = require('github').GitHubApi;

var github = new GitHubApi(true);
var repoApi = github.getRepoApi();

repoApi.search('node github', function(err, repos){
    if (repos){
        var i;
        for(i = 0; i < repos.length; i++){
            console.log(repos[i].name);
        }
    } else {
        console.log('Not found any repos!\n');
    }
});
How to run
  • download and install node.js from here
  • run 'npm install github' in your source directory
  • create new file 'example.js' and paste code sniped into it and save changes
  • run 'node example.js'
Output you should see the following output to console:
C:\code\node\npm-index>node scripts\example_github.js
The "sys" module is now called "util". It should have a similar interface.
get:  post
send GET request: /api/v2/json/repos/search/node%20github
node-github
node-buildbot-github
node-bus.github.com
node-github-receiver
node-uso-github
node-github-issues
node.github.com
github-node-tools
NodeGithubResumeWriter
g-node.github.com
node-github-deployhook