Skip to content

Instantly share code, notes, and snippets.

@khadorkin
Forked from hagino3000/urlfetch.js
Created May 27, 2014 10:50
Show Gist options
  • Save khadorkin/2408b47f350e75241eda to your computer and use it in GitHub Desktop.
Save khadorkin/2408b47f350e75241eda to your computer and use it in GitHub Desktop.
module.exports = function urlFetch(urlString, callback) {
var url = require('url').parse(urlString);
var module = url.protocol === 'https:' ? require('https') : require('http');
require('http').get({
host: url.hostname,
port: url.port,
path: url.path
}, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
if (res.statusCode === 200) {
callback(null, body);
} else {
console.error('HTTP POST to ' + urlString + ' faled ' + res.statusCode);
}
});
}).on('error', function(e) {
console.log('errror');
console.log(e);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment