craigtb
craigtb

Reputation: 667

Node express-http-proxy not routing request to server

I am trying to create and express server that reverse proxies to multiple applications. What I am running in to is that when I go to one of the routes the request never makes it to the server. Here are some code snippets of what I am doing:

let app = express();
app.use('/app2', express.static(__dirname + '/build', {
setHeaders: (res, req, path) => {
    console.log(path);

    metrics.httpRequestDurationMicroseconds // handles http_request_duration_ms Duration of HTTP requests in ms
        .labels(req, path, res.statusCode)
        .observe(10)
}

}));

When I go to localhost:5000/app2 it loads fine. Here is my proxy server:

proxyApp.use('/app2', proxy('http://localhost:5000/app2' ) );
proxyApp.use('/', proxy('http://my-site.com/'));

I am running it on port 5001. When i go to localhost:5001 my-site.com loads as expected. When I go to localhost:5001/app2 I get nothing and i see no traffic on the server.

For a little more context, I originally had app2 being served at / instead of /app2 and then I was able to make my proxy server load app2. But when I changed app2 to serve static content at /app2 it started breaking.

Anyone have any ideas on how to make this work or what is going on? It looks like the proxy wants to always send requests to / instead of /app2 no matter what I put in.

I am using express-http-proxy:1.4.0 and express:4.16.4

Any help is appreciated.

Upvotes: 1

Views: 831

Answers (1)

dudix
dudix

Reputation: 11

Try to use proxyReqPathResolver option

app.use('/app2', proxy("http://localhost:5000", {
  proxyReqPathResolver: function (req) {
    return "/app2"
  },
}

Upvotes: 1

Related Questions