Reputation: 99
I don't understand what is the need of using app.set and app.get if all we want is access the variable.
app.set('title','My title');
var title = app.get('title);
What I could do instead is directly set My title to title variable.
var title = 'My title';
Here's the question I have gone through before questioning here on stack overflow
Upvotes: 0
Views: 636
Reputation: 560
You're correct in the sense that you may not find it useful to set properties like app.set('title', 'my website')
on the app object (unless for some reason you reference this property elsewhere in your code, or a package you've installed expects this property to be set).
You must keep in mind however that setting other specific property names will do more than what meets the eye, and may change the way that your application behaves. For example, setting app.set('trust proxy', true)
will adjust how the req.ip
property is populated by providing instead the value of the x-forwared-for
header of requests (which is useful if your application is behind a reverse-proxy like CloudFlare etc).
A list of these special properties per ExpressJS documentation can be found here: http://expressjs.com/en/api.html#app.set
Upvotes: 1
Reputation: 144
app.set() and app.get() are one method for managing global variables in node.js. Variables have local scope inside a file, if you want to access them from another one then you will need to export them from the origin file and import them in the one that is going to use their values. The opposite happens with app.set() and app.get(), you can export the app instance and use the values associated with it wherever you want.
Upvotes: 1