【发布时间】:2017-05-10 07:50:14
【问题描述】:
我正在尝试设置一个代理服务器,它应该从我正在访问的任何页面返回 http requests。
基本上,如果我导航到www.google.com,那么我预计会收到以下请求:
这可以使用node-http-proxy 模块实现吗?
我尝试了以下代码,但无法弄清楚如何获取请求..
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {
//
// Put your custom server logic here
//
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
}).listen(8000);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
更新:
我将你的浏览器配置为使用我的代理服务器,并将代码更改如下:
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createServer(function (req, res, proxy) {
//
// Put your custom server logic here
//
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
})
proxy.listen(8000);
proxy.on('proxyReq', function(proxyReq, req, res, options) {
console.log(req.url);
console.log(proxyReq.url);
});
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
但是当我访问不同的网站时,控制台中没有日志
【问题讨论】:
标签: javascript node.js proxy node-http-proxy