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

标签 javascript http node.js http-proxy 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);

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

我正在使用 Windows 7 Chrome

最佳答案

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

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

关于javascript - 如何使用 node.js http-proxy 在计算机中记录 HTTP 流量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10985350/

相关文章:

javascript - XPages 进度条在服务器页面持久性设置 "keep pages in memory"下无法按预期工作

javascript - 如何隐藏iframe src

java - 有没有办法在终端中执行包含单个单词(或短语)的 jar 文件?

http - 需要帮助解决 Istio IngressGateway HTTP 错误 503

node.js - 需要多个nodejs api文件

javascript - 找不到Nodeunit命令?

javascript - CSS、Internet Explorer 7 : layout messed up when I click on a menu

Javascript 替换不影响所有数组元素

python - 在python中挂起http POST请求应该怎么做?

node.js - 使用nodemailer接收邮件而不设置 "Allow less secure apps to access"