Node.js 如何为heroku设置socket.io CORS?

Node.js 如何为heroku设置socket.io CORS?,node.js,express,heroku,socket.io,Node.js,Express,Heroku,Socket.io,如果我在Express中使用socket.io,如何为heroku设置CORS?我的应用程序可以在本地主机上运行,但当我在Heroku上部署时,它不能在手机和Chrome上运行。当我在chrome上检查console.log时,我发现了这个错误 A`ccess to XMLHttpRequest at 'https://fart-game.herokuapp.com/socket.io/?EIO=4&transport=polling&t=Nc0YKct' from origin

如果我在Express中使用socket.io,如何为heroku设置CORS?我的应用程序可以在本地主机上运行,但当我在Heroku上部署时,它不能在手机和Chrome上运行。当我在chrome上检查console.log时,我发现了这个错误

A`ccess to XMLHttpRequest at 'https://fart-game.herokuapp.com/socket.io/?EIO=4&transport=polling&t=Nc0YKct' from origin 'http://fart-game.herokuapp.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains the invalid value 'https://fart-game.herokuapp.com/'.`
我的index.js如下所示

const express = require('express');
const path = require('path');
const http = require('http');
const socketio = require('socket.io');

const { addUser, getUser, getUsersInRoom, removeUser } = require('./users');
const { rollFunction, 
        sumNumbers
    } = require('./game')

var cors = require('cors');

const app = express();
app.use(cors())

const buildPath = path.join(__dirname, '..', 'build');
 app.use(express.static(buildPath));

const server = http.createServer(app);

const io = socketio(server, {
    cors: {
        origin: `https://fart-game.herokuapp.com/`,
        methods: ["GET", "POST"],
        credentials: true
      }
});

const PORT = process.env.PORT || 5000;

const router = require('./router');
const { get } = require('./router');
const { isFunction } = require('util');

app.use(router)

server.listen(PORT, () => {
    console.log(`Server is runing on port ${PORT}`);
});
依赖关系

"cors": "^2.8.5",
 "express": "^4.17.1",
 "socket.io": "^4.0.1"

我在错误中注意到了这一部分

 from origin 'http://fart-game.herokuapp.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains the invalid value 'https://fart-game.herokuapp.com/'.`
您接受来自
https://fart-game.herokuapp.com/
但实际上从
http://fart-game.herokuapp.com
,所以它不起作用

我认为这应该奏效:

const io = socketio(server, {
    cors: {
        origin: `http://fart-game.herokuapp.com`, // I copied the origin in the error message and pasted here
        methods: ["GET", "POST"],
        credentials: true
      }
});

是的,现在可以用了。。下次使用HTTP和HTTPS时需要更多注意:)