Settings - Express.js Guide: The Comprehensive Book on Express.js (2014)

Express.js Guide: The Comprehensive Book on Express.js (2014)


This is one of the places where Express.js shines! All the configurations are very self-explanatory and easy to read and understand.

9.1 env

This variable is used to store the current environment mode for this particular Node.js process. The value is automatically set by Express.js from process.env.NODE_ENV (which is fed to Node.js through an environmental variable of the executing machine) or if that is not set, to thedevelopment value.

The other most common values for env setting are:

· test

· stage

· preview

· production

We can augment the env param by adding app.set('env', 'preview'); or process.env.NODE_ENV=preview in our code. However, the better way is to start an app with $ NODE_ENV=preview node app or set the NODE_ENV variable on the machine.

Knowing in what mode the application runs is very important because logic related to error handling, compilation of stylesheets, and rendering of the templates can differ dramatically. Needless to say, databases and hostnames are different from environment to environment.

9.2 view cache

This flag, if set to false, which is the default, allows for painless development because templates are read each time the server requests them. On the other hand, if view cache is true, it facilitates templates compilation caching which is a desired behavior in production.

If the previous setting env is production, then this is enabled by default.

9.3 view engine

view engine holds the template file extension, e.g., ‘ext’ or ‘jade’, to utilize if the file extension is not passed to the res.render() function inside of the request handler.

For example, if we comment out this line from cli-app/app.js example:

1 // app.set('view engine', 'ejs');

The server won’t be able to locate the file because our instructions in cli-app/routes/index.js are too ambiguous:

1 /*

2 * GET home page.

3 */


5 exports.index = function(req, res){

6 res.render('index', { title: 'Express' });

7 };

The result of not having a proper template extension set.

The result of not having a proper template extension set.

We can effortlessly fix this by adding an extension to the cli-app/routes/index.js file:

1 exports.index = function(req, res){

2 res.render('index.ejs', { title: 'Express' });

3 };

For more information on how to apply different template engines, please refer to the chapter Different Template Engines.


The views setting is an absolute path to a directory with templates. This setting defaults to ./views relative to the main application file, e.g., app.js, or the file where the __dirname global is called.

As we mentioned above in the MVC Structure and Modules chapter, changing template folder name is trivial, e.g.,

1 app.set('views', __dirname + '/templates');

9.5 trust proxy

Set trust proxy to true if your Node.js app is working behind reverse proxy such as Varnish or Nginx. This will permit trusting in the X-Forwarded-* headers, e.g., X-Forwarded-Proto (req.protocol) or X-Forwarder-For (req.ips).

trust proxy is disabled by default.


1 app.set('trust proxy', true);

9.6 jsonp callback name

If you’re building an application that serves front-end clients from different domains and you don’t want to apply cross-origin resource sharing (CORS) mechanism, then JSONP is the way to go along with the res.jsonp() method of Express.js.

The default callback name, which is a prefix for our JSONP response, is usually provided in the query string of the request with the name callback, e.g., ?callback=updateView. However, if you want to use something different, just set the setting jsonp callback name to that value, e.g., for the requests with a query string param ?cb=updateView, we can use this:

1 app.set('jsonp callback name', 'cb');

so that our responses would be wrapped in updateView && updateView(body); JavaScript code (with the proper Content-Type header of course).

In most cases, we don’t want to alter this value because the callback value is somewhat standardized by jQuery JSONP functions.

9.7 json replacer and json spaces

Likewise, when we use the Express.js method res.json(), we can apply special parameters: replacer and spaces, to JSON.stringify() function in the scope of the application.

Replacer acts like a filer. You can read more about it at Mozilla Developer Network (MDN).

Express.js uses null as the default value for json replacer.

The spaces parameter is in essence an indentation size. Its value defaults to 2 in development and to 0 in production. In most cases, we leave these settings alone.

9.8 case sensitive routing

The case sensitive routing flag should be self-explanatory. We disregard the case of the URL paths when it’s false, which is the default value, and do otherwise when the value is set to true. For example, if we have app.enable('case sensitive routing');, then /users and /Users won’t be the same. It’s best to have this option disabled for the sake of avoiding confusion.

9.9 strict routing

The last but not least flag — strict routing — deals with cases of trailing slashes in URLs. With strict routing enabled, e.g., app.set('strict routing', true');, the paths will be treated differently, e.g., /users and /users/ will be completely separate routes.

By default, this parameter is set to false.

9.10 x-powered-by

Sets the HTTP response header X-Powered-By to Express value. This option is enabled by default.

9.11 etag

ETag or entity tag is one of the caching tools. If someone doesn’t know what it is or how to use it, it’s better to leave it on. Otherwise, to disable it: app.disable('etag');.

9.12 subdomain offset

The subdomain offset setting controls the value returned by req.subdomains property. This setting is useful when the app is deployed on multiple subdomains, such as By default, the two extreme parts in the domain are dropped and the rest are returned in reversed order in the req.subdomains, so for our example, the result is ['nlm', 'ncbi']. However, if the app has subdomain offset set to three by app.set('subdomain offset', 3);, the result of req.subdomains will be just ['ncbi'].