How do i access the variables set using express's app.set()
for e.g
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] } }
The app. set() function is used to assigns the setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server.
The app. use() method mounts or puts the specified middleware functions at the specified path. This middleware function will be executed only when the base of the requested path matches the defined path.
5) What is the way to store local variables that can be accessed within the application? Answer: C is the correct option. We can store local variables that can be accessed within the application by using app. locals.
To make a global variable, just declare it without the var keyword. (Generally speaking this isn't best practice, but in some cases it can be useful - just be careful as it will make the variable available everywhere.) //we can now access 'app' without redeclaring it or passing it in... /* * GET home page. */ app.
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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With