Created
January 13, 2014 20:08
-
-
Save lxe/8407112 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // mr.js -- very small request maker | |
| var http = require('http') | |
| , https = require('https') | |
| , url = require('url') | |
| var mr = module.exports = function mr (uri, callback) { | |
| uri = typeof(uri) === 'string' ? url.parse(uri, true) : uri; | |
| uri.method = uri.method || 'GET'; | |
| uri.headers = { host : uri.host, accept : 'text/html' }; | |
| var req = (uri.protocol === 'http:' ? http : https).request(uri, function(res) { | |
| var chunks = []; | |
| res.on('data', [].push.bind(chunks)); | |
| res.on('end', function () { callback(null, Buffer.concat(chunks)); }); | |
| res.on('error', callback); | |
| }); | |
| uri.body && req.write(uri.body); | |
| req.end(); | |
| }; | |
| mr('http://www.google.com/', function(err, body) { | |
| console.log(err, body.toString()); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment