2

I have two instances of the same node app running on port 3000 and 4000. I would like to have the following behavior:

https://localhost/dev ==> http://localhost:3000 https://localhost/prod ==> http:// localhost:4000

I have the following proxy in place:

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

var PATH_TO_KEY = "/home/wow/browser.key",
    PATH_TO_CERT = "/home/wow/browser.crt",
    PATH_TO_CHAIN = "";

var options = {
  ssl: {
    key: fs.readFileSync(PATH_TO_KEY, 'utf8'),
    cert: fs.readFileSync(PATH_TO_CERT, 'utf8'),
    //ca : fs.readFileSync(PATH_TO_CHAIN, 'utf8')
  },

  target: "http://localhost:4000", // this is prod
  ws: true,
  xfwd: true,
  router: {
    'https://localhost/dev': 'http://127.0.0.1:3000/',
    'https://localhost/prod': 'http://127.0.0.1:4000/',
  },
  pathRewrite: {
    '^/dev' : '/',     // remove /dev/ path
    '^/prod' : '/'           // remove /prod/ path
  },
};
var server = httpProxy.createProxyServer(options).listen(443);

However, when I visit either https://localhost/dev or https://localhost/prod, the following happens:

  1. It always redirects to the target which is specified and not any of the urls specified in the router.
  2. The pathRewrite does not seem to happen since the I always end up at target/prod or target/dev. The path for /dev and /prod do not exist and hence I get a page cannot be displayed error.

I'm not sure where I'm going wrong with this. Could someone help?

Thanks.

blueren
  • 2,730
  • 4
  • 30
  • 47

1 Answers1

1

'router' is not an option

Not only is 'router' not a documented feature of http-proxy, it's simply not a feature of http-proxy at all:

git clone https://github.com/nodejitsu/node-http-proxy.git
> Cloning into 'node-http-proxy'...
> remote: Counting objects: 5786, done.
> remote: Total 5786 (delta 0), reused 0 (delta 0), pack-reused 5785
> Receiving objects: 100% (5786/5786), 1.30 MiB | 6.06 MiB/s, done.
> Resolving deltas: 100% (2784/2784), done.
pushd node-http-proxy/
grep -r 'router' .
> # empty output

Nor is it a feature of any of its dependencies:

npm install
grep -r 'router' .
> # empty output again

It is also not a feature of https-proxy, by the same method of checking as seen above.

However, there are some other options:

Questions to ask yourself:

1. Do you need this in node?

As someone with a deep and abiding love-hate relationship with node (I joined in the v0.2.x days and have a few commits in core) and who is familiar with it's ugly network stack (author of Greenlock, Goldilocks, Telebit), and having opened multiple issues with the tls, http, and net modules I can tell you this:

node is almost certainly the wrong technical tool for the job... but it may still be the right choice depending on the community you're targeting

2. Could the other apps be in node?

What's the purpose that you need to forward to various ports? Could you reasonable write those applications as plugins to your main application? Or are you supporting arbitrary applications?

coolaj86
  • 74,004
  • 20
  • 105
  • 125
  • Thanks for the detailed answer @CoolAJ86, at the moment my requirement for a reverse proxy is very limited and I do not feel the need for a dedicated service such as nginx necessary. I shall take a look at the ones you've mentioned. – blueren Jul 04 '18 at 16:50