Reputation: 89
i wanted to build socket.io server and when setting up the client i have an error (on the client side)
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)
code: server.js:
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:
<!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>
i have installed the following liblaries:
socket.io socket.io-client express ejs
Upvotes: 0
Views: 574
Reputation: 5303
You're not initializing the client correctly. Your server is at localhost:3000, but the client doesn't know that, because you haven't told it where to look.
Looking at the socketio documentation, you need to pass the URL of the server's socket endpoint:
const URL = "http://localhost:3000";
const socket = io(URL, { autoConnect: false });
It's worth mentioning that the error message indicates this: "not found." It's not a verbose error message, but it's a valuable debugging signal.
Upvotes: 1