我只是想在laravel中隐含socket.io。但出现Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3001/socket.io/?EIO=3&transport=polling&t=MM9_PhA. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
错误。
下面是我的node.js文件:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function (err, count) {
});
redis.on('message', function (channel, message) {
io.set('origins', 'http://localhost:3001');
console.log('Message Recieved: ' + message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
http.listen(3001, function () {
io.set('origins', 'http://localhost:3001');
console.log('Listening on Port 3001');
});
请帮助我找到原因。
答案 0 :(得分:0)
您需要在app.js中添加标题。
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3001");
next();
});