forked from nswbmw/N-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
156 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.