The following settings are required in the application to allow Express rendering template files:
Views, put the directory of template files, such as: app.set (' views ', './views ')
View engine, template engine, such as: App.set (' View engine ', ' Jade ')
Then install the appropriate template engine NPM package.
And the Express-compatible template engine, such as Jade, invokes its export method __express (FilePath, Options, callback) rendering template via Res.render ().
Some template engines do not follow this convention, and Consolidate.js can map all the popular template engines in the Node to this Convention, which seamlessly connects to Express.
Once the view engine is set successfully, you do not need to explicitly specify the engine or load the template engine module in the application, and Express is already loaded internally, as shown below.
App.set (' View engine ', ' Jade ');
Generate the Jade template file named Index.jade in the views directory as follows:
HTML
head
title!= title
h1!= Message
Then create a route rendering Index.jade file. If you do not set view engine, you need to indicate the suffix of the view file, otherwise it will be omitted.
App.get ('/', function (req, res) {
res.render (' index ', {title: ' Hey ', message: ' Hello there! '});
At this point, the request is sent to the home page, and "Index.jade" is rendered as HTML.