我花了最后几个小时搞清楚如何将socket.io与快速路由器结合使用我试图让socket.io工作.
这是我使用的包:
"express": "~4.9.0", "ejs": "~0.8.5", "socket.io":"~1.3.3"
我的app.js:
// loading modules var express = require('express'); var http = require('http'); var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); //routers var secretRouter = require('./routes/secret'); // Setup serving static assets app.use(express.static('public')); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); // Setup initial routing - index app.use('/', secretRouter); // Start the server server.listen(3000, function(){ console.log('App listening at http://localhost:3000'); });
我的秘密路由器:
var express = require('express'); var router = express.Router(); // Show the login form router.get('/login', function(req, res, next) { res.locals.req = req; res.render('secret/login'); }); // If someone tries to login router.post('/login', function(req, res, next) { username = req.body.username; password = req.body.password; req.getConnection(function(err, connection){ connection.query('SELECT * FROM users WHERE name = ? AND password = ?',[username, password], function(err, results) { res.locals.data = results; if (results.length > 0){ req.session.regenerate(function(){ req.session.login = true; req.session.username = req.body.username; res.redirect(req.baseUrl); }); }else { console.log('wachtwoord of naam is fout'); res.redirect(req.baseUrl); } }); }); }); module.exports = router;
一切正常.
我可以添加一个io连接功能,让我知道是否有人在app.js中打开了这样的网站:
io.on('connection', function(socket){ console.log('a user connected'); });
但是现在我没有在app.js中使用上面的代码,而是想在秘密路由器中使用它.但只是将它放入秘密路由器将给我一个错误,说io没有定义.
所以我需要将io var传递给路由器.
编辑:
在我的app.js中,我改变了:var secretRouter = require('./ routes/secret');
至:
var secretRouter = require('./routes/secret')(io);
在我的秘密路由器中:
module.exports = function(io) { // catch here var express = require('express'); var router = express.Router(); return router; } // Show the login form router.get('/login', function(req, res, next) { res.locals.req = req; res.render('secret/login'); }); // If someone tries to login router.post('/login', function(req, res, next) { username = req.body.username; password = req.body.password; req.getConnection(function(err, connection){ connection.query('SELECT * FROM users WHERE name = ? AND password = ?',[username, password], function(err, results) { res.locals.data = results; if (results.length > 0){ req.session.regenerate(function(){ req.session.login = true; req.session.username = req.body.username; res.redirect(req.baseUrl); }); }else { console.log('wachtwoord of naam is fout'); res.redirect(req.baseUrl); } }); }); }); module.exports = router;
但现在我得到路由器未定义的错误
你可以通过它
var io = require('socket.io').listen(server); var secretRouter = require('./routes/secret')(io); // pass here app.use('/', secretRouter);
然后在秘密路由器中捕获它
module.exports = function(io) { // catch here var express = require('express'); var router = express.Router(); // ...routes return router; }