我正在尝试实现最简单的示例:
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/