This article describes how to use the template engine in Express. For more information about how to use the template engine, see the following settings in the application to make the Express rendering template file:
Views: Directory of the template file, for example, app. set ('View', './view ')
View engine, template engine, for example: app. set ('View engine ', 'Jade ')
Then install the corresponding template engine npm package.
$ npm install jade --save
A template engine compatible with Express, such as Jade, uses res. render () to call its export method _ express (filePath, options, callback) to render the template.
Some Template engines do not follow this convention. Consolidate. js can map all popular template engines in Node to this convention, so that they can be seamlessly connected with 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. Express has been loaded internally, as shown below.
app.set('view engine', 'jade');
Generate a jade template file named index. Jade under the views Directory. the content is as follows:
html head title!= title body h1!= message
Create a route rendering index. jade file. If view engine is not set, you must specify the suffix of the view file. Otherwise, it will be omitted.
app.get('/', function (req, res) { res.render('index', { title: 'Hey', message: 'Hello there!'});});
In this case, the request "index. jade" is rendered as HTML.