javascriptnode.jsexpress

What app.set function does in express.js?


I am new to node.js and was going through an example and could not understand what app.set('title', 'My Site'); does.

can anybody explain?


Solution

  • You can use the express instance to store and retrieve variables. In this case, you can set the title to be "My Site" and retrieve it later with something like

    var title = app.get('title');
    

    without the need to declare and keep a global variable messing around.

    The name of the parameter means nothing. You could do

    app.set('jabberwocky', 'correct battery horse staples');
    

    as well. If you're using express with jade, for example, you might need to retrieve the value of 'jabberwocky' in a template, further along the line.


    Edit: Since this answer got marked as correct and it's the most upvoted, It's my duty to point you to the one further below by Vlad Pana https://stackoverflow.com/a/44007538/1030087

    Quire a few keynames you set on the app instance do have a special meaning.