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

时间:2016-09-24 15:01:25

标签: javascript node.js socket.io

我正在尝试将实时用户数纳入我使用Express JS的网站http://clickthebutton.herokuapp.com。尝试安装socket.io时出现此错误:

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

是的,我使用过'npm install socket.io --save'

我环顾四周寻找答案,尝试过它们并没有任何帮助。如果有人认为他们知道发生了什么,那么回复将会很受欢迎!这是我的代码:

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>

路由/ 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!')
});
  

我只复制/粘贴引用socket.io

的代码

谢谢!

1 个答案:

答案 0 :(得分:1)

仅在app.js您就可以在socket.io创建三个个实例,并再次在routes/index.js中创建。整个应用程序中应该只有一个。

Express的常见设置如下所示:

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);

如果您需要在其他文件中引用appserverio,则需要将其作为变量传递。

编辑:由于您正在使用基于express-generator的设置,因此将在bin/www而非app.js中创建HTTP服务器。在这种情况下,您还需要在那里创建socket.io服务器:

/**
 * Create HTTP server.
 */

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

同样,如果您需要在代码库中的其他位置引用io,则需要从那里传递它。