William G William G - 2 months ago 22
Node.js Question

Express.js 'socket.io/socket.io.js 404'

I'm trying to incorporate a live user count on my website http://clickthebutton.herokuapp.com which uses Express JS. When trying to install socket.io I get this error:

GET /socket.io/socket.io.js 404 1.911 ms - 1091



Yes, I have used 'npm install socket.io --save'


I have looked around for answers, tried them and nothing has helped. If anyone thinks they know what's happening, a response would be well appreciated! Here's my code:

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var io = require('socket.io')(app);

var routes = require('./routes/index');
var users = require('./routes/users');

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

var server = app.listen(app.get('port'), function () {
console.log('server listening on port ' + server.address().port);
})
var io = require('socket.io').listen(server);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

module.exports = app;


index.ejs

<!-- socket.io -->
<script src="socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect();
</script>
</div>
</center>
</body>


routes/index.js

var express = require('express');
var router = express.Router();
var redis = require('redis'), client = redis.createClient(secret, "secret"); client.auth("secret", function() {console.log("Connected!");});
var app = express();
var server = require('http').Server(app);
var http = require('http');

var io = require('socket.io').listen(server);

io.on('connection', function(socket){
console.log('a user connected');
console.log(Object.keys(io.sockets.connected));
});


io.on('connection', function (socket) {
console.log('Socket.io connected!')
});



I only copy/pasted the code that referenced socket.io


Thanks!

Answer

In app.js alone you're creating three instances of socket.io, and once again in routes/index.js. There should be only one throughout your entire app.

A common setup for Express looks like this:

var app    = require('express')();
var server = app.listen(app.get('port'), function () {
  console.log('server listening on port ' + server.address().port);
});
var io = require('socket.io')(server);

If you need to reference either app, server or io in other files, you need to pass it as a variable.

EDIT: because you're using an express-generator based setup, the HTTP server will be created in bin/www and not app.js. In that case, you also need to create the socket.io server there:

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
var io = require('socket.io')(server); // add this line

Again, if you need to reference io somewhere else in your codebase, you need to pass it along from there.