当前位置:  开发笔记 > 编程语言 > 正文

express和socket.io:将var io传递给路由器

如何解决《express和socket.io:将vario传递给路由器》经验,为你挑选了1个好方法。

我花了最后几个小时搞清楚如何将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;

但现在我得到路由器未定义的错误



1> adeneo..:

你可以通过它

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


您需要像这样最后返回-> https://jsfiddle.net/7gja3ryt/
推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有