简体   繁体   English

部署在 heroku 上的节点应用程序 - 出现 404 错误(即使 url 是正确的)

[英]Node app deployed on heroku - getting 404 error (even when url is correct)

I have deployed my backend on heroku and I have a test get request's response on /我已经在 heroku 上部署了我的后端,并且我在/上有一个测试获取请求的响应

app.get('/', (req, res) => {
    res.send("Hello, World. The app is running!");
});

On hitting the / end point that is https://ayush-portfolio-backend.herokuapp.com/ , I get the expected GET output.到达 https://ayush-portfolio-backend.herokuapp.com//端点时,我得到了预期的GET output。

But, when from the frontend react app, I hit any POST endpoint, eg:-但是,当从前端反应应用程序时,我点击任何POST端点,例如:-

// index.js
app.use('api/portfolios/', (require('./routes/portfolio')));

// portfolio route
router.post('/getportfolios', async (req, res) => {...});

I am unable to get any response on the frontend.我无法在前端得到任何响应。

As suggested by heroku docs, when I do heroku logs正如 heroku 文档所建议的那样,当我执行heroku logs
I get the following error:-我收到以下错误:-

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

I don't know what is causing the following error.我不知道是什么导致了以下错误。 The app works perfectly fine on the local environment, but don't know why the problem has occured.该应用程序在本地环境中运行良好,但不知道为什么会出现问题。

I am a beginner for heroku and I don't know much about it, also i searched the google and stackoverflow for the error but did not find any relevant solution to the problem.我是 heroku 的初学者,对此我不太了解,我也搜索了谷歌和 stackoverflow 的错误,但没有找到任何相关的问题解决方案。

I just assume that the error can be because of some double / // creating in the path as in the error it is shown eg path='//api/portfolios/getportfolios' .我只是假设该错误可能是由于在路径中创建了一些双 / //创建的错误,例如path='//api/portfolios/getportfolios' So, I also tried removing the / from frontend and everywhere else trying to make it sinbgle / but this didn't solve the problem.因此,我还尝试从前端和其他任何地方删除 / 试图使其单一 / 但这并没有解决问题。

Can someone please guide me to find the solution of the above problem.有人可以指导我找到上述问题的解决方案。 Thank you!谢谢!

////////////////EDIT//////////////// ////////////////编辑////////////////
portfolio route file: github组合路由文件: github

try尝试

// 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.

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