Using template engines with Express

  • Installation template engine
$ npm install pug --save
  • Setting the template path
app.set('views', './views')
  • Setting template engine
app.set('view engine', 'pug')
  •  Create a template page
//Create a Pug template file named index.pug in the views directory, with the following content:
html
  head
    title= title
  body
    h1= message
  • Rendering template page
const express = require('express')

let app = express()

app.set('views', 'views')

app.set('view engine', 'pug')

app.get('/', function (req, res) {
    res.render('index', {title: 'Hey', message: 'Hello there!'})
})

app.listen(80)

 

Guess you like

Origin www.cnblogs.com/stefzi/p/11291814.html