Created
May 20, 2013 13:45
-
-
Save bl4de/5612328 to your computer and use it in GitHub Desktop.
A little work with Node.js and GitHub API - I'd like to implement some of similar functionality in my new SimpleVirtualAssistant project. Script checks repos of username, passed by url query string param 'username': http://127.0.0.1:2323/?username=bl4de
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var https = require("https"); | |
var url = require("url"); | |
var port = "2323"; | |
var host = "127.0.0.1"; | |
var server = http.createServer( function(request,response) { | |
var _url = url.parse(request.url, true); | |
// console.log(_url); | |
var username = _url.query.username; | |
console.log(username); | |
// var username = "bl4de"; | |
var body = ''; | |
var repos = []; | |
var options = { | |
host : 'api.github.com', | |
path : '/users/' + username + '/repos', | |
method: 'GET', | |
headers: { | |
'User-agent': 'Node.js' | |
}, | |
type: 'owner', | |
sort: 'updated', | |
direction: 'asc' | |
}; | |
var g = https.request( options, function(gitresp) { | |
gitresp.on('data', function(chunk) { | |
console.log("chunk received"); | |
body += chunk; | |
}); | |
gitresp.on('end', function() { | |
console.log("gitresp end"); | |
var json = JSON.parse(body); | |
json.forEach( function(repo) { | |
// console.log(repo); | |
repos.push({ | |
name: repo.name, | |
description: repo.description, | |
html: '<li>name: ' + repo.name + ' description: ' + | |
repo.description + '</li>' | |
}); | |
}); | |
response.writeHead(200, { | |
"Content-type" : "text/html" | |
}); | |
response.write('<div><h2>Repos of ' + username + | |
' (' + repos.length + ')</h2></div><ul>'); | |
repos.forEach( function(repo){ | |
response.write(repo.html); | |
}); | |
response.end("</ul>"); | |
}); | |
}); | |
g.end(); | |
}); | |
server.listen(port, host, function() { | |
console.log("Server started on " + host + ":" + port); | |
}); | |
// getRepos(username, function(repos) { | |
// console.log("Repositories: ", repos); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment