简体   繁体   English

节点 express-http-proxy 未将请求路由到服务器

[英]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.当我去localhost:5000/app2它加载正常。 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.我在端口 5001 上运行它。当我转到localhost:5001 my-site.com 时按预期加载。 When I go to localhost:5001/app2 I get nothing and i see no traffic on the server.当我转到localhost:5001/app2时,我什么也得不到,也看不到服务器上的流量。

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.对于更多的上下文,我最初在/而不是/app2上提供了 app2,然后我能够让我的代理服务器加载 app2。 But when I changed app2 to serve static content at /app2 it started breaking.但是,当我将 app2 更改为在/app2处提供静态内容时,它开始崩溃。

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.无论我输入什么,代理似乎都希望始终将请求发送到/而不是/app2

I am using express-http-proxy:1.4.0 and express:4.16.4我正在使用 express-http-proxy:1.4.0 和 express:4.16.4

Any help is appreciated.任何帮助表示赞赏。

Try to use proxyReqPathResolver option尝试使用proxyReqPathResolver选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM