Question

I'm trying to make a NodeJS http-proxy with a Router Table. I saw some examples using http-proxy and try like this :

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.

It seems to work (it only intercepts request to testproxy.com/toto and tata) but when I try :

curl http://testproxy.com/toto

I have a NodeJS error :

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 ? I have to do a dynamic proxy, any idea ?

I'm new at NodeJS, and I'm stuck. Thanks a lot for your answers.

Pierre-Luc

Was it helpful?

Solution

It seems that proxy table routing was removed from node-http-proxy when they released version 1.0.0.

However, they provided a new way of doing it using the new API:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top