Secret Express.js Settings

Secret Express.js Settings
Courses on Node.js, React
and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →


Express.js is one of the top Node.js frameworks out there. It was used in the overwhelming majority of projects that I’ve encountered since I began working with Node.js in 2011. One of the main selling points and key differentiators is the framework’s configurability. However, while writing Express.js Guide and Pro Express.js, I discovered a few secret settings never mentioned in their documentation.

As of this writing, these undocumented settings (Figure 1) are:

  • json spaces
  • etag
  • query parser
Figure 1: Documented Express.js Settings

Figure 1: Documented Express.js Settings

Let’s take a closer look at each one of them. This article is applicable to Express.js versions 3.x and up to 4.8.1 which is the latest as of this writing.

json spaces

When you use the Express.js’ method response.json() to send back to the client’s JSON data, you benefit from special parameters: replacer and spaces. The former is documented on the website while the latter is not. Under the hood, these parameters are passed to the JSON.stringify() function (MDN docs). JSON.stringify() is a widely used function for transforming native JavaScript/Node.js object into strings.

The spaces option modifies JSON output exactly as described in the stringify() documentation—“Causes the resulting string to be pretty-printed”. For example, you can set the spaces value to 4 to indent each level with four spaces:

// ... Express.js app instantiation 
app.set('json spaces', 4);
// ... Express.js app routes

The statement above will produce indentation as shown in Figure 2.

Figure 2: JSON output with replacer and spaces set

Figure 2: JSON output with replacer and spaces set

On the other hand, when you use response.json() with default settings (probably to minimize the size), the output will look less readable (as shown in Figure 3).

Figure 3: JSON output without spaces set

Figure 3: JSON output without spaces set

My opinion is that spaces set to 2 can be used when the application is in development mode (app.get('env')=='development').

etag

ETag or entity tag is one of the caching mechanisms. The way it works is akin to putting a unique identifier for the content on a given URL. In other words, if content doesn’t change on a specific URL, the etag will remain the same and the browser will use the cache.

In Figure 4, you can see an example of the ETag response header.

Figure 4: ETag header is enabled

Figure 4: ETag header is enabled

If someone doesn’t know what it is or how to use it, it’s better to leave the Express.js etag setting alone, which means that by default the ETag will be enabled. Otherwise, to disable it:

// ... Express.js app instantiation 
app.disable('etag'); 
// ... Express.js app routes

This will eliminate ETag response header as shown in Figure 5.

Figure 5: ETag header is enabled

Figure 5: ETag header is enabled

By default, Express.js uses “weak” ETag. Other possible values are:

  • true: weak ETag, e.g., app.enable('etag'); that produces a response with ETag as shown in Figure 4
  • false: no ETag at all (IMHO not recommended), e.g., app.disable('etag'); that produces a response without ETag as shown in Figure 5
  • weak: weak ETag, e.g., app.set('etag', 'weak');
  • strong: strong ETag, e.g., app.set('etag', 'strong');

An identical strong ETag guarantees the response is byte-for-byte the same, while an identical weak ETag indicates that the response is semantically the same.

[Sidenote]

Reading blog posts is good, but watching video courses is even better because they are more engaging.

A lot of developers complained that there is a lack of affordable quality video material on Node. It's distracting to watch to YouTube videos and insane to pay $500 for a Node video course!

Go check out Node University which has FREE video courses on Node: node.university.

[End of sidenote]

query parser

Query, or query string, is a data sent in the URL after the question mark sing, e.g., http://anywherelib.com?name=value&name2=value2. Express.js automatically includes the parsing of such query strings in the form of a middleware.

The default value for query parser is extended which will use the qs module.

All possible values for the query parser setting are:

The usage example with disabled query parser middleware as follows:

// ... Express.js app instantiation 
app.set('query parser', false);
// ... Express.js app routes

The usage example with qs query parser middleware, as follows:

// ... Express.js app instantiation 
app.set('query parser', `extended`);
// ... Express.js app routes

It’s possible to pass your own function as an argument. In this case, your custom function will be used for parsing instead of parsing libraries like qs. For example,

// ... Express.js app instantiation 
app.set('query parser', function(value, options){
  // ... value is a query string, process it here
});
// ... Express.js app routes

Conclusion

As usual with undocumented features, use them with caution. Also be aware of the framework’s versions (this article was tested with 4.8.1).

This text is part of Azat’s upcoming book—Pro Express.js by Apress. If you liked this article, make sure to opt-in for the webapplog.com newsletter.

Courses on Node.js,
React and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →

--
Azat Mardan
Azat Mardan avatar
https://www.linkedin.com/in/azatm
To contact Azat, the main author of this blog, submit the contact form.

Also, make sure to get 3 amazing resources to FREE when you sign up for the newsletter.
Simple.
Easy.
No commitment.

6 thoughts on “Secret Express.js Settings

  1. Pingback: Error Handling and Running an Express.js App

  2. Pingback: Issue #20 – Oct 3rd, 2014 | Tales from the Front End

  3. Azat Post author

    Nice! When did you update the docs?
    This post was written a month ago, that’s why I took a screenshot. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *