Created
January 13, 2014 10:08
-
-
Save quard8/8397599 to your computer and use it in GitHub Desktop.
Simple nodejs client with gzip support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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