ExpressJS 和 Apache:Sub-routes 不工作
ExpressJS and Apache: Sub-routes not working
让我假设 base routes do 工作正常,下面列出的每个文件都放在网站的根文件夹中。
我的router.js
结构如下:
const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
res.send({ response: "Server is up and running good." }).status(200);
});
router.get("/user", (req, res) => {
res.send({ response: "subroute" }).status(200);
});
router.post('/', (req, res) => {
res.send({ response: 'true' })
})
module.exports = router;
当然,在index.js
(主文件)中,我指示app
到use
上面的路由器
const router = require('./router');
app.use('/', router);
在 Apache2 VirtualHost 配置上,我有以下代理配置:
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / https://127.0.0.1:8443
ProxyPassReverse / https://127.0.0.1:8443
正如标题所说,问题是 sub-routes (/user
) 不工作,而是抛出 502 Bad Gateway 错误。 POST 和 GET 在基本路线 /
上,但是,工作正常。
有趣的是,正如我所相信的那样,问题变成了 ProxyPass
和 ProxyPassReverse
指令。
具体来说,您需要在目标主机的末尾添加一个尾部斜线。
配置错误:
ProxyPass / https://127.0.0.1:8443
ProxyPassReverse / https://127.0.0.1:8443
有效配置:
ProxyPass / https://127.0.0.1:8443/
ProxyPassReverse / https://127.0.0.1:8443/
让我假设 base routes do 工作正常,下面列出的每个文件都放在网站的根文件夹中。
我的router.js
结构如下:
const express = require("express");
const router = express.Router();
router.get("/", (req, res) => {
res.send({ response: "Server is up and running good." }).status(200);
});
router.get("/user", (req, res) => {
res.send({ response: "subroute" }).status(200);
});
router.post('/', (req, res) => {
res.send({ response: 'true' })
})
module.exports = router;
当然,在index.js
(主文件)中,我指示app
到use
上面的路由器
const router = require('./router');
app.use('/', router);
在 Apache2 VirtualHost 配置上,我有以下代理配置:
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / https://127.0.0.1:8443
ProxyPassReverse / https://127.0.0.1:8443
正如标题所说,问题是 sub-routes (/user
) 不工作,而是抛出 502 Bad Gateway 错误。 POST 和 GET 在基本路线 /
上,但是,工作正常。
有趣的是,正如我所相信的那样,问题变成了 ProxyPass
和 ProxyPassReverse
指令。
具体来说,您需要在目标主机的末尾添加一个尾部斜线。
配置错误:
ProxyPass / https://127.0.0.1:8443
ProxyPassReverse / https://127.0.0.1:8443
有效配置:
ProxyPass / https://127.0.0.1:8443/
ProxyPassReverse / https://127.0.0.1:8443/