/**
* Created by @chaotoge on 2017/7/5.
*/
var express = require('express');
var router = express.Router();
var checkLogin = require('../middlewares/check').checkLogin;
var PostModel = require('../models/posts');
var CommentModel = require('../models/comments');
// GET /posts 所有用户或者特定用户的文章页
// eg: GET /posts?author=xxx
/*router.get('/', function(req, res, next) {
res.render('posts');
// res.send(req.flash());
});*/
router.get('/', function(req, res, next) {
var author = req.query.author;
PostModel.getPosts(author)
.then(function (posts) {
res.render('posts', {
posts: posts
});
})
.catch(next);
});
/*// POST /posts 发表一篇文章
router.post('/', checkLogin, function(req, res, next) {
res.send(req.flash());
});
// GET /posts/create 发表文章页
router.get('/create', checkLogin, function(req, res, next) {
res.send(req.flash());
});*/
// GET /posts/create 发表文章页
router.get('/create', checkLogin, function(req, res, next) {
res.render('create');
});
// POST /posts 发表一篇文章
router.post('/', checkLogin, function(req, res, next) {
var author = req.session.user._id;
var title = req.fields.title;
var content = req.fields.content;
// 校验参数
try {
if (!title.length) {
throw new Error('请填写标题');
}
if (!content.length) {
throw new Error('请填写内容');
}
} catch (e) {
req.flash('error', e.message);
return res.redirect('back');
}
var post = {
author: author,
title: title,
content: content,
pv: 0
};
PostModel.create(post)
.then(function (result) {
// 此 post 是插入 mongodb 后的值,包含 _id
post = result.ops[0];
req.flash('success', '发表成功');
// 发表成功后跳转到该文章页
res.redirect(`/posts/${post._id}`);
})
.catch(next);
});
// GET /posts/:postId 单独一篇的文章页
/*router.get('/:postId', function(req, res, next) {
res.send(req.flash());
});*/
// GET /posts/:postId 单独一篇的文章页
/*router.get('/:postId', function(req, res, next) {
var postId = req.params.postId;
Promise.all([
PostModel.getPostById(postId),// 获取文章信息
PostModel.incPv(postId)// pv 加 1
])
.then(function (result) {
var post = result[0];
if (!post) {
throw new Error('该文章不存在');
}
res.render('post', {
post: post
});
})
.catch(next);
});*/
// GET /posts/:postId 单独一篇的文章页
router.get('/:postId', function(req, res, next) {
var postId = req.params.postId;
Promise.all([
PostModel.getPostById(postId),// 获取文章信息
CommentModel.getComments(postId),// 获取该文章所有留言
PostModel.incPv(postId)// pv 加 1
])
.then(function (result) {
var post = result[0];
var comments = result[1];
if (!post) {
throw new Error('该文章不存在');
}
res.render('post', {
post: post,
comments: comments
});
})
.catch(next);
});
/*// GET /posts/:postId/edit 更新文章页
router.get('/:postId/edit', checkLogin, function(req, res, next) {
res.send(req.flash());
});
// POST /posts/:postId/edit 更新一篇文章
router.post('/:postId/edit', checkLogin, function(req, res, next) {
res.send(req.flash());
});
// GET /posts/:postId/remove 删除一篇文章
router.get('/:postId/remove', checkLogin, function(req, res, next) {
res.send(req.flash());
});*/
// GET /posts/:postId/edit 更新文章页
router.get('/:postId/edit', checkLogin, function(req, res, next) {
var postId = req.params.postId;
var author = req.session.user._id;
PostModel.getRawPostById(postId)
.then(function (post) {
if (!post) {
throw new Error('该文章不存在');
}
if (author.toString() !== post.author._id.toString()) {
throw new Error('权限不足');
}
res.render('edit', {
post: post
});
})
.catch(next);
});
// POST /posts/:postId/edit 更新一篇文章
router.post('/:postId/edit', checkLogin, function(req, res, next) {
var postId = req.params.postId;
var author = req.session.user._id;
var title = req.fields.title;
var content = req.fields.content;
PostModel.updatePostById(postId, author, { title: title, content: content })
.then(function () {
req.flash('success', '编辑文章成功');
// 编辑成功后跳转到上一页
res.redirect(`/posts/${postId}`);
})
.catch(next);
});
// GET /posts/:postId/remove 删除一篇文章
router.get('/:postId/remove', checkLogin, function(req, res, next) {
var postId = req.params.postId;
var author = req.session.user._id;
PostModel.delPostById(postId, author)
.then(function () {
req.flash('success', '删除文章成功');
// 删除成功后跳转到主页
res.redirect('/posts');
})
.catch(next);
});
// POST /posts/:postId/comment 创建一条留言
/*router.post('/:postId/comment', checkLogin, function(req, res, next) {
res.send(req.flash());
});
// GET /posts/:postId/comment/:commentId/remove 删除一条留言
router.get('/:postId/comment/:commentId/remove', checkLogin, function(req, res, next) {
res.send(req.flash());
});*/
// POST /posts/:postId/comment 创建一条留言
router.post('/:postId/comment', checkLogin, function(req, res, next) {
var author = req.session.user._id;
var postId = req.params.postId;
var content = req.fields.content;
var comment = {
author: author,
postId: postId,
content: content
};
CommentModel.create(comment)
.then(function () {
req.flash('success', '留言成功');
// 留言成功后跳转到上一页
res.redirect('back');
})
.catch(next);
});
// GET /posts/:postId/comment/:commentId/remove 删除一条留言
router.get('/:postId/comment/:commentId/remove', checkLogin, function(req, res, next) {
var commentId = req.params.commentId;
var author = req.session.user._id;
CommentModel.delCommentById(commentId, author)
.then(function () {
req.flash('success', '删除留言成功');
// 删除成功后跳转到上一页
res.redirect('back');
})
.catch(next);
});
module.exports = router;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
node.js博客项目.zip (102个子文件)
style.css 1KB
comments.ejs 2KB
post-content.ejs 2KB
signup.ejs 2KB
edit.ejs 988B
create.ejs 938B
footer.ejs 722B
nav-setting.ejs 689B
signin.ejs 658B
header.ejs 516B
notification.ejs 383B
nav.ejs 265B
404.ejs 231B
error.ejs 230B
posts.ejs 156B
post.ejs 131B
.gitignore 523B
posts.js 7KB
posts.js 4KB
signup.js 3KB
signup.js 3KB
index.js 3KB
mongo.js 2KB
signin.js 2KB
comments.js 1KB
check.js 509B
signout.js 486B
index.js 483B
users.js 422B
default.js 228B
package.json 705B
4.9 文章.md 13KB
4.4 功能设计.md 11KB
4.10 留言.md 9KB
4.6 连接数据库.md 9KB
4.5 页面设计.md 7KB
4.7 注册.md 7KB
4.15 部署.md 7KB
4.14 测试.md 6KB
3.3 模板引擎.md 4KB
4.8 登出与登录.md 4KB
2.6 npm 使用注意事项.md 4KB
3.4 Express 浅析.md 4KB
3.2 路由.md 3KB
4.13 日志.md 3KB
1.1 Node.js 的安装与使用.md 3KB
2.1 require.md 2KB
4.3 配置文件.md 2KB
1.2 MongoDB 的安装与使用.md 2KB
2.2 exports 和 module.exports.md 2KB
4.2 准备工作.md 2KB
3.1 初始化一个 Express 项目.md 2KB
2.5 package.json.md 1KB
2.3 Promise.md 1KB
2.4 环境变量.md 1KB
4.11 404 页面.md 1KB
4.12 错误页面.md 917B
4.1 开发环境.md 403B
4.5.12.png 227KB
4.14.3.png 218KB
upload_e03460d33604eee3edb7929949c04a59.png 173KB
4.5.9.png 156KB
4.5.13.png 114KB
4.2.1.png 102KB
3.1.1.png 102KB
1.2.4.png 101KB
1.2.5.png 100KB
4.5.4.png 97KB
4.5.3.png 95KB
3.4.2.png 94KB
2.2.1.png 91KB
3.4.1.png 84KB
4.5.8.png 82KB
4.14.2.png 81KB
avatar.png 79KB
1.2.2.png 79KB
1.2.3.png 75KB
4.15.3.png 73KB
4.5.10.png 72KB
4.5.7.png 63KB
2.6.1.png 62KB
4.8.3.png 56KB
4.8.1.png 54KB
4.5.1.png 52KB
4.7.1.png 51KB
4.15.2.png 50KB
1.1.2.png 47KB
4.15.1.png 46KB
4.5.6.png 46KB
1.2.1.png 44KB
4.5.11.png 43KB
4.5.5.png 42KB
4.5.2.png 39KB
3.1.2.png 35KB
1.1.5.png 32KB
1.1.4.png 23KB
4.14.1.png 23KB
4.8.2.png 20KB
1.1.1.png 17KB
4.2.2.png 16KB
共 102 条
- 1
- 2
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 萤石云cp1-2c3wf
- 基于STM32的智能家庭火灾报警系统源码+演示ppt+演示视频(高分项目).zip
- 11. Django 常用的Web应用程序
- 萤石云固件CS-XP1-8B4WF
- HTML5小游戏【农场连连看-优秀H5小游戏合集】游戏源码分享下载 - farm.zip
- 基于Python的酒店评论情感分析源码+文档(高分项目).zip
- meta-llama-3-8b-instruct 的 model-00003-of-00004.safetensors 的3/3
- nginx配置文件,注意其中需要对应宿主机的路径
- 自用数据集自用数据集自用数据集
- HTML5小游戏【堆雪人-优秀H5小游戏合集】游戏源码分享下载 - epicsnowman.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功