【问题标题】:Getting CORS EROOR on socket.io and express(node.js)在 socket.io 和 express(node.js) 上获取 CORS 错误
【发布时间】:2021-10-11 10:25:57
【问题描述】:

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;
  },
};

【问题讨论】:

    标签: node.js express websocket socket.io


    【解决方案1】:

    在你必须发送的 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;
          },
        };
    

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 2017-01-18
      • 2015-11-21
      • 2022-01-14
      • 2015-06-24
      • 2023-03-19
      • 2019-11-09
      • 2012-12-12
      • 2017-06-04
      相关资源
      最近更新 更多