[英]Node app deployed on heroku - getting 404 error (even when url is correct)
我已经在 heroku 上部署了我的后端,并且我在/
上有一个测试获取请求的响应
app.get('/', (req, res) => {
res.send("Hello, World. The app is running!");
});
在到达 https://ayush-portfolio-backend.herokuapp.com/的/
端点时,我得到了预期的GET
output。
但是,当从前端反应应用程序时,我点击任何POST
端点,例如:-
// index.js
app.use('api/portfolios/', (require('./routes/portfolio')));
// portfolio route
router.post('/getportfolios', async (req, res) => {...});
我无法在前端得到任何响应。
正如 heroku 文档所建议的那样,当我执行heroku logs
时
我收到以下错误:-
2022-09-21T07:57:41.743176+00:00 heroku[router]: at=info method=OPTIONS path="//api/portfolios/getportfolios" host=ayush-portfolio-backend.herokuapp.com request_id=0aff16a8-8aee-499e-a69e-4bdf8d583e6d fwd="103.164.24.154" dyno=web.1 connect=0ms service=5ms status=204 bytes=312 protocol=https2022-09-21T07:57:41.808753+00:00 heroku[router]: at=info method=OPTIONS path="//api/portfolios/getportfolios" host=ayush-portfolio-backend.herokuapp.com request_id=3c1974f2-522c-47b8-92d8-2f201bd0e2ab fwd="103.164.24.154" dyno=web.1 connect=0ms service=1ms status=204 bytes=312 protocol=https2022-09-21T07:57:42.054955+00:00 heroku[router]: at=info method=POST path="//api/portfolios/getportfolios" host=ayush-portfolio-backend.herokuapp.com request_id=6092ee53-d98d-4fa0-9fff-abd46554de56 fwd="103.164.24.154" dyno=web.1 connect=0ms service=10ms status=404 bytes=445 protocol=https
2022-09-21T07:57:42.172808+00:00 heroku[router]: at=info method=POST path="//api/portfolios/getportfolios" host=ayush-portfolio-backend.herokuapp.com request_id=cbe7b873-b4f5-4eb6-a80f-12fd1018029a fwd="103.164.24.154" dyno=web.1 connect=0ms service=2ms status=404 bytes=445 protocol=https
我不知道是什么导致了以下错误。 该应用程序在本地环境中运行良好,但不知道为什么会出现问题。
我是 heroku 的初学者,对此我不太了解,我也搜索了谷歌和 stackoverflow 的错误,但没有找到任何相关的问题解决方案。
我只是假设该错误可能是由于在路径中创建了一些双 / //
创建的错误,例如path='//api/portfolios/getportfolios'
。 因此,我还尝试从前端和其他任何地方删除 / 试图使其单一 / 但这并没有解决问题。
有人可以指导我找到上述问题的解决方案。 谢谢!
////////////////编辑////////////////
组合路由文件: github
尝试
// index.js
const portfolioRoutes = require('./routes/portfolio');
app.use('/api/portfolios', portfolioRoutes);
// portfolio route
router.post('/getportfolios', async (req, res) => {...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.