Appendix C. Extending and configuring Express

 

Express does a lot right out of the box, but extending Express and tweaking its configuration can simplify development and allow it to do more.

C.1. Extending Express

Let’s start by looking at how to extend Express. In this section, you’ll learn how to

  • Create your own template engines
  • Take advantage of community-created template engines
  • Improve your applications using modules that extend Express

C.1.1. Registering template engines

Engines may provide Express support out of the box by exporting an __express method. But not every engine will provide this, or you may want to write your own engines. Express facilitates this by providing the app.engine() method. In this section, we’ll take a look at writing a small markdown template engine with variable substitution for dynamic content.

The app.engine() method maps a file extension to a callback function so that Express knows how to use it. In the following listing, the .md extension is passed so that render calls such as res.render('myview.md') will use the callback function to render the file. This abstraction enables practically any template engine to be used within the framework. In this custom template engine, braces are used around local variables to allow for dynamic input—for example, {name} will output the value of name wherever it’s found in the template.

Listing C.1. Handling the .md extension

C.2. Advanced configuration