DEV Community

Discussion on: How To Deploy a Full-Stack MERN App with Heroku/Netlify

Collapse
 
salyadav profile image
Saloni Yadav

Did you not get a CORS error while making call from netlify to heroku?

Collapse
 
stlnick profile image
Nick

I did not. In my Express router I used the cors package so app.use(cors()) to prevent that.

I do, however, need to remove that and just specify to allow requests and responses between my Netlify and Heroku urls. I haven’t done it yet but I do know it can be done to allow those two urls to communicate.

Collapse
 
salyadav profile image
Saloni Yadav

Thanks for replying 😀

cors() package did not help with browser hosted on one domain making a backend call to a different domain server. I had to add "_redirects" file specified by Netlify documentation to redirect the browser-server call to server-server call between netlify and heroku.

Anyways, I followed this article to get my fullstack running. So a BIG BIG THANK YOU!! It was very helpful. 🤗

Thread Thread
 
stlnick profile image
Nick

I'm very glad it helped! And now you've helped me by giving me a better idea of what I'll need to do to set my cors up properly for Netlify instead of allowing all cors. So thank you!

Thread Thread
 
jealousgx profile image
JealousGx • Edited

I am getting the same cors error from the browser. I tried surfing the internet for the solution but could not find one and could not understand the Netlify's documentation either.

Now, the thing is: I want to register/login using my backend to save all the data and to retrieve it. I have changed everything from the client side, it looks fine to me and the server is also up and running. This is the only issue. How did you resolve it. Could you elaborate it?

--
For some reasons, I can not upload the image here but I am pretty sure you got the same error as mine. It's like:

Access to XML....(BACKEND URL) from origin (FRONTEND URL) has been blocked by CORS policy....

@stlnick
@salyadav
There is more to the error but I am just pointing out the main one.
I hope you get the point and help me resolve it asap!

--
Cheers~

Collapse
 
nataliekmiller92 profile image
Natalie Kappele-Miller

Thank you for this article by the way, it has helped me loads. Also, I know I am late to this post, but I am wondering how you solved your cors issue using _redirects file as I have scoured the docs and still confused about how to go about this. I have a _redirects file as well as a netfliy.toml file trying to redirect requests from my Netlfiy client to my Heroku backend but to no avail. Any direction in this matter would be greatly appreciated

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
salyadav profile image
Saloni Yadav

github.com/salyadav/gundoosmean-cl...

This is the reference to the client side code hosted on netlify.

github.com/salyadav/gundoosmean-se...

This is the server side code on heroku.

The reason why this will not be an issue on local, let’s say if you have your frontend backend in different port is because both are localhost and you are not actually making a cross origin request.

There Are other ways to resolve this core error, which is including your cors header in the http request that you are making.

Adding __redirects is another way.
Things to look out for-
The base_url changed to herokus server, and the redirect for the api calls.

Let me know if the reference to the github also doesn’t help... we can debug your code further.