如何使用 express 的app.set()
访问变量集,例如
app.set('view engine','jade');
app.set('jsDirectory',/js/');
From the guide, i understand that i can access the same using app.get(<key>)
, but this is the output of 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] } }
They become available through the app.settings object:
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']);
这是 2 年前的事了,但它仍然是谷歌上弹出的第一个链接,所以我认为这可能是合适的。
你也可以这样设置你的变量
app.set('port', 3000);
后来得到它
app.get('port');
我更喜欢这种方法,因为它更短,更直接。这也是他们在Express 4.x文档中使用的方式。
app.get(name)
Returns the value of name app setting, where name is one of strings in the app settings table.
app.set('view engine','hbs')
**All are correct:**
app.get('view engine')
app.locals.settings['view engine']
app.settings['view engine']