Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js socket.io/express';跨源请求被阻止';使用firefox和';错误连接被拒绝';含铬_Node.js_Express_Socket.io - Fatal编程技术网

Node.js socket.io/express';跨源请求被阻止';使用firefox和';错误连接被拒绝';含铬

Node.js socket.io/express';跨源请求被阻止';使用firefox和';错误连接被拒绝';含铬,node.js,express,socket.io,Node.js,Express,Socket.io,我正在使用socket.io和express制作一个多用户应用程序。它工作得很好,直到我想把它变成多个实例,一个托管登录页,另一个运行每个节点项目 火狐错误 跨源请求被阻止:同一源策略不允许读取远程资源http://localhost:5002/socket.io/?EIO=3&transport=polling&t=N3Xwg8R. (原因:CORS请求未成功)。 铬误差 GEThttp://localhost:5002/socket.io/?EIO=3&transport=polling&t

我正在使用socket.io和express制作一个多用户应用程序。它工作得很好,直到我想把它变成多个实例,一个托管登录页,另一个运行每个节点项目

火狐错误
跨源请求被阻止:同一源策略不允许读取远程资源http://localhost:5002/socket.io/?EIO=3&transport=polling&t=N3Xwg8R. (原因:CORS请求未成功)。

铬误差
GEThttp://localhost:5002/socket.io/?EIO=3&transport=polling&t=N3XwwIP 网络::错误连接被拒绝

服务器 客户 现在来看有趣的部分。我试过的。大部分来自24058157个答案

var io=require('socket.io')(服务器,{origins:'*:'*:'})

const io = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});
io.origins(“*:*”)
nope

const io = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});
io.set('origins','*:*')

const io = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});
io.set('origins','http://mydomain.xyz:5002');

const io = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});
没有

还是没有

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', req.get('Origin') || '*');
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
  res.header('Access-Control-Expose-Headers', 'Content-Length');
  res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range');
  if (req.method === 'OPTIONS') {
    return res.send(200);
  } else {
    return next();
  }
});
这个可能用错了,但我不明白

也许是我的nginx配置


server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name mysite.xyz  www.mysite.xyz;

    location ^~ / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
location /multiplayerdrawing {
    rewrite ^/multiplayerdrawing/(.*) /$1 break;
        proxy_pass http://localhost:5002/multiplayerdrawing; #whatever port your app runs on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

您在控制台中遇到了什么错误。你能把完整的错误也加进去吗?我已经把两个完整的错误都加进去了