Node.js Express、Socket.io和Nginx的CORS问题

Node.js Express、Socket.io和Nginx的CORS问题,node.js,express,sockets,nginx,cors,Node.js,Express,Sockets,Nginx,Cors,我已经修改了一个教程,使一个简单的Socket.io聊天进入节点。它在本地托管时工作,但在将其推送到测试服务器后,我无法接受套接字连接。这似乎是一个与跨源代码相关的问题,尽管我对如何在Nginx中进行路由也有点困惑。遵循相关问题中的建议没有帮助 客户端脚本: var socket=io.connect('http://localhost/socket.io'); Index.js: const express = require('express'); const app = express()

我已经修改了一个教程,使一个简单的Socket.io聊天进入节点。它在本地托管时工作,但在将其推送到测试服务器后,我无法接受套接字连接。这似乎是一个与跨源代码相关的问题,尽管我对如何在Nginx中进行路由也有点困惑。遵循相关问题中的建议没有帮助

客户端脚本:
var socket=io.connect('http://localhost/socket.io');

Index.js:

const express = require('express');
const app = express();
const path = require('path');
const httpServer = require('http').createServer(app);
const io = require('socket.io')(httpServer, {
    cors:true,
    origins:["*"],
    // origins:["http://xxx.xxx.xxx.xxx:8080"],
    // transports: ['websocket'],
});


const views_path = (__dirname + '/views');

app.set('views',views_path);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req,res){
    console.log('render request received');
    res.render('startPage.ejs');
});

io.sockets.on('connection', socket => {
    console.log('connection received.')
    socket.on('username', function(username) {
        socket.username = username;
        io.emit('is_online', socket.username);
    });
    //...
});

httpServer.listen(8080);
server {
    server_name campfire;
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/campfire/html;

    index index.html index.htm index.nginx-debian.html;

    location ^~ /assets/ {
        gzip_static on;
        expires 12h;
        add_header Cache-Control public;
    }

    location / {
        proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8080;
    }
    location /socket.io/ {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'Upgrade';
            proxy_http_version 1.1;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://localhost:8080/socket.io/;
    }
}

nginx站点可用:

const express = require('express');
const app = express();
const path = require('path');
const httpServer = require('http').createServer(app);
const io = require('socket.io')(httpServer, {
    cors:true,
    origins:["*"],
    // origins:["http://xxx.xxx.xxx.xxx:8080"],
    // transports: ['websocket'],
});


const views_path = (__dirname + '/views');

app.set('views',views_path);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req,res){
    console.log('render request received');
    res.render('startPage.ejs');
});

io.sockets.on('connection', socket => {
    console.log('connection received.')
    socket.on('username', function(username) {
        socket.username = username;
        io.emit('is_online', socket.username);
    });
    //...
});

httpServer.listen(8080);
server {
    server_name campfire;
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/campfire/html;

    index index.html index.htm index.nginx-debian.html;

    location ^~ /assets/ {
        gzip_static on;
        expires 12h;
        add_header Cache-Control public;
    }

    location / {
        proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8080;
    }
    location /socket.io/ {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'Upgrade';
            proxy_http_version 1.1;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://localhost:8080/socket.io/;
    }
}


欢迎有任何见解

希望这将有助于解决所有CORS错误

因为它会帮你处理的

const cors = require('cors');
app.use(cors());

文档

希望这将有助于解决所有CORS错误

因为它会帮你处理的

const cors = require('cors');
app.use(cors());

进行此更改的文档修复了此问题:

客户端脚本:
var socket=io.connect()


这种方式使用默认的套接字连接目标。

进行此更改修复了以下问题:

客户端脚本:
var socket=io.connect()


这种方式使用默认的套接字连接目标。

您好,谢谢您的回答。可悲的是,我已经试过了,但它不能解决任何问题。嗨,谢谢你的回答。不幸的是,我已经试过了,但它并没有解决任何问题。