I'm trying to learn nodejs and have this page which is supposed to print out all of the repos that someone has at GitHub. Right now, it randomly stops halfway through the message, so if I try to parse it into JSON it fails.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var https = require('https');
var options = {
host: 'api.github.com',
headers: {'user-agent': 'GitHubAPI'},
path: '/users/vermiculus/repos'
};
var gitapi = https.get(options, function(git) {
git.on("data", function(chunk) {
//JSON.parse(chunk); //DEBUG Fails and dies.
res.end(chunk);
});
});
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Edit: Here's a sample response. See how it literally stops in the middle of a string.
[{"id":24659454,"name":"arara","full_name":"vermiculus/arara","owner":{"login":"vermiculus","id":2082195,"avatar_url":"https://avatars.githubusercontent.com/u/2082195?v=3","gravatar_id":"","url":"https://api.github.com/users/vermiculus","html_url":"https://github.c
The
data
event for the response object ofhttps.get()
can get called multiple times, but you're ending your HTTP server's response after the first.You should collect all the chunks and combine them in an
end
handler:FWIW, if the GH API data is JSON, setting
text/plain
as content type doesn't make a lot of sense.If you just want to proxy the GH responses, you can also use
pipe()
(which basically does the same as above, just more efficiently):