DEV Community

Discussion on: How to create a simple and beautiful chat with MongoDB, Express, React and Node.js (MERN stack)

Collapse
 
sumnanazadi profile image
Sumnan Azadi

I tried this on my localhost but here I got
"GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NMdKy4- net::ERR_FAILED"
"Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NMdKy4-' from origin 'localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Collapse
 
jeffsoriano profile image
Jeff Soriano

If you're using socket.io v3.0+ you need to add this to index.js in the server (replace old code with this):

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

socket.io/docs/v3/migrating-from-2...