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
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.
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).
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.
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.
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 4false
: no ETag at all (IMHO not recommended), e.g.,app.disable('etag');
that produces a response without ETag as shown in Figure 5weak
: 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.
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:
false
: disable parsingtrue
: uses qssimple
: uses the corequerystring
moduleextended
: uses theqs
module
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.
They weren’t on the old site (look at the screen shot).
Nice! When did you update the docs?
This post was written a month ago, that’s why I took a screenshot. :-)
They are almost all documented now at http://expressjs.com/4x/api.html#app-settings
etag and query parser are documented.