我在脚下射击自己:
我想在我的Express应用程序中的app和req对象上提供config,core和mean.
我正在使用不在4.x API中的属性.我还应该知道什么?
// express.js
module.exports = function(db, config, meanModules) {
var app = express();
// ...
// Get mean-core
var core = require('meanjs-core')(db, config);
// Attach config, core, and modules to app <==== POSSIBLE FOOT SHOOTING
app.config = config;
app.core = core;
app.mean = meanModules;
// Middleware to adjust req
app.use(function(req, res, next) {
// Add config, core, and modules to all requests <==== POSSIBLE FOOT SHOOTING
req.config = config;
req.core = core;
req.mean = meanModules;
next();
});
// ...
}
解决方法:
我建议将一个属性附加到可能永远不会发生冲突的应用程序,并从那里访问所有内容,例如app.myLibrary.
app.myLibrary = {config: config, core: core, mean: meanModules};
并从路由/中间件中访问app.myLibrary:
req.app.myLibrary
除非中间件中发生的动态因每个请求而异,否则最好只使用req.app.myLibrary访问它.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。