Access-Control-Allow-Origin'标头包含节点中带有websocket的多个值

时间:2018-08-25 06:46:15

标签: node.js sockets

连接时出现的错误是:

“无法加载http://localhost:8081/socket.io/?EIO=3&transport=polling&t=MLlqN2Y:'Access-Control-Allow-Origin'标头包含多个值'http://localhost:3000,*',但只允许一个。Origin'http://localhost:3000因此,不允许访问。”

我在这里看到了类似的问题,但是尝试了所有解决方案,但均无济于事。这是我的服务器代码atm:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var express = require('express');
var cors = require('cors');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);
var Models = require('./models');
//var authRoutes = require('./routes/auth.js');
var apiRoutes = require('./routes/api.js');
var http = require('http');



app.use('/api', apiRoutes);
//
app.use(cors());
app.options('*', cors());

 server.listen(8081);
io.on('connection', function(socket) {
  console.log('connected to socket')
  socket.emit('connect', {hello: 'world' });
  socket.on('cmd', function (data) {
    console.log(data);
  });
  socket.on('room', function(data) {
    socket.broadcast.to(data.doc).emit('joined', data.user);
    socket.join(data.doc);

  });

});

app.set('port', 8080);

app.listen(app.get('port'), () => {
  console.log(`Server is running on port ${app.get('port')}`)
})

0 个答案:

没有答案