我正在尝试创建一个能够对博客/文章发表评论的博客网站。我有终点
app.get('/post/:slug', function(req, res) {
var _slug = req.params.slug;
var blog_post = _.findWhere(_DATA, { slug: _slug });
if (!blog_post) return res.render('404');
res.render('post', blog_post);
});
使用HandleBars
处理博客/文章的显示。它从数据库_DATA
检索帖子,并使用模板post.handlebars
显示它。它现在没有别的。
我想添加评论这篇文章的能力。我对网络编程很陌生,不知道如何处理这样的请求。现在,我的想法是添加
<article>
<form method="POST" action="/comment">
<div class="input-field">
<label>Comment:</label>
<textarea type="text" name="comment" rows="20" placeholder="What's on your mind?"></textarea>
</div>
<button type="submit">Add Comment</button>
</form>
</article>
到我post.handlebars
的底部并添加结束点app.post('/comment', function(req, res) {});
但这会带来多个问题,即我怎么知道我评论哪篇文章?在这个新的终点,我的req.body
除了评论的内容之外什么都不包含。
处理此问题的正确方法是什么?
答案 0 :(得分:0)
将评论表单中的文章ID作为隐藏输入。当您提交表单时,您将获得article-id值。 我已经使用把手将文章ID放在表单中,根据你的数据改变了article.id的值。
<article>
<form method="POST" action="/comment">
<div class="input-field">
<label>Comment:</label>
<textarea type="text" name="comment" rows="20" placeholder="What's on your mind?"></textarea>
</div>
<input type="hidden" name="article-id" value="{{article.id}}">
<button type="submit">Add Comment</button>
</form>
</article>