I'm getting CORS error in server but I'm not getting this error in local. My project is working in local but when I try to connect my socket.io api in server I'm getting this error in console:
Access to XMLHttpRequest at 'https://my-web-site.com.tr/socket.io/?EIO=4&transport=polling&t=ORwXVX_' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My api code is like this:
const cors = require('cors');
const express = require('express');
const app = express();
const http = require('http').Server(app);
const port = process.env.PORT || 3200;
app.use(cors({
origin: '*'
}));
const io = require('socket.io')(http, {
cors: {
origin: '*',
methods: ["GET", "POST"],
allowedHeaders: ["*"],
credentials: true
}
}).of('/abcHub');
io.on('connection', (socket) => {
console.log("Connected: " + socket.id);
socket.on('disconnect', () => {
console.log(`Disconnect ${socket.id}`);
});
});
const server = http.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
My client code like this:
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src = "node_modules/socket.io/client-dist/socket.io.min.js"></script>
<script>
$(document).ready(() => {
const socket = io("https://my-web-site.com.tr/abcHub");
});
</script>
I wonder that why I don't get cors error in local but I get in server? If you have any question, you can ask. Thanks.