Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What app.set function does (express.js)?

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

like image 378
Paras Avatar asked Aug 10 '14 13:08

Paras


People also ask

What is set function in Express?

The app.set() function assigns or sets a setting name to value. This can store any type of value as the user wants, but there are some certain names that can be used to configure the behaviour of the server. Some of the properties that can be configured with the set functionality are − env. etag.

Which function is used to create an Express app?

App. use() is used to bind *application-level middleware to an instance of the app object which is instantiated on the creation of the Express server (router. use() for router-level middleware). Here, the path is optional.

What do you use express JS for?

What Is Express JS? Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.


2 Answers

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.

like image 162
ffflabs Avatar answered Sep 23 '22 13:09

ffflabs


I know I am a bit late to the party but I had the same problem and that's how I got here. After I did some research, I fell that the answer is a bit incomplete. In case that first parameter is an application setting, the following table must be read.

Property -> Type -> Description

case sensitive routing -> Boolean -> Enable case sensitivity. When enabled, "/Foo" and "/foo" are different routes. When disabled, "/Foo" and "/foo" are treated the same.

env -> String -> Environment mode. Be sure to set to "production" in a production environment; see Production best practices: performance and reliability.
process.env.NODE_ENV (NODE_ENV environment variable) or “development” if NODE_ENV is not set.

etag -> Varied -> Set the ETag response header. For possible values, see the etag options table.

jsonp callback name -> String -> Specifies the default JSONP callback name.

json replacer -> Varied -> The 'replacer' argument used by JSON.stringify. NOTE: Sub-apps will inherit the value of this setting.

json spaces -> Varied -> The 'space' argument used by JSON.stringify. This is typically set to the number of spaces to use to indent prettified JSON. NOTE: Sub-apps will inherit the value of this setting.

query parser -> Varied -> Disable query parsing by setting the value to false, or set the query parser to use either “simple” or “extended” or a custom query string parsing function. The simple query parser is based on Node’s native query parser, querystring. The extended query parser is based on qs. A custom query string parsing function will receive the complete query string, and must return an object of query keys and their values.

strict routing -> Boolean -> Enable strict routing. When enabled, the router treats "/foo" and "/foo/" as different. Otherwise, the router treats "/foo" and "/foo/" as the same. NOTE: Sub-apps will inherit the value of this setting.

subdomain offset -> Number -> The number of dot-separated parts of the host to remove to access subdomain.

trust proxy -> Varied -> Indicates the app is behind a front-facing proxy, and to use the X-Forwarded-* headers to determine the connection and the IP address of the client. NOTE: X-Forwarded-* headers are easily spoofed and the detected IP addresses are unreliable.

When enabled, Express attempts to determine the IP address of the client connected through the front-facing proxy, or series of proxies. The req.ips property, then contains an array of IP addresses the client is connected through. To enable it, use the values described in the trust proxy options table.

The trust proxy setting is implemented using the proxy-addr package. For more information, see its documentation. NOTE: Sub-apps will inherit the value of this setting, even though it has a default value.

views -> String or Array -> A directory or an array of directories for the application's views. If an array, the views are looked up in the order they occur in the array.
process.cwd() + '/views'

view cache -> Boolean -> Enables view template compilation caching.

view engine -> String -> The default engine extension to use when omitted. NOTE: Sub-apps will inherit the value of this setting.

x-powered-by -> Boolean -> Enables the "X-Powered-By: Express" HTTP header.

like image 34
Vlad Pana Avatar answered Sep 22 '22 13:09

Vlad Pana