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

新手上路,请多包涵

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

 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);

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

我正在使用 Windows 7 Chrome

原文由 gztomas 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 283
2 个回答

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

我从 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);

原文由 davl 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题