How to ungzip JSON response using zlib

I tried sending JSON response with the following code but it gives me an error saying

console.log(zlib.createGunzip()).pipe(data);
TypeError: Cannot call method 'pipe' of undefined

My code:

res.on('data', function (chunk) {
  data += chunk;
  console.log(data);
  console.log(res.headers['content-encoding']);
  var output;

  switch (res.headers['content-encoding']) {
    // or, just use zlib.createUnzip() to handle both cases
    case 'gzip':
      console.log(data);
      console.log(zlib.createGunzip()).pipe(data);
    break;

    case 'deflate':
      console.log(zlib.createInflate()).pipe(data);
    break;

    default:
      console.log(data);
    break;
  }
});

Any help regarding this will be very useful.

console.log() returns nothing (undefined). You're trying to call method pipe on console.log's returned value

I guess you need this console.log(zlib.createGunzip().pipe(data));