Skip to content

Instantly share code, notes, and snippets.

@quard8
Created January 13, 2014 10:08
Show Gist options
  • Select an option

  • Save quard8/8397599 to your computer and use it in GitHub Desktop.

Select an option

Save quard8/8397599 to your computer and use it in GitHub Desktop.
Simple nodejs client with gzip support
#!/usr/local/bin/node
var HOST = 'http://google.com',
http = require('http'),
zlib = require('zlib');
var request = function(path, callback) {
var buffer = [];
http.get(HOST + path, function(res) {
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())
}).on('end', function() {
// response and decompression complete, join the buffer and return
callback(buffer.join(''));
}).on('error', function(err) {
callback(err);
})
}).on('error', function(err) {
console.log(err);
});
};
request('/', function(data) {
//working with plain data
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment