评论功能
- 创建评论集合
- 判断用户是否登录,如果用户登录,再允许用户提交评论表单
- 在服务器端创建文章评论功能对应的路由
- 再路由请求处理函数中接受客户端传递过来的评论信息
- 将评论信息存储在评论集合中
- 将页面重定向回文章详情页面
- 在文章详情页面路由中获取文章评论信息并展示在页面中
在model下新建comment.js
const mongoose = require('mongoose');
const commentSchema = new mongoose.Schema({
//文章id
aid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Article',
},
//用户id
uid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
//评论时间
publishDate: {
type: Date,
default: Date.now, //当前时间
},
content: {
type: String
}
});
//3. 使用规则创建文章集合
const Comment = mongoose.model('Comment', commentSchema);
//4. 将集合规则作为模块成员导出
module.exports = {
Comment
}
判断登录角色
在login.js中判断登录角色
if(user.role == 'admin') {
//重定向到用户列表页面
res.redirect('/admin/user');
} else {
//重定向到用户列表页面
re