I am using socketIO with express.
In my project I have a login page and a home page. When i do successful login i navigate to localhost:3000/home where I get this error :
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1418187395022-0 404 (Not Found)
I did not do any modification in my app.js(project created by express ).
Index.js :
var express = require('express');
var router = express.Router();
var http = require('http');
var fs = require('fs');
var io = require('socket.io')(http);
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/home', function(req, res) {
res.render('home', { title: 'Express' });
});
io.on('connection', function(socket){
console.log("User Connected");
socket.on('chat message', function(msg){
io.emit('chat message', msg);
console.log("Message");
});
socket.on('disconnect', function(msg){
console.log("User DisConnected");
});
});
router.post('/authenticate', function(req, res) {
fs.readFile("./public/Verification/Users.json", "utf8", function (err, data) {
if (err)
console.log(err);
else{
var result = JSON.parse(data);
for(var i=0;i<result.Users.length;i++){
if(req.body.username == result.Users[i].username && req.body.password == result.Users[i].password){
console.log("Success!!!!!!!!!!!!!!");
res.location("home");
res.redirect("home");
}
}
}
});
});
module.exports = router;
I get this error whenever I navigate to localhost:3000/home. I am new to both socketIO and express. Please tell me if I am missing something.
Thanks
EDIT:
In my layout.jade i defined socketio like this:
script(src='https://cdn.socket.io/socket.io-1.2.0.js')
If you're running Express 4, it appears to me like you're missing the lines of code:
That will start your web server and set it to port 3000. See the bone simple Express 4 app here: http://expressjs.com/4x/api.html
And, then to start up socket.io, you would add:
And, there is no need for this line:
Since I was battling with this issue for the past 5 hours. And this is the first result in google.
Of course its 2015 and some of the code has changed.
I think currently the best start of the index.js code is:
The order is highly important.
My problem was, that everything was installed properly and node was listening. However the example.com/socket.io/socket.io.js and example.com/socket.io/?eio=... was 404 not found. However, when I tried example.com:3000/socket.io/socket.io.js everything worked. So now was the question, how to change the getJSON query to have the port part.
So the solution was to edit the main.js, that is being added to the index.html, where your chat is. There is:
to be replaced with
This corrects the getJSON queries URL and adds the correct port and no more 404. I hope it helps someone in the future.
This is how i solve it on server side (2018):
On client side
It looks easy but I spent hours before I found the solution since the solutions above didn't work for me.
just call the listen of the server and with the callback I passed the server instance to module.
part of the
bin/www
file from express appand this is how my socket module
socket.js
all of the socket listeners and emits managing from here.