nodejs express session用法(含保存到redis)

普通用法:

 1 var express = require('express');
 2 var session = require('express-session');
 3 
 4 var app = express();
 5 
 6 app.use(session({
 7     name: 'test1',                // 非常重要,用于区分两个系统的session
 8     secret: 'test1 cat',
 9     cookie: { maxAge: 5 * 60 * 60 * 1000 },
10     resave: true,
11     saveUninitialized: true
12 }));

 

session保存到redis:

 1 var express = require('express');
 2 var session = require('express-session');
 3 var redisStore = require('connect-redis')(session);
 4 
 5 var app = express();
 6 
 7 
 8 var redisCfg = {
 9     host:"127.0.0.1",
10     port:"6379",
11     auth_pass:"",
12     db:0
13 };
14 
15 app.use(session({
16     store: new redisStore(redisCfg),
17     secret: 'test', // 可修改成自己的
18     cookie: { maxAge: 10 * 60 * 1000 },
19     resave: true,
20     saveUninitialized: false
21 }));

 

posted @ 2017-06-30 10:41  L狗哥  阅读(1001)  评论(0编辑  收藏  举报