为了使“app.js”与其他功能分开,我重组了我的节点应用程序,如下所示:
应用程序.js
// declare dependencies, etc
var server = http.createServer(app);
var controllers = require('./controllers');
controllers.set(app, server, passport);
控制器/index.js
var socketController = require('./socketController.js');
var passportController = require('../config/passport.js');
var routeController = require('./routeController.js');
var routes = require('../routes');
// server side data
var sessions = [];
var userPrivileges = [];
module.exports.set = function(app, server, passport) {
/**
* Routes
*/
// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
routeController.set(app, passport, sessions, userPrivileges);
socketController.set(app, server, sessions, userPrivileges);
passportController.set(passport);
};
控制器/routeController.js
module.exports.set = function(app, passport, sessions, userPrivileges) {
// route to test if the user is logged in or not
app.get('/loggedin', function(req, res) {
res.send(req.isAuthenticated() ? req.user : '0');
});
};
我的问题是当我调用 GET/loggedin
它返回一堆 HTML - 似乎 bodyparser 没有被激活。我尝试过在其中声明 bodyparser ,routeController.js
但这似乎没有帮助。我将如何在这些不同的文件中共享这个中间件?