I encountered an error when trying to set up a socket.io server and client. The error I received on the client side was:
Failed to load resource:http://localhost:3000/socket.io/?EIO=4&transport=polling&t=OK-egu3 the server responded with a status of 404 (Not Found)
Server.js code:
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http, {cors: {origin: "*"}});
const path = require('path');
app.set('view engine', 'ejs');
app.use(express.json({extended: true, limit: '1mb'}));
io.on('connection', (socket) => {
console.log("new connection " + socket);
});
app.get("/", (req, res, next) => {
res.render('index'/*, { ioS : io }*/);
});
console.log("Ready");
app.listen(3000);
Index.ejs code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>socket Respons</h1>
</body>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js" integrity="sha384-/KNQL8Nu5gCHLqwqfQjA689Hhoqgi2S84SNUxC3roTe4EhJ9AfLkp8QiQcU8AMzI" crossorigin="anonymous"></script>
<script>
//const io = ;
const socket = io();
console.log(socket);
</script>
</html>
The libraries I have installed are:
- socket.io - socket.io-client - express - ejs