在 socket.io 和 express(node.js) 上获取 CORS ERROR
Getting CORS EROOR on socket.io and express(node.js)
Socket.io 和 Node.js (express.js) 出现 CORS 错误
let io;
module.exports = {
init: (httpServer) => {
io = require("socket.io")(httpServer);
return io;
},
getIO: () => {
if (!io) {
throw new Error("Socket.io not inittialized");
}
return io;
},
};
在必须发送的 socket js 中尝试此代码
cors: {
origin: "*",
methods: ["GET", "POST"],
},
套接字中的cors也可以避免这个错误
let io;
module.exports = {
init: (httpServer) => {
io = require("socket.io")(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
return io;
},
getIO: () => {
if (!io) {
throw new Error("Socket.io not inittialized");
}
return io;
},
};
Socket.io 和 Node.js (express.js) 出现 CORS 错误
let io;
module.exports = {
init: (httpServer) => {
io = require("socket.io")(httpServer);
return io;
},
getIO: () => {
if (!io) {
throw new Error("Socket.io not inittialized");
}
return io;
},
};
在必须发送的 socket js 中尝试此代码
cors: {
origin: "*",
methods: ["GET", "POST"],
},
套接字中的cors也可以避免这个错误
let io;
module.exports = {
init: (httpServer) => {
io = require("socket.io")(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
return io;
},
getIO: () => {
if (!io) {
throw new Error("Socket.io not inittialized");
}
return io;
},
};