1、根據(jù)規(guī)則去解析鏈接,并且獲取ID或者索引值
//請求路徑,http://localhost/movies/1
let index = req.pathObj.base;
2、根據(jù)索引獲取數(shù)據(jù)
let movies = [
{
name:"雪暴",
brief:"雪暴簡介",
author:"張震"
},
{
name:"少年的你",
brief:"少年的你簡介",
author:"周冬雨"
},
]
let pageData = movies[index]
3、根據(jù)模板渲染頁面
res.render(movies[index],'./template/index.html')
4、底層需要實(shí)現(xiàn)渲染函數(shù),通過正則匹配,找到需要修改的地方進(jìn)行一一修改
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)
}
})
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。