简体   繁体   English

在 socket.io 和 express(node.js) 上获取 CORS EROOR

[英]Getting CORS EROOR on socket.io and express(node.js)

Socket.io and Node.js (express.js) getting CORS error 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;
  },
};

Try this code in socket js you have to send在必须发送的套接字 js 中尝试此代码

cors: {
       origin: "*",
       methods: ["GET", "POST"],
      }, 

Cors in the socket also to avoid this error套接字中的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;
      },
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM