如何使用node.js http-proxy记录计算机中的HTTP流量? [英] How do I use node.js http-proxy for logging HTTP traffic in a computer?

查看:189
本文介绍了如何使用node.js http-proxy记录计算机中的HTTP流量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

我正在使用Windows 7 Chrome

I'm using Windows 7 Chrome

推荐答案

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

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

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

这篇关于如何使用node.js http-proxy记录计算机中的HTTP流量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆