Frontend works, but API calls fail after deploying React-Redux app to Heroku

I’ve got a React-Redux app that runs smoothly on my local machine. It uses Express, MongoDB Atlas, and a Node.js server for the backend. However, I’m having trouble after deploying it to Heroku.

When I try to use the app on Heroku, I keep getting ‘failed to fetch’ errors. I’m not sure what’s causing this issue.

I just realized I set the base URL to ‘localhost:3000’ in my client-side code. Should I change this to the Heroku app URL instead? I’m not sure if that’s the problem or if there’s something else I’m missing.

It’s weird because when I run the backend locally on localhost:3000, everything works fine with the Heroku-deployed frontend. But as soon as I shut down my local server, the Heroku app stops working and shows the ‘failed to fetch’ error again.

Any ideas on how to fix this? I’m pretty stuck and could use some guidance on getting my API calls to work properly after deployment. Thanks!

It sounds like you’ve identified the root cause of your issue. Changing the base URL from ‘localhost:3000’ to your Heroku app URL is indeed crucial for your deployed application to function correctly. This mismatch is why your app works with a local backend but fails when relying on the Heroku-deployed server.

To resolve this, consider implementing environment-specific configuration. You can use environment variables to dynamically set the base URL depending on whether the app is running in development or production. This approach allows your code to work seamlessly in both environments without manual changes.

Additionally, ensure your Express server is properly configured for CORS, as this is often overlooked and can cause issues with API calls in a deployed environment. Double-check your Heroku configuration and logs for any other potential server-side issues that might be contributing to the problem.

hey man, sounds lik ur havin a rough time wit heroku. yeah, u def need to change that base URL. try using process.env.NODE_ENV to switch between local and production URLs. also, make sure ur expresss server is set up for CORS. those things should help fix ur fetch issues. good luck!