簡體   English   中英

express和socket.io:將var io傳遞給路由器

[英]express and socket.io: pass var io to router

我花了最后幾個小時來弄清楚如何將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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM