我正在尝试将socket.io连接到我的路由系统,但是尽管我已经读过多种方法来实现此目的,但是我不确定设置的最佳方法,因为我与服务器的隔离度为3度文件和需要socket.io配置的路由文件。我应该将其作为构造函数参数传递吗?
我尝试传递参数//Routing app.use(controllers)(io);
,但是路由器模块遇到错误,因此我认为我没有正确设置它。
错误:var search = 1 + req.url.indexOf('?'); TypeError: Cannot read property 'indexOf' of undefined
这是我的设置。
app.js:
var express = require('express');
var app = express();
//Port Setting
var port = process.env.PORT || 3000;
//Server Port
var server = app.listen(port);
//External Packages
var io = require('socket.io')(server);
var redisAdapter = require('socket.io-redis');
var controllers = require('./app/controllers/routes');
//Routing
app.use(controllers);
routes.js:
var express = require('express');
var router = express.Router();
router.use('/app', require('./app-routes'));
router.use('/api', require('./api-routes'));
module.exports = router;
app-routes.js(我要在其中配置socket.io):
var express = require('express');
var app = express();
var appRoutes = express.Router();
/*==== / ====*/
appRoutes.route('/')
.get(activityFeed.load)
.post(activityFeed.filter);