HungryTurtleCode / multiplayerSnake

Code for multiplayer snake game with socket.io tutorial
117 stars 123 forks source link

CORS Error on 15:00 #3

Closed tsensei closed 1 year ago

tsensei commented 3 years ago

On the video (one at brad traversys channel), around 15:00 seconds, while making the request from index.js , we are getting a cors error. The probable reason for this error maybe the current version of 4.1.2 and the version in client code is 2.3.0. I got the solution from one of the comments that reinstalling the older version solves the problem or you may include cors header while initializing socket. Maybe you should pin the comment or give any solution to this

jimmirra commented 2 years ago

As of version 3.0 of socket.io, you need to explicitly enable CORS. documentaion

const http = require("http");
const socket_io = require("socket.io");

const httpServer = http.createServer();
const io = new socket_io.Server(httpServer, {
  cors: {
    origin: "http://127.0.0.1:8080",
  },
});
i-am-darshil commented 2 years ago

This helps to solve cors and 400 Bad Request Issue

const httpServer = require("http").createServer();
const io = require('socket.io')(httpServer,{
  cors: {
          origin: "http://127.0.0.1:5500",
          methods: ["GET", "POST"],
          credentials: true,
          transports: ['websocket', 'polling'],
  },
  allowEIO3: true
  })