0

I'm looking at this code snippet from this answer on creating a reverse-proxy nodejs server:

var http = require('http');

http.createServer(onRequest).listen(3000);

function onRequest(client_req, client_res) {
  console.log('serve: ' + client_req.url);

  var options = { ... };

  var proxy = http.request(options, function (res) {
    client_res.writeHead(res.statusCode, res.headers)
    res.pipe(client_res, {
      end: true                 <--------------- why here?
    });
  });

  client_req.pipe(proxy, {
    end: true                 <--------------- why here?
  });
}

I don't quite understand why we need to pass end:true to pipe?

Here it says:

By default, stream.end() is called on the destination Writable stream when the source Readable stream emits 'end', so that the destination is no longer writable.

So my understanding is that when the end event is called on the original request it will be called on the proxied request as well. What am I missing?

Max Koretskyi
  • 85,840
  • 48
  • 270
  • 414

0 Answers0