ajsie
ajsie

Reputation: 79686

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

Upvotes: 13

Views: 8867

Answers (1)

bmaeser
bmaeser

Reputation: 1010

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);

Upvotes: 12

Related Questions