Created
September 10, 2013 15:46
-
-
Save andygup/6511364 to your computer and use it in GitHub Desktop.
Full node http get solution
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"); | |
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