在我的 Apache 配置中,我将 /node
上的所有流量转发到端口 3000
,Express 服务器正在监听该端口。
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPass /node http://localhost:3000/
</IfModule>
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);
当我将浏览器指向 http://myserver.com/node 时我收到响应 { data: 'Welcome to the app' }
,很好。虽然,当我尝试去 http://myserver.com/node/route或 http://myserver.com/node/1210我收到错误消息 Cannot GET//route
。
有什么想法可以更新我的 Apache 配置以保留 Express 路由吗?
我在 CentOS 上运行 Apache 2.4.6。
最佳答案
在主机的末尾有一个额外的 /
。尝试将其更改为:
ProxyPass /node http://localhost:3000
关于node.js - 如何设置 Apache ProxyPass 以保留 Express 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152648/