专栏目录请点击
安装渲染引擎
这个属于express中的内容
安装
- 假设我们需要渲染一个页面,那么我们就需要一个渲染引擎,这里我们使用
ejs
,安装如下
npm install ejs
配置
- 安装之后,我们需要配置模板引擎的渲染目录,我们在
index.js
下设置如下
...
const auth = require("./wechat/auth")
// 配置模板资源目录
app.set("views","./views")
// 配置模板引擎
app.set("view engine","ejs")
// 页面路由
app.get("/search",(req,res) => {
// 在这里渲染模板内容
})
// 应用中间件
app.use(auth())
...
- 在根目录下新建
views
文件夹,并写一个search.ejs
文件,写上如下内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>这是一个搜索页面</h1>
</body>
</html>
渲染
app.get("/search", (req, res) => {
// 在这里渲染模板内容
res.render("search")
})
测试
- 我们访问一下路由
你的域名/search
- 就会发现,他返回了我们渲染的界面