Home>

The following settings need to be made in the application for express to render the template file:

views, the directory where the template files are placed,For example:app.set ("views", "./views")

view engine, template engine,For example:app.set ("view engine", "jade")

Then install the appropriate template engine npm package.

$npm install jade --save

Express-compatible template engine,For example jade, call its export method __express (filepath, options, callback) via res.render () 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,This allows seamless connection with express.

Once the view engine is successfully set,There is no need to specify the engine explicitly,Or load the template engine module in the application,express is already loaded internally,As follows.

app.set ("view engine", "jade");

Generate a jade template file named index.jade in the views directory,The contents are as follows:

html
 head
  title!=title
 body
  h1!=message

Then create a route to render the index.jade file. If the view engine is not set, you need to specify the suffix of the view file,Otherwise it will be missed.

app.get ("/", function (req, res) {
 res.render ("index", {title:"hey", message:"hello there!"});
});

Send a request to the homepage at this time,"Index.jade" will be rendered as html.

  • Previous Android performance optimization methods
  • Next Some PHP page scripting tips related to SEO optimization for WordPress