最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using global variables in expressnode - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 2

Brief 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.

发布评论

评论列表(0)

  1. 暂无评论