Implementación de proxy inverso en Nodejs

I am trying to create a reverse proxy using nodejs , where i could map different ombinations to point to various websites.

The follwing is my first attempt code:

var http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createServer(function (req, res, proxy) {
  proxy.proxyRequest(req, res, {
  host: 'www.yahoo.com',
  port: 80
 });
}).listen(8000);

well works fine , but if i have a whole set of combinations . i dunno how to go about doing so.

My Second attempt

var http = require('http'),
httpProxy = require('http-proxy');

var mapping = {
hostnameOnly: true ,
router : {
 '127.0.0.1:8000' : 'www.google.com' ,
 '127.0.0.1:8001' : 'www.yahoo.com'
   }
}

var proxyserver = httpProxy.createServer(mapping).listen (80) ;

This doest even work , i dunno why .

Well all i am trying to create is simple application where i map to domains and fetch those pages. I am still getting a hang of nodejs , so i apologies if the question sounds ridiculous.

preguntado el 09 de marzo de 12 a las 15:03

Not exactly an answer, but here's some tips to help you diagnose the problem. NodeJS stores all dependencies under node_modules. Dependencies under node_modules are just js files. Dependencies like node-http-proxy are written completely in JavaScript, therefore you're able to change them. Just go to node-http-proxy under your node_modules and add a few console.log(). It should help. -

1 Respuestas

Your second attempt fails because the hostnameOnly specification does not accept ports in the routing paths. From the node-http-proxy docs:

var options = {
  hostnameOnly: true,
  router: {
    'foo.com': '127.0.0.1:8001',
    'bar.com': '127.0.0.1:8002'
  }
}

You're on the right track though. See aquí. It should work fine if you omit the hostnameOnly argumento.

Respondido el 26 de diciembre de 13 a las 23:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.