shahalpk
shahalpk

Reputation: 3452

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

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] } }

Upvotes: 25

Views: 32746

Answers (3)

Nischal Jakhar
Nischal Jakhar

Reputation: 29

app.set('view engine','hbs')
**All are correct:**
app.get('view engine')
app.locals.settings['view engine']
app.settings['view engine']

Upvotes: -3

Rapha&#235;l Parent
Rapha&#235;l Parent

Reputation: 253

I know this is 2 years old, but it is still the first link that pops up on google so i thought this could be appropriate.

You could also set your variable like that

     app.set('port', 3000);

And later get it with

     app.get('port');

I prefer that approach because it's shorter and more straight forward. It is also the way they use in the 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. 

Upvotes: 23

Hector Correa
Hector Correa

Reputation: 26690

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']);

Upvotes: 39

Related Questions