如何设置Apache ProxyPass以保留Express路由

时间:2022-04-11 16:54:20

In my Apache config im forwarding all traffic on /node to port 3000, where the Express server is listening.

在我的Apache配置中,我将/ node上的所有流量转发到Express服务器正在侦听的端口3000。

<IfModule mod_proxy.c>

  ProxyRequests Off

  ProxyPass /node http://localhost:3000/

</IfModule>

The Express app looks like this:

Express应用程序如下所示:

var express = require('express');

var app = express();
var router = express.Router();

router.route('/route/:id').get(function (req, res) {

    res.json({ description: 'Welcome to a route with an ID' }); 
});

router.route('/route').get(function (req, res) {

        res.json({ description: 'Welcome to the normal route' }); 
});

router.route('/').get(function (req, res) {

    res.json({ data: 'Welcome to the app' }); 
});

app.use('/', router);

app.listen(3000);

When I direct my browser to http://myserver.com/node I get the response { data: 'Welcome to the app' }, which is fine. Though, when I try to go http://myserver.com/node/route or http://myserver.com/node/1210 I get an error Cannot GET //route.

当我将浏览器定向到http://myserver.com/node时,我收到了响应{data:'Welcome to the app'},这很好。但是,当我尝试去http://myserver.com/node/route或http://myserver.com/node/1210时,我收到错误无法获取//路由。

Any ideas how I can update my Apache config to preserve the Express routes?

有什么想法我如何更新我的Apache配置以保留Express路线?

I'm running Apache 2.4.6 on CentOS.

我在CentOS上运行Apache 2.4.6。

1 个解决方案

#1


9  

You have an extra / at the end of your host. Try changing it to:

你有一个额外的/在你的主机结束。尝试将其更改为:

ProxyPass /node http://localhost:3000

#1


9  

You have an extra / at the end of your host. Try changing it to:

你有一个额外的/在你的主机结束。尝试将其更改为:

ProxyPass /node http://localhost:3000