1

I create API, BackEnd and Frontend website.

I used Express for API, BackEnd - FrontEnd using ReactJs

So in my local everything works. But when i deployed to server This error message show

Access to XMLHttpRequest at 'http://domain/headline' from origin 'http://domain' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.

and

Access to XMLHttpRequest at 'http://domain/marketData/snack' from origin 'http://domain' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

in ReactJs im using axios for getting data.

And in my nginx server i already set

location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
        if ($request_method = 'GET') {
                add_header 'Access-Control-Allow-Origin' 'http://domain';
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            }
    }

Did i missed something?

Reza
  • 119
  • 2
  • 9

1 Answers1

0

Install cors first from npm by npm i cors in your api server. Then in your app.js or server.js or index.js (main file that you create http service) use these line of code

const cors = require('cors');
app.use(cors());
app.options('*', cors());
Riajul Islam
  • 1,425
  • 15
  • 19
  • yes, for express already using cors. – Reza Mar 20 '19 at 04:31
  • So now you need to just allow cors in your frontend service. Please check answer of this post it may help you https://stackoverflow.com/questions/43462367/how-to-overcome-the-cors-issue-in-reactjs – Riajul Islam Mar 20 '19 at 04:38