如何在Express js中使用app.set()访问变量集?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (16)

如何使用Express的app.set()如:

app.set('view engine','jade');
app.set('jsDirectory',/js/');

从指南中,我了解到我可以使用app.get(<key>),但这是console.log(app.get('view engine'))

{ router:
   { app:
      { stack: [Object],
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 0,
        connections: [Getter/Setter],
        allowHalfOpen: true,
        _handle: null,
        httpAllowHalfOpen: false,
        cache: {},
        settings: [Object],
        redirects: {},
        isCallbacks: {},
        _locals: [Object],
        dynamicViewHelpers: {},
        errorHandlers: [],
        route: '/',
        routes: [Circular],
        router: [Getter],
        root: 'C:\\Users\\Shahal\\Works\\App',
        models: {},
        extensions: {},
        disconnectSchemas: [Function: disconnectSchemas],
        passport: [Object] },
     routes: {},
     params: {},
     _params: [],
     middleware: [Function] } }
提问于
用户回答回答于

它们可以通过app.set对象获得:

app.set('oneSetting', 'one');
app.set('twoSetting', 'two');
app.set('view engine','jade');

console.log(app.settings.oneSetting);
console.log(app.settings.twoSetting);
console.log(app.settings['view engine']);
用户回答回答于

您也可以这样设置变量。

     app.set('port', 3000);

然后再用

     app.get('port');

因为它更短,更直接。

    app.get(name)
    Returns the value of name app setting, where name is one of strings in the app settings table. 

扫码关注云+社区