25

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] } }
1
  • 1
    It sounds like you might be running express 2.x? The documentation on expressjs.com is documentation for express 3.x. Commented Nov 13, 2012 at 13:36

3 Answers 3

39

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']);
Sign up to request clarification or add additional context in comments.

Comments

23

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 Comments

I might be missing something, but 'port' isn't listed in the app settings table, so how does this work?
@KnewB If it is not there before, express will create the new one and assign the value.
but why we cant get a variable that is set in a different file ( file1.js ) and even though file1.js is executed before we execute get() function.
-3
app.set('view engine','hbs')
**All are correct:**
app.get('view engine')
app.locals.settings['view engine']
app.settings['view engine']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.