Measuring traffic with node.js http-proxy

I wrote a small reverse proxy for hosting my applications on the same computer using http and node-http-proxy modules. For example:

I have:

  • proxy running on port 80

  • website1.com running on port 3000

  • website2.com running on port 3001

  • website3.com running on port 3002

If I access the website1.com domain, the proxy will serve the contents from server running on port 3000 using node-http-proxy.

But now I need to measure the bandwidth used for each domain (both incoming/outgoing, or at least outgoing)

I've tried listening for 'data' events on request object, but in documentation they said that readable events isn't emitted on IncomignMessage for some reason.

I wrote a little module for the "base" functionality too, it can be found here:

https://npmjs.org/package/reproxy

See example/example.js

So, how can I accomplish this measure, using the current setup?

Answers:

Answer

The solution I found was setting and 'end' event on RoutingProxy object and grabbing the socket information in the event callback.

var proxy = new require('http-proxy').RoutingProxy();

proxy.on('end', function(req, res, response) {

    var host = req.headers.host;
    var bytesIn = response.socket._bytesDispatched;
    var bytesOut = response.socket.bytesRead;

    console.log('request to ' + host);
    console.log('request:  ' + bytesIn + ' bytes.');
    console.log('response: ' + bytesOut + ' bytes.');

});

Note that this is not optimal solution, because the request size includes the headers added by the reverse proxy, such as "x-" headers.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.