0
I’m trying to chat in Ode js, but when I start the server.js
and enter the site, an error appears:
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NfTIQ3t 400 (Bad Request)
This mistake keeps repeating itself, every 3 seconds it makes the same mistake again. Can anyone help me? I don’t know what’s going on.
Here are the codes of index.html
and server.js
:
//server.js
const express = require('express');
const path = require('path');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use('/', (req, res) => {
res.render('index.html')
});
io.on('connection', socket => {
console.log(`Socket conectado: ${socket.id}`)
})
server.listen(3000)
console.log('App iniciado')
<!DOCTYPE html>
<!--index.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>Chat GG</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id="chat">
<input type="text" name="username" placeholder="Digite seu username">
<div class="messages"></div>
<input type="text" name="message" placeholder="Digite sua mensagem">
<button type="submit">Enviar</button>
</form>
<script type="text/javascript">
var socket = io('http://localhost:3000');
</script>
</body>
</html>