Skip to content

Commit

Permalink
update-2017-11-11
Browse files Browse the repository at this point in the history
  • Loading branch information
nswbmw committed Nov 12, 2017
1 parent 63adf5e commit d0e7c51
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 130 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@
- [pm2](https://github.com/nswbmw/N-blog/blob/master/book/4.15%20%E9%83%A8%E7%BD%B2.md#4152-pm2)
- [部署到 Heroku](https://github.com/nswbmw/N-blog/blob/master/book/4.15%20%E9%83%A8%E7%BD%B2.md#4152-部署到-heroku)
- [部署到 UCloud](https://github.com/nswbmw/N-blog/blob/master/book/4.15%20%E9%83%A8%E7%BD%B2.md#4153-部署到-ucloud)
- 课后作业
- 扩展训练
- 添加分页功能
- 添加二级评论功能
- 添加标签(tag)功能

## 捐赠

您的捐赠,是我持续开源的动力。

支付宝 | 微信
------|------
![](./public/alipay.png) | ![](./public/wechat.jpeg)
![](./public/alipay.png) | ![](./public/wechat.jpeg)
6 changes: 3 additions & 3 deletions book/4.10 留言.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ router.get('/:postId', function (req, res, next) {
```js
// POST /posts/:postId/comment 创建一条留言
router.post('/:postId/comment', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('创建留言')
})

// GET /posts/:postId/comment/:commentId/remove 删除一条留言
router.get('/:postId/comment/:commentId/remove', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('删除留言')
})
```

Expand Down Expand Up @@ -315,4 +315,4 @@ router.get('/:postId/comment/:commentId/remove', checkLogin, function (req, res,

上一节:[4.9 文章](https://github.com/nswbmw/N-blog/blob/master/book/4.9%20%E6%96%87%E7%AB%A0.md)

下一节:[4.11 404页面](https://github.com/nswbmw/N-blog/blob/master/book/4.11%20404%20%E9%A1%B5%E9%9D%A2.md)
下一节:[4.11 404页面](https://github.com/nswbmw/N-blog/blob/master/book/4.11%20404%20%E9%A1%B5%E9%9D%A2.md)
34 changes: 11 additions & 23 deletions book/4.12 错误页面.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
前面讲到 express 有一个内置的错误处理逻辑,如果程序出错,会直接将错误栈返回并显示到页面上。现在我们来自己写一个错误页面,新建 views/error.ejs,添加如下代码
前面讲到 express 有一个内置的错误处理逻辑,如果程序出错,会直接将错误栈返回并显示到页面上。如访问:`localhost:3000/posts/xxx/edit` 没有权限编辑的文章页,将会直接在页面中显示错误栈,如下

**views/error.ejs**

```ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><%= blog.title %></title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<h2><%= error.message %></h2>
<p><%= error.stack %></p>
</body>
</html>
```js
Error: 权限不足
at /Users/nswbmw/Desktop/myblog/routes/posts.js:95:15
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
```

修改 index.js,在 `app.listen` 上一行添加如下代码
现在我们修改代码,实现复用页面通知。修改 index.js,在 `app.listen` 上面添加如下代码

**index.js**

```js
// error page
app.use(function (err, req, res, next) {
res.render('error', {
error: err
})
req.flash('error', err.message)
res.redirect('/posts')
})
```

这里我们实现了自己定义的错误页面的展示
这里我们实现了将错误信息用页面通知展示的功能,刷新页面将会跳转到主页并显示『权限不足』的红色通知

上一节:[4.11 404页面](https://github.com/nswbmw/N-blog/blob/master/book/4.11%20404%20%E9%A1%B5%E9%9D%A2.md)

下一节:[4.13 日志](https://github.com/nswbmw/N-blog/blob/master/book/4.13%20%E6%97%A5%E5%BF%97.md)
下一节:[4.13 日志](https://github.com/nswbmw/N-blog/blob/master/book/4.13%20%E6%97%A5%E5%BF%97.md)
9 changes: 7 additions & 2 deletions book/4.2 准备工作.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ESLint 需要结合编辑器或 IDE 使用,如:
- Sublime Text 需要装两个插件:SublimeLinter + SublimeLinter-contrib-eslint
- VS Code 需要装一个插件:ESLint

> 小提示:Sublime Text 安装插件通过 ctrl+shift+p 调出 Package Control,输入 install 选择 Install Package 回车。输入对应插件名搜索,回车安装。
> 小提示:VS Code 安装插件需要点击左侧『扩展』页
全局安装 eslint:

```sh
Expand All @@ -69,10 +72,12 @@ eslint --init

初始化 eslint 配置,依次选择:

-> Use a popular style guide
-> Standard
-> Use a popular style guide
-> Standard
-> JSON

> 注意:如果 Windows 用户使用其他命令行工具无法上下切换选项,切换回 cmd。
eslint 会创建一个 .eslintrc.json 的配置文件,同时自动安装并添加相关的模块到 devDependencies。这里我们使用 Standard 规范,其主要特点是不加分号。

### 4.2.4 EditorConfig
Expand Down
34 changes: 17 additions & 17 deletions book/4.4 功能设计.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
3. 查看一篇文章(包含留言):`GET /posts/:postId`
5. 发表文章
1. 发表文章页:`GET /posts/create`
2. 发表文章:`POST /posts`
2. 发表文章:`POST /posts/create`
6. 修改文章
1. 修改文章页:`GET /posts/:postId/edit`
2. 修改文章:`POST /posts/:postId/edit`
Expand Down Expand Up @@ -143,47 +143,47 @@ const checkLogin = require('../middlewares/check').checkLogin
// GET /posts 所有用户或者特定用户的文章页
// eg: GET /posts?author=xxx
router.get('/', function (req, res, next) {
res.send(req.flash())
res.send('主页')
})

// POST /posts 发表一篇文章
router.post('/', checkLogin, function (req, res, next) {
res.send(req.flash())
// POST /posts/create 发表一篇文章
router.post('/create', checkLogin, function (req, res, next) {
res.send('发表文章')
})

// GET /posts/create 发表文章页
router.get('/create', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('发表文章页')
})

// GET /posts/:postId 单独一篇的文章页
router.get('/:postId', function (req, res, next) {
res.send(req.flash())
res.send('文章详情页')
})

// GET /posts/:postId/edit 更新文章页
router.get('/:postId/edit', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('更新文章页')
})

// POST /posts/:postId/edit 更新一篇文章
router.post('/:postId/edit', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('更新文章')
})

// GET /posts/:postId/remove 删除一篇文章
router.get('/:postId/remove', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('删除文章')
})

// POST /posts/:postId/comment 创建一条留言
router.post('/:postId/comment', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('创建留言')
})

// GET /posts/:postId/comment/:commentId/remove 删除一条留言
router.get('/:postId/comment/:commentId/remove', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('删除留言')
})

module.exports = router
Expand All @@ -199,12 +199,12 @@ const checkNotLogin = require('../middlewares/check').checkNotLogin

// GET /signin 登录页
router.get('/', checkNotLogin, function (req, res, next) {
res.send(req.flash())
res.send('登录页')
})

// POST /signin 用户登录
router.post('/', checkNotLogin, function (req, res, next) {
res.send(req.flash())
res.send('登录')
})

module.exports = router
Expand All @@ -220,12 +220,12 @@ const checkNotLogin = require('../middlewares/check').checkNotLogin

// GET /signup 注册页
router.get('/', checkNotLogin, function (req, res, next) {
res.send(req.flash())
res.send('注册页')
})

// POST /signup 用户注册
router.post('/', checkNotLogin, function (req, res, next) {
res.send(req.flash())
res.send('注册')
})

module.exports = router
Expand All @@ -241,7 +241,7 @@ const checkLogin = require('../middlewares/check').checkLogin

// GET /signout 登出
router.get('/', checkLogin, function (req, res, next) {
res.send(req.flash())
res.send('登出')
})

module.exports = router
Expand Down
12 changes: 6 additions & 6 deletions book/4.7 注册.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ router.post('/', checkNotLogin, function (req, res, next) {

// 待写入数据库的用户信息
let user = {
name,
password,
gender,
bio,
avatar
name: name,
password: password,
gender: gender,
bio: bio,
avatar: avatar
}
// 用户信息写入数据库
UserModel.create(user)
Expand Down Expand Up @@ -239,4 +239,4 @@ router.get('/', function (req, res, next) {

上一节:[4.6 连接数据库](https://github.com/nswbmw/N-blog/blob/master/book/4.6%20%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93.md)

下一节:[4.8 登出与登录](https://github.com/nswbmw/N-blog/blob/master/book/4.8%20%E7%99%BB%E5%87%BA%E4%B8%8E%E7%99%BB%E5%BD%95.md)
下一节:[4.8 登出与登录](https://github.com/nswbmw/N-blog/blob/master/book/4.8%20%E7%99%BB%E5%87%BA%E4%B8%8E%E7%99%BB%E5%BD%95.md)
Loading

0 comments on commit d0e7c51

Please sign in to comment.