Get a response from the proxy server

I created a proxy server in node.js using the node -http-proxy module.

It looks like:

var http = require('http'),
httpProxy = require('http-proxy'),
io = require("socket.io").listen(5555);

var proxy = new httpProxy.RoutingProxy();

http.createServer(function (req, res) {
     proxy.proxyRequest(req, res, {
         host: 'localhost',
         port: 1338
     });
}).listen(9000);

So, I need before sending a response back to the client in order to get the body from the server so that I proxy and analyze it.

But I can not find an event or attribute where I can get this data. I tried:

proxy.on('end', function (data) {
    console.log('end');
});

But I can’t understand how to get the body of a mime from it.

+5
source share
2 answers

If all you want to do is check the answer (read-only), you can use:

proxy.on('proxyRes', function(proxyRes, req, res){

    proxyRes.on('data' , function(dataBuffer){
        var data = dataBuffer.toString('utf8');
        console.log("This is the data from target server : "+ data);
    }); 

});

But note that the "proxyRes" event is raised after the response is sent.

Link to: https://github.com/nodejitsu/node-http-proxy/issues/382

+5
source

: -

var http = require('http'),
    httpProxy = require('http-proxy'),
    io = require("socket.io").listen(5555);

var proxy = new httpProxy.RoutingProxy();

http.createServer(function (req, res) {
    console.log(req.url);
    res.oldWrite = res.write;
    res.write = function(data) {
        /* add logic for your data here */
        console.log(data.toString('UTF8'));
        res.oldWrite(data);
    }

    proxy.proxyRequest(req, res, {
        host: 'localhost',
        port: 1338
    });

}).listen(9000);

.

+4

All Articles