node.js - Getting incomplete response from GitHub API -


i'm trying learn nodejs , have page supposed print out of repos has @ github. right now, randomly stops halfway through message, if try parse json 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 , dies.       res.end(chunk);     });   }); }).listen(1337, '127.0.0.1'); console.log('server running @ http://127.0.0.1:1337/'); 

edit: here's sample response. see how literally stops in middle of 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 response object of https.get() can called multiple times, you're ending http server's response after first.

you should collect chunks , combine them in end handler:

var gitapi = https.get(options, function(git) {   var chunks = [];    git.on("data", function(chunk) {     chunks.push(chunk);   });    git.on("end", function() {     var json = buffer.concat(chunks);     res.end(json);   }); }); 

fwiw, if gh api data json, setting text/plain content type doesn't make lot of sense.

if want proxy gh responses, can use pipe() (which same above, more efficiently):

var gitapi = https.get(options, function(git) {   git.pipe(res); }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -