Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Created July 10, 2012 07:03
Show Gist options
  • Save hagino3000/3081722 to your computer and use it in GitHub Desktop.
Save hagino3000/3081722 to your computer and use it in GitHub Desktop.
Simple URL fetch for node
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