How do I use node.js http-proxy for logging HTTP traffic in a computer?

天涯浪子 提交于 2019-12-30 03:33:33

问题


I am trying to implement the simplest example:

var http = require('http'),
var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {
    //
    // I would add logging here
    //
    proxy.proxyRequest(req, res, { host: 'www.google.com', port: 80 });
}).listen(18000);

When I configure my browser to use this proxy and I navigate to www.google.com I receive no response. What is that I am doing wrong?

I'm using Windows 7 Chrome


回答1:


Here is an simple example how to log requests. I use a similar to log all my domains to one database.

I copied much from http://blog.nodejitsu.com/http-proxy-middlewares

var fs = require('fs'),
    http = require('http'),
    httpProxy = require('http-proxy'),

logger = function() {    
  // This will only run once
  var logFile = fs.createWriteStream('./requests.log');

  return function (request, response, next) { 
    // This will run on each request.
    logFile.write(JSON.stringify(request.headers, true, 2));
    next();
  }
}

httpProxy.createServer(
  logger(), // <-- Here is all the magic
  {
    hostnameOnly: true,
    router: {
      'example1.com': '127.0.0.1:8001', // server on localhost:8001
      'example2.com': '127.0.0.1:8002'  // server 2 on localhost:8002
  }
}).listen(8000);



回答2:


I am not sure if this helps because the posted information are really short. But I found a post that they updated the api ...

you might want to check out this post:

Updating to node-http-proxy v0.5.0 http://blog.nodejitsu.com/updating-node-http-proxy



来源:https://stackoverflow.com/questions/10985350/how-do-i-use-node-js-http-proxy-for-logging-http-traffic-in-a-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!