Socket.io + Node.js CORS 错误阻止了我的请求

Socket.io + Node.js CORS ERROR Blocked my req

我正在使用 node 和 socket.io 编写一个应用程序,它给出了启用跨源请求的错误。

const io = require("./socket").init(server, {
      cors: {
        origin: "*",
        methods: ["GET", "POST"],
      },
    });

这是我的 soket.io 代码...

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

希望对你有用...