Express 中配置使用 art-template模板引擎

art-template 官网

https://aui.github.io/art-template/

安装:

npm install --save art-template
npm install --save express-art-template

 

配置使用 art-template

app.engine('html', require('express-art-template') )

  

使用:

app.get('/', function (req, res) {
   //express 默认会去项目中的 views 目录中找 
    res.render('index.html', {
        title: 'hello word!'
    })
})

想要修改默认的 views 视图渲染目录,  views 不要写错

app.set('views', 目录路径)

  

实例:

var express = require('express')

//1. 创建app
var app = express()

/*
 * 第一个参数 表示, 当渲染以 .art 结尾的文件时候 使用 art-template 模板引擎
 * express-art-template 是专门用来 Express 中把 art-template 整合到 Express  
 * 虽然不需要加载 art-template   但也必须要安装
 */

//app.engine( 'art', require('express-art-template') )
app.engine( 'html', require('express-art-template') )

/*
 * Express 为 Response 相应对象提供一个方法 : render
 * render 方法默认不可以使用, 但是如果配置了模板引擎就可以
 * res.render('html模板名','模板数据')
 * 第一个参数不能写路径 , 默认会去项目中的views 目录汇总找模板文件
 * 也就是 Express 有一个约定, 开发人员把所有的视图文件都放到 views 文件中
 */

// 如果要修改 views 目录
app.set('views', 'render函数的默认路径')

 app.get('/404', function (req, res) {
 	res.render('404.html')
 })

 app.get('/admin', function (req, res) {
 	res.render('admin/index.html', {
 		title: '管理系统'
 	})
 })

//给设置静态文件路径  用/public/ 代替  './public/'
app.use('/public/', express.static('./public/') )

app.get('/', function (req, res) {

	//res.end('hello world')
	res.send('hello world')

})

app.listen(3000, function () {
	console.log( 'express app is running...' )
})

  

原文地址:https://www.cnblogs.com/jasonLiu2018/p/11217817.html