Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access variables set using app.set() in express js

Tags:

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] } } 
like image 509
shahalpk Avatar asked Nov 13 '12 12:11

shahalpk


People also ask

What does app set () do?

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.

What is app use () in Express?

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.

How do you store local variables that can be access within the application in express JS?

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.

How do I create a global variable in Express?

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.


1 Answers

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']); 
like image 81
Hector Correa Avatar answered Sep 19 '22 15:09

Hector Correa