25
votes

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] } }
3
It sounds like you might be running express 2.x? The documentation on expressjs.com is documentation for express 3.x. - Linus Thiel

3 Answers

38
votes

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

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. 
-3
votes
app.set('view engine','hbs')
**All are correct:**
app.get('view engine')
app.locals.settings['view engine']
app.settings['view engine']