Home » Nodejs » Node.js HTTP response streams

Node.js HTTP response streams

Posted by: admin November 29, 2017 Leave a comment


Using the native http.get() in Node.js, I’m trying to pipe a HTTP response to a stream that I can bind data and end events to.

I’m currently handling this for gzip data, using:

http.get(url, function(res) {
  if (res.headers['content-encoding'] == 'gzip') {
    gunzip.on('data', dataCallback);
    gunzip.on('end', endCallback);

Gunzip is a stream and this just works. I’ve tried to create streams (write streams, then read streams) and pipe the response, but haven’t been having much luck. Any suggestions to replicate this same deal, for non-gzipped content?


The response object from a HTTP request is an instance of readable stream. Therefore, you would collect the data with the data event, then use it when the end event fires.

var http = require('http');
var body = '';

http.get(url, function(res) {
  res.on('data', function(chunk) {
    body += chunk;
  res.on('end', function() {
    // all data has been downloaded

The readable.pipe(dest) would basically do the same thing, if body in the example above were a writable stream.