Skip to content

Commit

Permalink
Update 4.4 功能设计
Browse files Browse the repository at this point in the history
- Add markdown syntax highlight to all Javascript code block
- Add quote to express-session document, include example solution for
  deprecated undefined errors
  • Loading branch information
Yue Gao authored and Yue Gao committed Oct 16, 2017
1 parent 84d5652 commit 76e4a4c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions book/4.4 功能设计.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ DELETE /post/:postId

我们通过引入 express-session 中间件实现对会话的支持:

```
```js
app.use(session(options))
```

Expand All @@ -92,7 +92,7 @@ session 中间件会在 req 上添加 session 对象,即 req.session 初始值

**middlewares/check.js**

```
```js
module.exports = {
checkLogin: function checkLogin(req, res, next) {
if (!req.session.user) {
Expand Down Expand Up @@ -121,7 +121,7 @@ module.exports = {

**routes/index.js**

```
```js
module.exports = function (app) {
app.get('/', function (req, res) {
res.redirect('/posts');
Expand All @@ -135,7 +135,7 @@ module.exports = function (app) {

**routes/posts.js**

```
```js
var express = require('express');
var router = express.Router();

Expand Down Expand Up @@ -192,7 +192,7 @@ module.exports = router;

**routes/signin.js**

```
```js
var express = require('express');
var router = express.Router();

Expand All @@ -213,7 +213,7 @@ module.exports = router;

**routes/signup.js**

```
```js
var express = require('express');
var router = express.Router();

Expand All @@ -234,7 +234,7 @@ module.exports = router;

**routes/signout.js**

```
```js
var express = require('express');
var router = express.Router();

Expand All @@ -252,7 +252,7 @@ module.exports = router;

**index.js**

```
```js
var path = require('path');
var express = require('express');
var session = require('express-session');
Expand Down Expand Up @@ -298,6 +298,28 @@ app.listen(config.port, function () {

> 注意:中间件的加载顺序很重要。如上面设置静态文件目录的中间件应该放到 routes(app) 之前加载,这样静态文件的请求就不会落到业务逻辑的路由里;flash 中间件应该放到 session 中间件之后加载,因为 flash 是基于 session 的。
> 根据 [https://github.com/expressjs/session](https://github.com/expressjs/session)
>> The default value is true, but using the default has been deprecated, as the default will change in the future. Please research into this setting and choose what is appropriate to your use-case.
> 如果出现以下错误,请显示设置session的'resave'和'saveUninitialized'
>> express-session deprecated undefined resave option; provide resave option index.js:20:9
>> express-session deprecated undefined saveUninitialized option; provide saveUninitialized option index.js:20:9
```js
app.use(session({
name: config.session.key,// 设置 cookie 中保存 session id 的字段名称
secret: config.session.secret,// 通过设置 secret 来计算 hash 值并放在 cookie 中,使产生的 signedCookie 防篡改
cookie: {
maxAge: config.session.maxAge// 过期时间,过期后 cookie 中的 session id 自动删除
},
store: new MongoStore({// save session to mongodb
url: config.mongodb// mongodb address
}),
resave: false,
saveUninitialized: false
}));
```

运行 `supervisor --harmony index` 启动博客,访问以下地址查看效果:

1. http://localhost:3000/posts
Expand Down

0 comments on commit 76e4a4c

Please sign in to comment.