I'm running a Node.js application with HTTPS and Socket.IO on an AWS EC2 instance, behind NGINX. The REST APIs work fine, but the WebSocket connection to Socket.IO fails with a 400 Bad Request.
const app = express();
const { createServer } = require("http"); // Use HTTP not HTTPS
const { Server } = require("socket.io");
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: ['https://abcdDomain.com', 'http://localhost:3000'],
methods: ["GET", "POST"],
credentials: true
}
});
io.on("connection", (socket) => {
console.log("Socket connected:", socket.id);
});
httpServer.listen(3001, () => {
console.log("HTTP server running on port 3001");
});