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中,你可以使用它们分别作为静态/动态视图助手工具。有一个简单的例子,如下:
至于在.jade等模板中如何使用locals中定义的属性,在stackoverflow中有相应的解决,而且来自express.js作者TJ Holowaychuck,示例及说明如下。下面这一段不是来自EXPRESS.JS作者(尽管使用的是3.0,现在不再有):
注意,上面例子中使用了Response对象中定义的locals,运行结果如下图所示:来自express.js作者TJ Holowaychuck的上述使用忠告则强调了使用顺序:
关于Express中的locals属性用法讨论至此,欢迎一起作进一步探讨......本文出自 “青峰” 博客,请务必保留此出处http://zhuxianzhong.blog.51cto.com/157061/1792781
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.localsobject is a JavaScript object, and its properties are local variables within the application.也就是说,你可以在Express程序的生命周期内为其locals这个对象属性定义任意你喜欢的属性(当然包括函数),然后在你需要的地方使用。但是,有重要提示与说明,如下:Once set, the value of
app.localsproperties 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);} |
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 |
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 |
相关文章推荐
- js将滚动条滚动到指定位置的简单实现方法
- 在外部js文件中获取web工程名
- JSP简单运行原理----------九大隐式对象和四大域对象
- 编写一个jsp程序,实现用户登录,当用户输入的用户或密码错误时,将页面重定向到错误提示页,并在该页面显示30秒后 自动回到用户登录界面
- js删除局部变量的实现方法
- json解析
- javascript 广告移动特效的实现代码
- javascript简单特效,js学习
- NSJSONSerialization
- JSP用户登录页面
- js选项卡
- js节点属性
- .net 使用Json(),maxJsonLength属性设置的值问题
- JS中的进制转换以及作用
- Simple MVVM中利用JsonConvert 序列化Model实体类
- javascript 广告移动特效的实现代码
- js删除局部变量的实现方法
- js将滚动条滚动到指定位置的简单实现方法
- js操作XML文件的实现方法兼容IE与FireFox
- jsp 分页 tag