he seguido un tutorial para hacer un chat en linea pero a la hora de usar socket.io no lo encuentra.
Este es el tutorial que utilicé
https://www.freecodecamp.org/news/simple-chat-application-in-node-js-using-express-mongoose-and-socket-io-ee62d94f5804/
Este es mi server.js
var express = require('express');
var bodyParser = require('body-parser')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoose = require('mongoose');
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}))
var Message = mongoose.model('Message',{name:'String',message:'String'});
var dbUrl = 'mongodb+srv://dayton:[email protected]/test?retryWrites=true';
app.get('/messages', (req, res) => {
Message.find({},(err, messages)=> {
res.send(messages);
})
})
app.get('/messages/:user', (req, res) => {
var user = req.params.user
Message.find({name: user},(err, messages)=> {
res.send(messages);
})
})
app.post('/messages', async (req, res) => {
try{
var message = new Message(req.body);
var savedMessage = await message.save()
console.log('saved');
var censored = await Message.findOne({message:'badword'});
if(censored)
await Message.remove({_id: censored.id})
else
io.emit('message', req.body);
res.sendStatus(200);
}
catch (error){
res.sendStatus(500);
return console.log('error',error);
}
finally{
console.log('Message Posted')
}
});
io.on('connection', () =>{ console.log('a user is connected')});
mongoose.connect(dbUrl, (err) =>{ console.log("Conexion establecida",err);});
var server = app.listen(3000, () => {
console.log('server is running on port', server.address().port);
});
este es mi index.html
<!DOCTYPE html5>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
</head>
<body>
<div class='container'>
<br>
<div class=''>
<h1 class='display-4'>Send Message</h1>
<br>
<input id='name' class='form-control' placeholder='Name'>
<br>
<textarea id='message' class='form-control' placeholder='Your Message Here'></textarea>
<br>
<button id='send' class='btn btn-success'>Send</button>
</div>
<div id='messages'>
</div>
</div>
<script>
var socket = io();
$(() => {
$("#send").click(()=>{
sendMessage({
name: $("#name").val(),
message:$("#message").val()
});
});
getMessages();
}
);
socket.on('message', addMessages);
function addMessages(message){
$("#messages").append(`<h4>${message.name}</h4><p>${message.message}</p>`);
}
function getMessages(){
$.get('http://localhost:3000/messages', (data) => {data.forEach(addMessages);});
}
function sendMessage(message){
$.post('http://localhost:3000/messages', message);
}
</script>
</body>
</html>
Todo funciona perfectamente, todo se inserta bien en la base de datos, pero no refresca la pagina para ver los mensajes enviados y cuando me fijo en la consola sale el mensaje
http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1559146749555-2
404 Not Found
Cannot GET /socket.io/
y la pagina no se recarga