Skip to content

Instantly share code, notes, and snippets.

@andygup
Created September 10, 2013 15:46
Show Gist options
  • Save andygup/6511364 to your computer and use it in GitHub Desktop.
Save andygup/6511364 to your computer and use it in GitHub Desktop.
Full node http get solution
var http = require("http");
Retrieve = function(){
console.log("retrieve");
/**
* Executes a GET request on the given url and returns a JSON object.
* Default timeout is 20 seconds.
* @param url
* @param callback
*/
this.getFeed = function(url,callback){
var result = null;
var timeout = 20000;
http.get(url,function(response){
//console.log("response: " + response.statusCode);
response.setEncoding('utf8');
response.setTimeout(timeout,function(err){
console.log("request timed out");
callback(null,"Request timed out");
})
response.on('data',function(data){
result = result + data;
})
response.on('end',function(){
callback(null,result);
})
});
}
}
exports.Retrieve = Retrieve;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment