I'm trying to figure out a way to make this library work with gzip and deflate.
Looked at this question, tried the suggestions, but for some reason it still does not work. I'm relatively new to node.js, could somebody take a look at my code and tell me what I'm doing wrong?
var request = require("request"),
zlib = require( 'zlib' );
var req = request( {
uri: "http://google.com",
headers: {
'Accept-Encoding': 'gzip, deflate',
'user-agent': 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1',
},
},
function( error, res, body ) {
var output;
switch ( res.headers[ 'content-encoding' ] ) {
case 'gzip':
var gzip = zlib.createGunzip();
res.pipe(gzip);
output = gzip;
break;
case 'deflate':
var gzip = zlib.createInflate();
res.pipe(gzip);
output = gzip;
break;
default:
res.setEncoding( 'utf8' );
output = res;
break;
}
//console.log( body ) // outputs encoded html
output.on('data', function ( data ) {
console.log( data ); // never happens
});
output.on('end', function() {
console.log( data ); // never happens
});
});