我正在努力将变量传递给模块。
在node.js中,我有以下分布:
node.js服务器(server.js):
// modules =================================================
var express = require('express');
var app = express();
// configuration ===========================================
app.set('port', process.env.PORT || 3000);
var myRoutes = require('./my.router')(app);
// Start the Server ===========================================
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
exports = module.exports = app; // expose app
路由器(my.router.js):
var myCtrl = require('./my.controller');
module.exports = function(app) {
app.get('/api/some', myCtrl.some);
app.get('/api/other', myCtrl.other);
}
控制器(my.controller.js):
exports.some = function(req, res, next) {
res.send('some');
};
exports.other = function(req, res, next) {
res.send('other');
}
这没关系。当我尝试使用socket.io并在调用/ api / some时发出事件时,我遇到了问题。
我需要app对象来创建服务器,所以我将路由器更改为:
var myCtrl = require('./my.controller');
module.exports = function(app) {
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3001);
app.get('/api/some', myCtrl.something);
app.get('/api/other', myCtrl.other);
}
当调用/ api / some时,我将控制器更改为发出事件:
exports.some = function(req, res, next) {
io.sockets.emit('my_event', {});
res.send('some');
};
exports.other = function(req, res, next) {
res.send('other');
}
在控制器内部,我需要io对象来完成这项工作。
对于对node.js有更多了解的人来说听起来很简单,但我无法使其工作。
我需要像app.get('/ api / some',myCtrl.something(io))这样的东西,但这不起作用......
答案 0 :(得分:0)
你不能只拆分你的代码:
var server = require('http').createServer(app);
var io = require('socket.io')(server);
在这个阶段你有你的io,然后
var myCtrl = require('./my.controller')(io);
您将io作为参数传递给控制器,该控制器应该是类似的函数:
/* my.controller.js */
module.exports = function(io) {
some: function(req, res, next) {
io.sockets.emit('my_event', {});
res.send('some');
},
other: function(req, res, next) {
res.send('other');
}
}
或类似的东西。
答案 1 :(得分:0)
// modules =================================================
var express = require('express');
var app = express();
// configuration ===========================================
app.set('port', process.env.PORT || 3000);
var server = require('http').createServer(app);
app.io = require('socket.io')(server); //add io key to app
server.listen(3001);
require('./my.router')(app);
app.Controllers = {};
app.Controllers.myCtrl = require('./my.controller')(app); //pass app to controllers as well (not necessary but you can bootstrap your controllers at the start instead of requiring them in files)
//app.Controllers.anotherCtrl = require('./my.anotherController')(app); //example
// Start the Server ===========================================
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
exports = module.exports = app; // expose app
控制器
module.exports = function(app) {
var Controller = {};
var io = app.io;
Controller.some = function(req, res, next) {
io.sockets.emit('my_event', {});
res.send('some');
};
Controller.other = function(req, res, next) {
res.send('other');
}
return Controller;
};
路线
module.exports = function(app) {
var myCtrl = app.Controllers.myCtrl;
app.get('/api/some', myCtrl.some);
app.get('/api/other', myCtrl.other);
}
答案 2 :(得分:0)
您可以使用请求对象在不同路由之间传递数据。
module.exports = function(app) {
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3001);
//middleware used to assign 'io' to the request object
function passIoObject(req, res, next) {
req.myIo = io;
next()
}
// either use app.use(passIoObject) if io should be available for all following routes
// or pass it only to specific routes
app.get('/api/some', passIoObject, myCtrl.something);
app.get('/api/other', myCtrl.other);
}
在您的主控制器中,您可以使用以下方式访问它:
exports.some = function(req, res, next) {
req.myIo.sockets.emit('my_event', {});
res.send('some');
}
除此之外,你应该避免像:
这样的结构module.exports = function(app) {
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3001);
app.get('/api/some', myCtrl.something);
app.get('/api/other', myCtrl.other);
}
代码越大,可维护性问题就越多,因为您总是需要查看require('./my.router')
文件以查看代码处理哪些路径/前缀。
而是这样写:
module.exports = function(app) {
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3001);
var router = express.Router();
router.get('/some', myCtrl.something);
router.get('/other', myCtrl.other);
return router;
}
在server.js中
var myRoutes = require('./my.router')(app);
app.use('/api', myRoutes);