How to resolve a Socket.io 404 (Not Found) error?
Solution 1:
If you're running Express 4, it appears to me like you're missing the lines of code:
var app = express();
var server = app.listen(3000);
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:
var io = require('socket.io').listen(server);
And, there is no need for this line:
var http = require('http');
Solution 2:
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:
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
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:
var socket = io();
to be replaced with
var socket = io.connect('http://example.com:3080');
This corrects the getJSON queries URL and adds the correct port and no more 404. I hope it helps someone in the future.
Solution 3:
Please change your config to this
var express = require('express');
// App setup
var app = express();
var socket = require('socket.io')
var server = app.listen(4000, function(){
console.log('listening for requests on port 4000,');
});
let io = socket(server)
io.on('connection', function(socket){
console.log(`${socket.id} is connected`);
});