admin管理员组文章数量:1389779
Trying to find a solution, i found 4 solutions, but i would like to know which one is better / best practice. And why! :P
1. Using req.app.get()
// app.js
app.set('settings', { domain: '' });
// other file
console.log(req.app.get('settings'));
2. Using req.app.settings (similar to above)
// app.js
app.set('settings', { domain: '' });
// other file
console.log(req.app.settings.settings);
3. Exporting app object so i can access app.get() without req object
// app.js
app.set('settings', { domain: '' });
module.exports = app;
// other file
var app = require('../app');
console.log(app.get('settings'));
4. Using a global variable. Probably bad idea but... isn't "settings" a global thing anyway? (I can avoid reusing it so i dont get scope problems)
// app.js
settings = { domain: '' };
// other file
console.log(settings);
Trying to find a solution, i found 4 solutions, but i would like to know which one is better / best practice. And why! :P
1. Using req.app.get()
// app.js
app.set('settings', { domain: 'http://www.example' });
// other file
console.log(req.app.get('settings'));
2. Using req.app.settings (similar to above)
// app.js
app.set('settings', { domain: 'http://www.example' });
// other file
console.log(req.app.settings.settings);
3. Exporting app object so i can access app.get() without req object
// app.js
app.set('settings', { domain: 'http://www.example' });
module.exports = app;
// other file
var app = require('../app');
console.log(app.get('settings'));
4. Using a global variable. Probably bad idea but... isn't "settings" a global thing anyway? (I can avoid reusing it so i dont get scope problems)
// app.js
settings = { domain: 'http://www.example' };
// other file
console.log(settings);
Share
Improve this question
asked Mar 23, 2014 at 3:01
Félix SanzFélix Sanz
1,8924 gold badges16 silver badges27 bronze badges
1
- If I were to rate, 1, 2, 3, 4. Don't do 4 at all. – thefourtheye Commented Mar 23, 2014 at 3:27
1 Answer
Reset to default 2Brief opinion:
1. Using req.app.get()
Here, we are defining accessor method (getter/setter) for global properties. So its syntactically correct and quite easy to understand.
2. Using req.app.settings (similar to above)
Here, we are defining setter, but not using getter to access value. IMO, not a good way. Also, its difficult to understand as well.
console.log(req.app.settings.settings);
3. Exporting app object so i can access app.get() without req object
Why, you need to import a file, if you can access it. It may be useful if you have a high dependency of app
module(like,high number of global setting which you need), which is generally the case when building an application.
4. Using a global variable. Probably bad idea but... isn't "settings" a global thing anyway? (I can avoid reusing it so i dont get scope problems) Not a good approach, as code is not maintainable in this case.
IMO, priority is like: 1 > 3 > 2 > 4.
本文标签: javascriptUsing global variables in expressnodeStack Overflow
版权声明:本文标题:javascript - Using global variables in expressnode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739405a2622519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论