Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js as a forwarding proxy but changing the URL path?

How do i let node.js act as a proxy and forward all requests sent from one server to another server but stripping /couchdb/ from the url path so that for example POST /couchdb/mydatabase will be POST /mydatabase. And when it receives the response it should send it to the first server.

All I have done is this (using express) to get all requests where the URL path starts with /couchdb/

app.all(/^\/couchdb\/(?:.)*/, function(req, res) {

});

Could someone guide me through. Thanks

like image 435
ajsie Avatar asked Feb 11 '11 07:02

ajsie


1 Answers

have a look at node-http-proxy. you can use it like this:

  var http = require('http'),
  httpProxy = require('http-proxy');
  httpProxy.createServer(function (req, res, proxy) {
         // Put your custom server logic here (eg rewrite url/header,...)
      proxy.proxyRequest(req, res, {host: 'localhost', port: 9000});
  }).listen(8000);
like image 71
bmaeser Avatar answered Oct 19 '22 15:10

bmaeser