您的位置:首页 > Web前端 > JavaScript

Express.js中的locals用法

2016-06-25 10:07 417 查看
在Express.js 4.x框架中存在两个locals定义。一个是Express本身带有的locals属性(Object类型),另一个是Response的locals属性(Object类型)。在本文中,我们来讨论一下Express本身带有的locals属性用法。先看历史:在Express.js 2.x时代,还没有出现locals,而是使用app.helpers 和app.dynamicHelpers 。在express2.X中,你可以使用它们分别作为静态/动态视图助手工具。有一个简单的例子,如下:
app.helpers({
<span style="white-space:pre"></span>inspect: function(obj) {
<span style="white-space:pre"></span>return util.inspect(obj, true);
}
});
如今,Express本身带有的locals属性把上述两个Helper替换掉了。官方说法是:The
app.locals
object is a JavaScript object, and its properties are local variables within the application.也就是说,你可以在Express程序的生命周期内为其locals这个对象属性定义任意你喜欢的属性(当然包括函数),然后在你需要的地方使用。但是,有重要提示与说明,如下:Once set, the value of
app.locals
properties persist throughout the life of the application, in contrast with res.locals properties that are valid only for the lifetime of the request.You can access local variables in templates rendered within the application. This is useful for providing (1)helper functions to templates, as well as (2)app-level data. Note, however, that you CANNOT access local variables in middleware.上面的E文不用我翻译了,翻译得拙劣反而会歪曲的原文主旨。因此,作者在新版本中作上述更新(当然是替换)是极有道理的。因此,在新版本中(时下最流行的自然是4.X),locals的作用进一步扩展,而不仅限于作为模板视图的工具函数使用。值得注意的是,3.X与4.X中也有一些不同(摘录http://blog.csdn.net/ling369523246/article/details/49487509的例子如下):
............但是在express 3.x后 这两个方法被废止。3.x 使用的是
app.locals({key2: value1,key2: value2})
就有如下:
app.locals({inspect: function(obj) {return util.inspect(obj, true);}});
但是express4.x后变化就很大了
app.locals.key1 = value1;app.locals.key2 = value2;
如下:app.locals.inspect=function(obj){return util.inspect(obj, true);}
至于在.jade等模板中如何使用locals中定义的属性,在stackoverflow中有相应的解决,而且来自express.js作者TJ Holowaychuck,示例及说明如下。下面这一段不是来自EXPRESS.JS作者(尽管使用的是3.0,现在不再有):
Here is an example using a fresh installation of express v3.0.0rc4app.js:
app.use(function(req, res, next){res.locals.variable="Hello locals from Response!";next();})app.configure(function(){app.set('port', process.env.PORT || 3000);app.set('views', __dirname + '/views');app.use(express.favicon());app.use(express.logger('dev'));app.use(express.bodyParser());app.use(express.methodOverride());app.use(express.static(path.join(__dirname, 'public')));});
index.jade:
extends layoutblock contenth1= titlep Welcome to #{title}p=variable
注意,上面例子中使用了Response对象中定义的locals,运行结果如下图所示:来自express.js作者TJ Holowaychuck的上述使用忠告则强调了使用顺序:
the one key thing you have to remember (currently), is that all of theroutes (app.get, app.put, etc)are part of a routing middleware called app.router (this is aninstance of a Router). So app.use()works with individual middleware, so if you call it between twoapp.get() calls for example it'snot actually added between them.so you might want to do something like: (这里使用示例说明使用顺序的问题)app.use(loadViewData);app.use(app.router);app.get('/.....that way it's before the routes.I should note that I've played with each app.{get,put,...}() callbeing itsown middleware, which is nice in a lot of ways but I need to improvetheperformance of that solution before considering it
关于Express中的locals属性用法讨论至此,欢迎一起作进一步探讨......本文出自 “青峰” 博客,请务必保留此出处http://zhuxianzhong.blog.51cto.com/157061/1792781
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: