全栈开发 - yog2 - 03 启用中间件cookie-session
方式一
1. project项目下安装npm包
1
npm install cookie-session --save
session模块可以给其它模块共享,所以安装在project下。
2. 创建插件文件
plugins/cookieSession/index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var session = require('express-session');
// 插件逻辑
module.exports.cookieSession = function(app, conf){
console.log('module.exports.cookieSession.conf.secret : ' + conf.secret);
return function(){
app.use(session(conf));
};
};
// 设置插件默认配置
module.exports.cookieSession.defaultConf = {
}
console.log 是我们设置的调试信息,一会运行起来 可以看到效果
3. 配置插件
修改文件 conf/plugins/http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module.exports.http = {
/**
**************************************************************************
*
* Middleware配置
*
* 用于管理加载哪些中间件,以及这些中间件的加载顺序
*
* 配置中可以通过字符串引用中间件插件,默认配置中均是Yog2的默认中间件
*
* 配置中也可以通过function(req, res, next){}的形式加载中间件而无需使用插件管理
*
***************************************************************************/
middleware: [
'favicon',
'compression',
'static',
'responseTime',
'cookieParser',
'bodyParser',
'log',
'ral',
'views',
'methodOverride',
'cookieSession',
'dispatcher',
'notFound',
'error'
]
};
...
// 这里是配置
module.exports.cookieSession = {
name: 'session',
keys: ['regtergrdg3425345wffbnyir768323453'],
secret : "afdds0098d7as879fdsuio3oih32jkljdsknzyi32",
maxAge: 24 * 60 * 60 * 1000 // 24 hours 单位毫秒,从设置cookie开始多少毫秒失效
}
加入 ‘cookieSession’ 注意执行的先后顺序 配置文件官方推荐放在
这个位置
1 conf/plugins/cookieSession.js
4. app 端加入执行
文件 server/action/index.js
1
2
3
4
5
6
7
8
9
10
...
var cookieSession = require('cookie-session')
module.exports = function(req, res){
req.session.views = (req.session.views || 0) + 1;
console.log('session.views:'+req.session.views);
...
};
设置了
这个session key 可以执行看看了
1 views
1
yog2 release --dest debug
5. project运行起来
1
2
3
4
5
6
7
8
yog2 run
...
YOG/LOADER 34338: load plugin [cookieSession] with conf {"name":"session","keys":["regtergrdg3425345wffbnyir768323453"],"secret":"afdds0098d7as879fdsuio3oih32jkljdsknzyi32","maxAge":86400000}
module.exports.cookieSession.conf.secret : afdds0098d7as879fdsuio3oih32jkljdsknzyi32
YOG/LOADER 34338: plugin [cookieSession] loaded in 0 ms
...
plugins load completed
看到
, 插件加载成功了
1 module.exports.cookieSession.conf.secret : afdds0098d7as879fdsuio3oih32jkljdsknzyi32
6. 浏览访问控制器 home/index
输入 http://127.0.0.1:8085/home 连续刷新3次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
YOG/DISPATCHER 34338: trying to get default action [index]
YOG/DISPATCHER 34338: get action file at [/Users/hans/Documents/test/yog2-test/yogServer/app/home/action/index.js]
YOG/DISPATCHER 34338: actually get action [index]
YOG/DISPATCHER 34338: disptacher for [/home] cost [45] ms
YOG/DISPATCHER 34338: start action excution [index] with method [GET]
YOG/DISPATCHER 34338: excute action [index] with default action method
session.views:1
YOG/DISPATCHER 34338: trying to get router [home]
YOG/DISPATCHER 34338: actually get router [home]
YOG/DISPATCHER 34338: router is matched, remove router from url [/]
YOG/DISPATCHER 34338: [/] lookup for user defined router
YOG/DISPATCHER 34338: user defined router was not found, [/] lookup for auto dispatcher
YOG/DISPATCHER 34338: trying to get default action [index]
YOG/DISPATCHER 34338: actually get action [index]
YOG/DISPATCHER 34338: disptacher for [/home] cost [3] ms
YOG/DISPATCHER 34338: start action excution [index] with method [GET]
YOG/DISPATCHER 34338: excute action [index] with default action method
session.views:2
YOG/DISPATCHER 34338: trying to get router [home]
YOG/DISPATCHER 34338: actually get router [home]
YOG/DISPATCHER 34338: router is matched, remove router from url [/]
YOG/DISPATCHER 34338: [/] lookup for user defined router
YOG/DISPATCHER 34338: user defined router was not found, [/] lookup for auto dispatcher
YOG/DISPATCHER 34338: trying to get default action [index]
YOG/DISPATCHER 34338: actually get action [index]
YOG/DISPATCHER 34338: disptacher for [/home] cost [3] ms
YOG/DISPATCHER 34338: start action excution [index] with method [GET]
YOG/DISPATCHER 34338: excute action [index] with default action method
session.views:3
输出为3,插件成功
1 session.views
方式二
修改文件 conf/plugins/http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports.http = {
middleware: [
'favicon',
'compression',
'static',
'responseTime',
'cookieParser',
'bodyParser',
'log',
'ral',
'views',
'methodOverride',
require('cookie-session')({
name: 'session',
keys: ['regtergrdg3425345wffbnyir768323453'],
secret : "afdds0098d7as879fdsuio3oih32jkljdsknzyi32",
maxAge: 24 * 60 * 60 * 1000 // 24 hours 单位毫秒,从设置cookie开始多少毫秒失效
}),
'dispatcher',
'notFound',
'error'
]
};
这种方式有点很直接代码简单,缺点是代码层次结构不优雅,复杂的情况不好维护(如:组件依赖、异步回调)。