1、根据规则去解析链接,并且获取ID或者索引值
//请求路径,http://localhost/movies/1
let index = req.pathObj.base;
2、根据索引获取数据
let movies = [
{
name:"雪暴",
brief:"雪暴简介",
author:"张震"
},
{
name:"少年的你",
brief:"少年的你简介",
author:"周冬雨"
},
]
let pageData = movies[index]
3、根据模板渲染页面
res.render(movies[index],'./template/index.html')
4、底层需要实现渲染函数,通过正则匹配,找到需要修改的地方进行一一修改
function render(options,path) {
fs.readFile(path,{encoding:"utf-8",flag:"r"},(err,data)=>{
if(err){
console.log(err)
}else{
let reg = /\{\{(.*?)\}\}/igs
let result;
while (result = reg.exec(data)){
//去除两边的空白
let strKey = result[1].trim()
let strValue = options[strKey]
data = data.replace(result[0],strValue)
}
this.end(data)
}
})
}
网友评论