diff --git a/lib/codeplex.js b/lib/codeplex.js new file mode 100644 index 0000000..70c3ab7 --- /dev/null +++ b/lib/codeplex.js @@ -0,0 +1,35 @@ +var https = require('https'); + +module.exports = { + getRepos: getRepos +} + +function getRepos(user, callback) { + + var options = { + hostname: 'www.codeplex.com', + path: '/api/users/' + user + '/projects', + method: 'GET', + headers: {'x-ms-version': '2012-09-01'} + }; + + console.log("Fetching repos from codeplex for user: %s", user); + var req = https.request(options, function(res) { + var json = ''; + res.on('data', function(d) { + json += d; + }); + res.on('error', function(err) { + callback(err, null); + }); + res.on('end', function() { + var repos = JSON.parse(json); + callback(null, repos); + }); + }); + + req.on('error', function(err) { + callback(err, null); + }); + req.end(); +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 2d5aef5..621099e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,37 +1,37 @@ -var https = require('https'); +var codeplex = require('./codeplex'); var http = require('http'); - +var url = require('url'); +var querystring = require('querystring'); http.createServer(function(request, response) { - console.log("Request handler 'codeplex' was called."); - - var options = { - hostname: 'www.codeplex.com', - path: '/api/users/aheidebrecht/projects', - method: 'GET' - }; - - var req = https.request(options, function(res) { - var json = ''; - res.on('data', function(d) { - json += d; - }); - - res.on('end', function() { - var str = JSON.stringify(JSON.parse(json), null, " "); - response.writeHead(200, { 'Content-Type': 'text/plain' }); - response.write(str); - response.end(); - }); - }); - - req.end(); - - req.on('error', function(err) { - console.error(err); - - response.writeHead(500, { 'Content-Type': 'text/plain' }); - response.write(err); - response.end(); - }); + + var parsed = url.parse(request.url); + var pathname = parsed.pathname; + if (pathname === '/') { + response.writeHead(200, { 'Content-Type': 'text/html' }); + response.write('codeplex api test'+ + '
'); + response.end(); + } else if (pathname === '/get-repos') { + var username = querystring.parse(parsed.query)["username"]; + + codeplex.getRepos(username, function(err, data) { + if (err) { + console.error(err); + + response.writeHead(500, { 'Content-Type': 'text/plain' }); + response.write(err); + response.end(); + } else { + var str = JSON.stringify(data, null, " "); + response.writeHead(200, { 'Content-Type': 'text/plain' }); + response.write(str); + response.end(); + } + }); + } else { + response.writeHead(200, { 'Content-Type': 'text/plain' }); + response.write(pathname); + response.end(); + } }).listen(999); \ No newline at end of file