Cors error with socket.io: No 'Access-Control-Allow-Origin' header is present on the requested Resource

Asked

Viewed 16 times

0

I have an API with a running socket.io instance, configured as follows:

  const sock = socketIo(server /* servidor express */, {
    origins: [CONFIG.frontend /* variavel que contem a url do front */],
    handlePreflightRequest: (_req: any, res: ServerResponse) => {
         origins: [CONFIG.frontend],
         handlePreflightRequest: (_req: any, res: ServerResponse) => {
              res.setHeader("Access-Control-Allow-Origin", CONFIG.frontend);
              res.writeHead(200, {
                  "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
                  "Access-Control-Allow-Credentials": "true",
                  "Access-Control-Allow-Headers":"cf-connecting-ip,cf-ipcountry,cf-ray,cf-visitor,x-auth-token,x-forwarded-for,x-forwarded-proto",
         });
         res.end();
      },
    },
  });
 sock.origins(CONFIG.frontend);

And this setting works in local environment (front: localhost:3000, api: localhost:3001)

However, in production (front: sub.dominio.com, api: api.dominio.com), it gives the error: No 'Access-Control-Allow-Origin' header is present on the requested resource

Only in production this error occurs, how can I correct?

(deploy is made using Docker)

Edit: The header goes in the request of options, it fails when it actually connects to the socket, follows a photo of the error: erro de cors no navegador

Request for preflight: Request de preflight da requisição, sem erro

Request itself from socket: Dados da request do servidor

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.