NodeJS Consolidate Module

Posted by shauryasinha on 27-Dec-2017 01:15

I want to understand what is the use of Consolidate module in NodeJS

All Replies

Posted by Chad Thomson on 08-Jan-2018 07:58

I'll take a swing at it.

I assume you're talking about this module:
https://www.npmjs.com/package/consolidate
https://github.com/tj/consolidate.js

The short version, Consolidate.js:

  • wraps template engines making them ExpressJS compatible
  • caches the generated output -- standard ExpressJS only caches the template file itself

There may be more aspects to it, but those are the headline features I discovered.

The author of the module does a good job documenting the use of the module, but doesn't really explain the "use-case" -- the "why" to it -- nor are there demonstrations comparing scenarios where it is used vs where it is not.
To answer that, and hopefully your question, I did some digging and found these points useful.

http://expressjs.com/en/guide/using-template-engines.html

"Express-compliant template engines such as Jade and Pug export a function named __express(filePath, options, callback), which is called by the res.render() function to render the template code.
Some template engines do not follow this convention. The Consolidate.js library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express."

Some template engines may have been developed without ExpressJS in-mind. ConsolidateJS wraps those engines and makes them ExpressJS 3.X compatible.

Also on that page:

"Note: The view engine cache does not cache the contents of the template’s output, only the underlying template itself. The view is still re-rendered with every request even when the cache is on."

Consolidate.js will cache the rendered output, too.

Hope that helps somewhat.

This thread is closed