'cors issue with angular & nodejs [closed]

i'm using angular & node.js & cors

in my local device everything is good and work

and now it's time to deployment to cloud server

the path of each frontend and backend

root/backend work on port :5000

root/frontend work on port :5002

(digitaloceanenter image description here)

i set up apache2 and phpmyadmin

in backend

 var corsOptions = { origin: "http://localhost:5002" };

it should by work

but it give to me status:0

and i open 2 terminal for run project frontend and project backend

ng serve --host 0.0.0.0 --port 5002

when i run it work https//ip:5002 but without data

Edit:

and i got error like this also

Access to XMLHttpRequest at 'http://localhost:5000/api/auth/signup' from origin 'http://ip:5002' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:5002' that is not equal to the supplied origin.


Solution 1:[1]

If you have deployed your projects on a server:

Try Changing this in your backend:
var corsOptions = { origin: "http://localhost:5002" };

To this:

cors({
     origin: "*",
        })

note: "*" means anyone can connect to your API or you can put your server' IP that serve your Forntend Project.

And on the Forntend side make sure you are calling your requests from your backend's server, for example:

on your local machine you would call it from: http://localhost:5000/api/auth/signup

but when your backend on a server, you need to change it to:
http://<your-backend-server-IP or DNS>/api/auth/signup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman