简体   繁体   English

NodeJS代理路由器表

[英]NodeJS Proxy Router Table

I'm trying to make a NodeJS http-proxy with a Router Table. 我正在尝试使用路由器表制作NodeJS http-proxy。 I saw some examples using http-proxy and try like this : 我看到了一些使用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);

FYI : testproxy.com refer to 127.0.0.1. 仅供参考:testproxy.com是指127.0.0.1。

It seems to work (it only intercepts request to testproxy.com/toto and tata) but when I try : 它似乎有效(它仅拦截对testproxy.com/toto和tata的请求),但是当我尝试时:

curl http://testproxy.com/toto

I have a NodeJS error : 我有一个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)

Is router table already supported by http-proxy module ? http-proxy模块已经支持路由器表吗? I have to do a dynamic proxy, any idea ? 我必须做一个动态代理,知道吗?

I'm new at NodeJS, and I'm stuck. 我是NodeJS的新手,但我陷入困境。 Thanks a lot for your answers. 非常感谢你的回答。

Pierre-Luc 皮埃尔·卢克

It seems that proxy table routing was removed from node-http-proxy when they released version 1.0.0. 似乎当他们发布1.0.0版本时,代理表路由已从node-http-proxy中删除。

However, they provided a new way of doing it using the new API: 但是,他们提供了使用新API的新方法:

https://blog.nodejitsu.com/node-http-proxy-1dot0/ https://blog.nodejitsu.com/node-http-proxy-1dot0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM