Created
June 2, 2013 06:31
-
-
Save makotom/5692824 to your computer and use it in GitHub Desktop.
HTTP request wrapper for Node.js and its showcase
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 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