【发布时间】:2021-09-09 10:55:41
【问题描述】:
免责声明:当我显然是个菜鸟时,我问了这个问题,但我留下了这个问题,希望它可以帮助将来的人。
背景: 图片让问题更清楚:My issue
我正在开发一个 Node JS + EJS 博客应用程序。 我有一个单独的页面(www.domain/compose),我可以在其中为我的帖子写标题和正文。然后我可以通过 www.domain/posts/post-title 访问这篇文章
如picture 所示,我想发送可呈现的 HTML 标签。
我想以我想要的任何顺序发送任何我喜欢的 HTML 标记。不仅是 em 和 img 标签。
我的节点服务器代码:
app.post("/compose", (req, res)=>{
const post = {
'title': req.body.newPostTitle,
'body': req.body.newPostContent,
};
posts.push(post);
res.redirect('/');
});
app.get("/posts/:postName", (req, res)=>{
const urlSuffix = req.params.postName;
posts.forEach((post)=>{
const postURL = lodash.kebabCase(post.title);
if(urlSuffix===postURL || urlSuffix===post.title){
console.log("Match Found!");
res.render("post", {postTitle:post.title, postBody: post.body});
}else{
console.log("Not a match")
}
});
});
我的 EJS 模板代码:
<%- include('partials/header') -%>
<h1><%=postTitle%></h1>
<p><%=postBody%></p>
<%- include('partials/footer') -%>
【问题讨论】:
标签: javascript node.js ejs blogs