Skip to content

Instantly share code, notes, and snippets.

@lxe
Created January 13, 2014 20:08
Show Gist options
  • Select an option

  • Save lxe/8407112 to your computer and use it in GitHub Desktop.

Select an option

Save lxe/8407112 to your computer and use it in GitHub Desktop.
// 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