Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Problema de CORS con socket.io

Al necesitar un websocket con mi sitio web, quería usar socket.io .

Sin embargo, estoy experimentando un problema de CORS:

 Access to XMLHttpRequest at 'http://localhost:2021/socket.io/?EIO=4&transport=polling&t=NlbFGS2' from origin 'http://localhost:63341' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. polling-xhr.js:202 GET http://localhost:2021/socket.io/?EIO=4&transport=polling&t=NlbFGS2 net::ERR_FAILED

Así que encontré este código en el sitio socket.io que probé pero sigo teniendo el mismo problema...

 const app = require('express')(); const http = require('http').createServer(app); const io = require("socket.io")(http, { cors: { origin: ["http://localhost:63341/"], methods: ["GET", "POST"] } })

Y aquí está el código en el lado del cliente:

 <script src="https://cdn.jsdelivr.net/npm/socket.io-client@4.1.2/dist/socket.io.msgpack.min.js"></script> <script> let socket = io('http://localhost:2021') </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Siga estos pasos en su servidor backend :

  1. Ejecute npm install cors

  2. Agregue este código después de crear la app :

     app.use(cors({ origin: "http://localhost:63341" }))
  3. Reinicie su servidor y verifique si funciona o no.

  4. Si aún no funciona, cambie su line 3 con esto:

     const io = require("socket.io")(http); // no cors configuration.
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar esto para permitir que cualquier cosa se conecte... Sin embargo, no haga esto en producción.

 export const io = new Server(http, { cors: { origin: "*" }, });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!