What is the difference between setting a property on app.locals and calling app.set()?

前端 未结 4 621
迷失自我
迷失自我 2021-02-05 07:09

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:

4条回答
  •  长发绾君心
    2021-02-05 07:42

    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.

提交回复
热议问题