The locals usage in express.js

Source: Internet
Author: User

There are two locals definitions in the Express.js 4.x framework. One is the Locals property (object type) with express itself, and the other is the locals property of response (object type).


In this article, let's discuss the use of the locals attribute with express itself.


First Look at history: in the Express.js 2.x era, there is no locals, but the use of app.helpers and App.dynamichelpers. In Express2. x, you can use them separately as the static/Dynamic View Helper tool.


There is a simple example, as follows:

App.helpers ({
<span style= "White-space:pre" ></span>inspect:function (obj) {
<span style= "White-space:pre" ></span>return util.inspect (obj, true);
}
});


Today, express itself has the locals attribute to replace the two helpers. The official statement is:


THE&NBSP, app.locals  object is a JavaScript object, and its properties are local variables within the application.


That is, you can define any attributes (including functions) that you like in the Express program's lifetime for its locals, and then use it where you need it. However, there are important hints and instructions, as follows:


Once Set, the value of app.locals properties persist throughout the life of the application, in contrast with res.locals Properties that is valid only for the lifetime of the request.

You can access local variables in templates rendered within the application. This is useful- providing (1) helper functions to templates, as well as (2)app-level data. Note, however, that's cannot access local variables in middleware.


The above e-text does not need me to translate, the translation is clumsy but will distort the original theme.


Therefore, it is very reasonable for the author to make the above update (and, of course, replace) in the new version. Therefore, in the new version (nowadays the most popular nature is 4. X), the role of locals is further expanded, not limited to the use of tool functions as template views.

It is noteworthy that there are some differences between 3.X and 4.X (excerpt http://blog.csdn.net/ling369523246/article/details/49487509 examples below):

............

But after the Express 3.x these two methods were abolished.

3.x is using the


App.locals ({
Key2:value1,
Key2:value2
})
There are the following:
App.locals ({
Inspect:function (obj) {
return Util.inspect (obj, true);
}
});


But it's a big change after express4.x.

as follows: App.locals.inspect=function (obj) {
return Util.inspect (obj, true);
}


As for how to use the properties defined in locals in the. Jade template, there is a corresponding resolution in StackOverflow, and from express.js author TJ Holowaychuck, examples and descriptions are as follows.

The following paragraph is not from the Express.js author (although using 3.0, which is now no longer available):

Here's an example using a fresh installation of express V3.0.0RC4

App.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 content h1= title P Welcome to #{title} p=variable


Note that the locals defined in the response object is used in the example above, and the result is as follows:


650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/83/3F/wKioL1dt7aXD8_XKAADYvYaRPP4038.png-wh_500x0-wm_3 -wmp_4-s_1789249872.png "title=" screenshot 2016-06-25 a.m. 10.32.46.png "alt=" wkiol1dt7axd8_xkaadyvyarpp4038.png-wh_50 "/ >



The above usage advice from Express.js author TJ Holowaychuck emphasizes the order of use:

The One key thing you has to remember (currently).
routes (App.get, app.put, etc)
Was part of a routing middleware called App.router
instance of a Router). So App.use ()
works with individual middleware, so if you call it between
App.get () calls for example it ' s
Not actually added between them.

So -might want to does something like: (Here's an example of using the order problem)

App.use (Loadviewdata);
App.use (App.router);

App.get ('/...

That's the it ' s before the routes.

I should note that I ' ve played with each App.{get,put,...} () Call
being its
own middleware, which is nice in a lot of ways and I need to improve
the
performance of that solution before considering it



About locals attribute usage in Express let's talk about it.






This article is from the "Green Peak" blog, please make sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1792781

The locals usage in express.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.