Heroku双'/'问题-节点js后端,404错误

os8fio9y  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(71)

我已经在heroku上部署了我的后端,并且在/上测试了get请求的响应

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

在到达/端点https://ayush-portfolio-backend.herokuapp.com/时,我得到预期的GET输出。
但是,当从前端react应用程序,我击中任何POST端点,例如:-

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

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

我无法在前端获得任何响应。
正如heroku docs所建议的,当我执行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的错误,但没有找到任何相关的解决方案的问题.
我只是假设这个错误可能是因为在路径中创建了一些double ///,如在错误中所显示的path='//api/portfolios/getportfolios'。因此,我也尝试从前端和其他地方删除/,试图使其成为single/,但这并没有解决问题。
请有人指导我找到解决上述问题的方法。谢谢!
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
投资组路由文件:github

siotufzp

siotufzp1#

尝试

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

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

相关问题