【发布时间】:2014-02-11 11:37:43
【问题描述】:
我正在尝试使用路由器表制作 NodeJS http-proxy。 我看到了一些使用 http-proxy 的示例并尝试这样:
var httpProxy = require('http-proxy');
var proxyTable = {};
proxyTable['testproxy.com/toto'] = 'google.com:80';
proxyTable['testproxy.com/tata'] = 'gmail.com:80';
var httpOptions = {
router: proxyTable
};
console.log('Proxy Server Listening on port 80');
console.log('Requests to textproxy.com/toto (on port 80) are redirected to google.com:80');
console.log('Requests to textproxy.com/tata (on port 80) are redirected to gmail.com:80');
httpProxy.createServer(httpOptions).listen(80);
仅供参考:testproxy.com 参考 127.0.0.1。
它似乎有效(它只拦截对 testproxy.com/toto 和 tata 的请求)但是当我尝试时:
curl http://testproxy.com/toto
我有一个 NodeJS 错误:
var proxyReq = (options.target.protocol === 'https:' ? https : http).reque
^
TypeError: Cannot read property 'protocol' of undefined
at Array.stream [as 3] (D:\workspace\Proxy W_S\node_modules\http-proxy\l
ib\http-proxy\passes\web-incoming.js:103:35)
at ProxyServer.<anonymous> (D:\workspace\Proxy W_S\node_modules\http-pro
xy\lib\http-proxy\index.js:83:21)
at Server.closure (D:\workspace\Proxy W_S\node_modules\http-proxy\lib\ht
tp-proxy\index.js:125:43)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23
)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:525:27)
http-proxy 模块是否已经支持路由表? 我必须做一个动态代理,知道吗?
我是 NodeJS 的新手,我被困住了。 非常感谢您的回答。
皮埃尔-吕克
【问题讨论】:
标签: node.js proxy routing router http-proxy