如何使用node.js http-proxy记录计算机中的HTTP流量?

gzt*_*mas 3 javascript http http-proxy node.js node-http-proxy

我正在尝试实现最简单的示例:

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);
Run Code Online (Sandbox Code Playgroud)

当我将浏览器配置为使用此代理并导航到www.google.com时,我没有收到任何回复.我做错了什么?

我正在使用Windows 7 Chrome

dav*_*avl 5

这是一个如何记录请求的简单示例.我使用类似的方法将我的所有域记录到一个数据库.

我从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);
Run Code Online (Sandbox Code Playgroud)