Jide
Jide

Reputation: 1130

How to use transform stream using request?

Basically, I would like to alter the http response before sending it to the client, using transform streams, but my code below throws an error : [Error: write after end].

Documentation on http://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback says :

Calling write() after calling end() will raise an error.

How can I prevent write() to be called after end() in this case ?

var request = require('request');
var Transform = require('stream').Transform;
var http = require('http');

var parser = new Transform();
parser._transform = function(data, encoding, done) {
  console.log(data);
  this.push(data);
  done();
};

parser.on('error', function(error) {
  console.log(error);
});

http.createServer(function (req, resp) {
  var dest = 'http://stackoverflow.com/';
  var x = request({url:dest, encoding:null})

  x.pipe(parser).pipe(resp)
}).listen(8000);

Upvotes: 6

Views: 5097

Answers (1)

Paul Mougel
Paul Mougel

Reputation: 17048

A stream is supposed to be used only once, but you're using the same transform stream for each incoming request. On the first request it will work, but when x closes, so will parser: that's why on the second client request you'll see the write after end error.

To fix this, just create a new transform stream on each use:

function createParser () {
    var parser = new Transform();
    parser._transform = function(data, encoding, done) {
        console.log(data);
        this.push(data);
        done();
    };
    return parser;
}

http.createServer(function (req, resp) {
  var dest = 'http://stackoverflow.com/';
  var x = request({url:dest, encoding:null})

  x.pipe(createParser()).pipe(resp)
}).listen(8000);

Upvotes: 13

Related Questions