簡體   English   中英

部署在 heroku 上的節點應用程序 - 出現 404 錯誤(即使 url 是正確的)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM