app.use(require(‘./controllers/sign’).auth_user); //在app.js 中引入此中间件 具体方法的定义如下(主要是获取未读消息) exports.auth_user = function (req, res, next) { console.log(req.path); if (req.session.user) { if (config.admins.hasOwnProperty(req.session.user.name)) { req.session.user.is_admin = true; } Message.getMessagesCount(req.session.user._id, function (err, count) { if (err) { return next(err); } req.session.user.messages_count = count; if (!req.session.user.avatar_url) { req.session.user.avatar_url = getAvatarURL(req.session.user); } res.local('current_user’, req.session.user); return next(); }); } else { var cookie = req.cookies[config.auth_cookie_name]; if (!cookie) { return next(); } var auth_token = decrypt(cookie, config.session_secret); var auth = auth_token.split(‘\t’); var user_id = auth[0]; User.getUserById(user_id, function (err, user) { if (err) { return next(err); } if (user) { if (config.admins.hasOwnProperty(user.name)) { user.is_admin = true; } Message.getMessagesCount(user._id, function (err, count) { if (err) { return next(err); } user.messages_count = count; req.session.user = user; res.local('current_user’, req.session.user); return next(); }); } else { return next(); } }); } };
我在exports.auth_user 中console.log(req.path),结果输入地址,敲回车,要经过n次请求,包括所有的js和css 这是是存在性能问题么?
追加:刚看了一下把app.use('/public’, express.static(staticDir));放在app.use(require(‘./controllers/sign’).auth_user); 的前面就不会出现这个问题