我一直试图在SocketIO上设置CORS,但是它不起作用。这是我的代码...
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.sendfile('./sc/index.html');
});
io.origins(['http://localhost:3000']);
io.on('connection', function(socket) {...});
但是在客户端,我会收到此错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
有人可以帮助我解决这个问题。我的SocketIO version is 2.1.1
谢谢!