如何使用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] } }
答案 0 :(得分:32)
它们可通过app.settings对象获得:
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']);
答案 1 :(得分:20)
我知道这是2岁,但它仍然是谷歌上弹出的第一个链接所以我认为这可能是合适的。
您也可以像这样设置变量
app.set('port', 3000);
后来用
来搞定 app.get('port');
我更喜欢这种方法,因为它更短更直接。这也是他们在Express 4.x documentation中使用的方式。
app.get(name)
Returns the value of name app setting, where name is one of strings in the app settings table.
答案 2 :(得分:-1)
app.set('view engine','hbs')
**All are correct:**
app.get('view engine')
app.locals.settings['view engine']
app.settings['view engine']