Почему не работает CORS socket io + express на now sh?

не работает socket io.

Вот ошибка:

Access to XMLHttpRequest at 'https://ufcbot-server.now.sh/socket.io/?EIO=3&transport=polling&t=N2Lw0f1' from origin 'https://ufcbot.now.sh' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
polling-xhr.js:267 GET https://ufcbot-server.now.sh/socket.io/?EIO=3&transport=polling&t=N2Lw0f1 net::ERR_FAILED

Вот код:

const pool = require(`./db`)

const express = require(`express`);
const app = require(`express`)();
const server = require(`https`).createServer(app);
const io = require('socket.io')(server);

app.use(express.static(`public`));
server.listen(80);

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');

    // authorized headers for preflight requests
    // https://developer.mozilla.org/en-US/docs/Glossary/preflight_request
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();

    app.options('*', (req, res) => {
        // allowed XHR methods  
        res.header('Access-Control-Allow-Methods', 'GET, PATCH, PUT, POST, DELETE, OPTIONS');
        res.send();
    });
});

io.origins(['https://ufcbot.now.sh'])

io.on('connection', (client) => {
    client.on('getRows', (id) => {
        pool.query(`SELECT * FROM users WHERE uid = ${id}`, (err, res) => {
            if (err) throw err;
            client.emit(`rowsData`, res);
        })
    });

    client.on(`createAccount`, (uid) => {
        pool.query(`INSERT INTO users (uid) VALUES (${uid})`, (err, res) => {
            if (err) throw err;
            client.emit(`createAccountData`, res);
        })
    })
});
    // Pass to next layer of middleware
    next();
});

app.use(express.static(`public`));
server.listen(8000);

io.on('connection', (client) => {
    client.on('getRows', (id) => {
        pool.query(`SELECT * FROM users WHERE uid = ${id}`, (err, res) => {
            if (err) throw err;
            client.emit(`rowsData`, res);
        })
    });

    client.on(`createAccount`, (uid) => {
        pool.query(`INSERT INTO users (uid) VALUES (${uid})`, (err, res) => {
            if (err) throw err;
            client.emit(`createAccountData`, res);
        })
    })
});

Помогите пожалуйста


Ответы (0 шт):