Skip to content

Instantly share code, notes, and snippets.

@makotom
Created June 2, 2013 06:31
Show Gist options
  • Save makotom/5692824 to your computer and use it in GitHub Desktop.
Save makotom/5692824 to your computer and use it in GitHub Desktop.
HTTP request wrapper for Node.js and its showcase
var HttpSimpleRequest = function(){
var http = require("http"), https = require("https"),
target = {},
getProperProtocol = function(param){
var testee = "";
if(typeof param === typeof ""){
testee = param.toString();
}else if(typeof param === typeof {} && param.proto !== undefined){
testee = param.ptoro.toString();
}
return (/^https:/).test(testee) ? https : http;
};
target.get = function(param, callback){
if(typeof callback !== typeof function(){}){
callback = function(data){
console.log(data.toString());
};
}
getProperProtocol(param).get(param, function(resp){ resp.on("data", function(data){ callback(data, resp); }); });
};
return target;
};
(function(){
var hsr = new HttpSimpleRequest();
hsr.get("http://makotom.net/");
return;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment