Skip to content

Instantly share code, notes, and snippets.

@tony1223
Created August 5, 2012 23:24
Show Gist options
  • Select an option

  • Save tony1223/3267929 to your computer and use it in GitHub Desktop.

Select an option

Save tony1223/3267929 to your computer and use it in GitHub Desktop.
var http = require('http'),
Iconv = require('iconv').Iconv;
function wget (host, path, callback,encoding /* optional */) {
var iconv = null;
if (encoding){
iconv = new Iconv(encoding,'UTF-8//TRANSLIT//IGNORE');
}
var options = {
host: host,
port: 80,
path: path,
method: 'GET',
};
var req = http.request(options, function(res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
var result = [];
res.on('data', function (chunk) {
if (iconv){
result.push(iconv.convert(new Buffer(chunk)));
}else{
result.push(chunk);
}
});
res.on('end', function (){
callback(result.join(""));
});
});
req.setTimeout(2000);
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment