http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.89k stars 1.97k forks source link

How to dump/clone http response body use Node.js coding style? #936

Open chenzx opened 8 years ago

chenzx commented 8 years ago

When i first wanted to build a local http proxy server, and cache all the images data to local disk, i searched for all the available proxy server scripts, since i like JavaScript, i preferred and tried node-http-proxy lib at first, but i found a problem:

In Node, stream objects can easily "pipe", since it's builtin method.

But i cannot easily clone/dump a stream, there is no such method. (Can i dispatch a single stream to multiple output channels?)

So i use python's httproxy, httproxy is too naive but it works after some modification.

Finally i switched to goproxy, which is really feature-complete and API-friendly.

JohnWong commented 8 years ago
proxy.on('proxyRes', function(proxyReq, req, res, options) {
  var oldWrite = res.write,
    oldEnd = res.end;

  var chunks = [];

  res.write = function (chunk) {
    chunks.push(chunk);
    oldWrite.apply(res, arguments);
  };

  res.end = function (chunk) {
    if (chunk)
      chunks.push(chunk);

    // request body
    var body = Buffer.concat(chunks);
    oldEnd.apply(res, arguments);
  };
});
chenzx commented 8 years ago

I haven't thought of overriding default methods, thanks!!!

2016-01-23 15:50 GMT+08:00 John Wong notifications@github.com:

proxy.on('proxyRes', function(proxyReq, req, res, options) { var oldWrite = res.write, oldEnd = res.end;

var chunks = [];

res.write = function (chunk) { chunks.push(chunk); oldWrite.apply(res, arguments); };

res.end = function (chunk) { if (chunk) chunks.push(chunk);

// request body
var body = Buffer.concat(chunks);
oldEnd.apply(res, arguments);

}; });

— Reply to this email directly or view it on GitHub https://github.com/nodejitsu/node-http-proxy/issues/936#issuecomment-174157553 .

DonMartin76 commented 6 years ago

This approach worked fine for us as well. Thanks for sharing.