根据
https://github.com/mozilla/node-client-sessions#usage
var sessions = require("client-sessions"); app.use(sessions({ cookieName: 'mySession',// cookie name dictates the key name added to the request object secret: 'blargadeeblargblarg',// should be a large unguessable string duration: 24 * 60 * 60 * 1000,// how long the session will stay valid in ms activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration,the session will be extended by activeDuration milliseconds }));
我希望用户的会话最多有效30天.如果我将持续时间设置为30天,将活动持续时间设置为30天,我想我最终将会有60天的会话时间.
如果我将持续时间设置为15天并且将活动持续时间设置为15天,那么登录的用户不会在15天内完成任何操作(不是我希望那种用户行为,但我的假设是否正确? )
解决方法
您可以使用快速会话NPM来实现您的功能:
码:
let session = require('express-session') let time = new Date(Date.now() + (30 * 86400 * 1000)) let app = express() let sess = { secret: 'keyboard_cat',cookie: { maxAge: time } } if (app.get('env') === 'production') { app.set('trust proxy',1) // trust first proxy sess.cookie.secure = true // serve secure cookies } app.use(session(sess)) app.use(session({ genid: function(req) { return genuuid() // use UUIDs for session IDs },secret: 'keyboard cat' })) //call your route and do session things here app.get('/',function(req,res,next) { if (req.session.views) { req.session.views++ res.setHeader('Content-Type','text/html') res.write('<p>views: ' + req.session.views + '</p>') res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>') res.end() } else { req.session.views = 1 res.end('welcome to the session demo. refresh!') } })
有关更多信息,请参阅此链