I'm in the process of learning Express - and thinking of the best place to save config style data. Options available are either in app.locals or app.set (settings)... so:
app.locals({ config: { name: 'My App', domain: 'myapp.com', viewPath: __dirname+'/views', viewEngine: 'jade' port: 3000 } }); app.set('view engine', app.locals.config.viewEngine || 'jade');
This would also allow me to use the following in my views:
<title>#{config.name}</title> // <title>My App</title>
Or the alternative is to use app.set like so:
app.set('name', 'My App'); app.set('domain', 'myapp.com');
... and then use this in the view:
<title>#{settings.name}</title>
I know both methods work, but I'm struggling to determine which is the better method to use. At the moment I'm leaning towards using app.locals, with the extra 'app' namespace as I believe there would be less chance of conflicts with future updates and other modules if using app.set.
The app. locals object defines the properties that are local variables inside an application. Once the value of app. locals property is set, it persists throughout the life of the application.
set() have predetermined effects that are described in the Express doc and work like configuration options. app. use() registers a middleware callback that will be part of the request handler chain for incoming http requests.
locals is an object that contains the local variables for the response which are scoped to the request only and therefore just available for the views rendered during that request or response cycle.
The app. set() function is used to assigns the setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server.
Wow, all of the answers are wrong, so let me give it a try. Despite what others say assinging to the app.local argument is different from using app.set(). Watch,
app.js app.locals.foo = 'bar'; app.set('baz', 'quz'); index.jade block content dl dt app.locals.foo = 'bar'; dd \#{settings.foo} = #{settings.foo} dd \#{foo} = #{foo} dt app.set('baz', 'quz') dd \#{settings.baz} = #{settings.baz} dd \#{baz} = #{baz}
If you ran this code, what you would see is,
app.locals.foo = 'bar'; #{settings.foo} = #{foo} = bar app.set('baz', 'quz') #{settings.baz} = quz #{baz} =
The reason for this is setting app.locals
sets attributes of the object that the view uses as its environment; what the view will read from without qualification. Conversely, app.set sets attributes on app.locals.settings
. You can verify this if you clobber app.locals.settings
in the above with app.locals.settings = {}
, which will make #{settings.baz}
undefined.
So which do you use? If it's not an app setting based on the response (res.set
) or global configuration (app.set
), use the direct write to app.locals
.
All properties of app.locals
are available in templates. Using app.set
assigns properties to app.locals.settings
, which is used for global application settings and is inherited by mounted applications. For example:
var app1 = express(), app2 = express(); app1.set('inheritable', 'foo'); app1.locals.notInheritable = 'bar'; app1.use('/mount', app2); app2.get('inheritable') === 'foo'; // true app2.locals.notInheritable === 'bar'; // false
So it's really a question of preference and whether or not you're mounting applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With