Home > OS >  Heroku double '/' problem - node js backend, 404 error
Heroku double '/' problem - node js backend, 404 error

Time:09-22

I have deployed my backend on heroku and I have a test get request's response on /

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.

But, when from the frontend react app, I hit any POST endpoint, e.g.:-

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

I just assume that the error can be because of some double / // creating in the path as in the error it is shown e.g. 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

CodePudding user response:

try

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

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