Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Netlify deploy can't connect to Heroku backend

I've built a wee program that works fine when I run it locally. I've deployed the backend to Heroku, and I can access that either by going straight to the URL (http://gymbud-tracker.herokuapp.com/users) or when running the frontend locally. So far so good.

However, when I run npm run-script build and deploy it to Netlify, something goes wrong, and any attempt to access the server gives me the following error in the console:

    auth.js:37 Error: Network Error
    at e.exports (createError.js:16)
    at XMLHttpRequest.p.onerror (xhr.js:99)

The action that is pushing that error is the following, if it is relevant:

export const signin = (formData, history) => async (dispatch) => {
  try {
    const { data } = await api.signIn(formData);

    dispatch({ type: AUTH, data });

    history.push("../signedin");
  } catch (error) {
    console.log(error);
  }
};

I've been tearing my hair out trying to work out what is changing when I build and deploy, but cannot work it out.

As I say, if I run the front end locally then it access the Heroku backend no problem - no errors, and working exactly as I'd expect. The API call is correct, I believe: const API = axios.create({baseURL: 'http://gymbud-tracker.herokuapp.com/' });

I wondered if it was an issue with network access to the MongoDB database that Heroku is linked to, but it's open to "0.0.0.0/0" (I've not taken any security precautions yet, don't kill me!). The MDB database is actually in the same collection as other projects I've used, that haven't had this problem at all.

Any ideas what I need to do?

The front end is live here: https://gym-bud.netlify.app/ And the whole thing is deployed to GitHub here: https://github.com/gordonmaloney/gymbud

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your issue is CORS (Cross-Origin Resource Sharing). When I visit your site and inspect the page, I see the following error in the JavaScript console which is how I know this:

Safari CORS Error

This error essentially means that your public-facing application (running live on Netlify) is trying to make an HTTP request from your JavaScript front-end to your Heroku backend deployed on a different domain.

CORS dictates which requests from a frontend application are ALLOWED to make a request to your backend API.

What you need to do to fix this is to modify your Heroku application and have it return the appropriate Access-Control-Allow-Origin header. This article on MDN explains the header and how you can use it.

Here's a simple example of the header you could set on your Heroku backend to allow this to work:

Access-Control-Allow-Origin: *

Please be sure to read the MDN documentation, however, as this example will allow any front-end application to make requests to your Heroku backend when in reality, you'll likely want to restrict it to just the front-end domains you build.

over 4 years ago · Santiago Trujillo Report

0

God I feel so daft, but at least I've worked it out.

I looked at the console on a different browser (Edge), and it said it was blocking it because it was mixed origin, and I realised I had just missed out the s in the https on my API call, so it wasn't actually a cors issue (I don't think?), but just a typo on my part!

So I changed: const API = axios.create({baseURL: 'http://gymbud-tracker.herokuapp.com' }); To this: const API = axios.create({baseURL: 'https://gymbud-tracker.herokuapp.com' });

And now it is working perfectly ☺️

Thanks for your help! Even if it wasn't the issue here, I've definitely learned a lot more about cors on the way, so that's good

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!