繁体   English   中英

apache2上的socket.io和nodejs的CORS标头错误

[英]CORS header error with socket.io and nodejs at apache2

我只是想在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). 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');
});

请帮助我找到原因。

您需要在app.js中添加标题。

app.use(function (req, res, next) {
   res.header("Access-Control-Allow-Origin", "http://localhost:3001");

  next();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM