0

I'm using react front end with nodeJs v6 backend, and socket.io-redis pub sub method. the connection is refused by the browser in this case.
I've this error: Failed to load https://api.mydomain.com/socket.io-client/?EIO=3&transport=polling&t=M8vzD5C: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed. Origin 'http://localhost:3000' is therefore not allowed access. im running on a front end react, backend nodeJs , with pub sub socket-redis connection here are my response headers : Access-Control-Allow-Credentials: true Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since, Authorization Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS Access-Control-Allow-Origin: http://localhost:3000 Access-Control-Allow-Origin: * Connection: keep-alive Content-Length: 104 Content-Type: text/plain; charset=UTF-8 Date: Sun, 18 Mar 2018 16:19:12 GMT Server: nginx/1.10.3 (Ubuntu) may i know what did i do wrongly apparently a connection can be established but it breaks off immediately and keeps trying to reconnect in a loop (which i know why) but why will my response header have mutliple values ?

DarkArtistry
  • 434
  • 1
  • 6
  • 22
  • You will have to show us your server code for us to identify what you did wrong there. It looks like you might have some duplicate middleware both setting CORS headers. – jfriend00 Mar 18 '18 at 16:56
  • @jfriend00 I actually sovled it by removing the add_header in my nginx file. May i know if this is a good way to solve it ? – DarkArtistry Mar 19 '18 at 05:20
  • OK, you can either write your own answer (if you think the answer would be useful to others) or you can delete your question. It shouldn't be left just hanging out here with no answer. – jfriend00 Mar 19 '18 at 05:32
  • @jfriend00 i thought i solved it, so in my ngnix site-enabled folder, theres an add_header to allow cors. however if by commenting it out, sockets gets connected correctly, BUT my API will have CORS issue ... god ... save me ... LOL – DarkArtistry Mar 19 '18 at 07:24

1 Answers1

-1

this is what I've added in the above code : and i commented out the add_headers that were affecting the socket requests

set $cors '';
if ($request_uri !~ '^/socket.io-client') {
set $cors 'true';
}

if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
DarkArtistry
  • 434
  • 1
  • 6
  • 22