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;
},
};
uj5u.com熱心網友回復:
在您必須發送的套接字 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;
},
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312243.html
