关于connect的2个session中间件
发布于 9个月前 作者 jy00853684 380 次浏览

express-sessionconnect-session

读了README后还是感觉迷迷糊糊的。写了段代码想看看究竟发生了什么。

var express = require('express')
  , cookieParser = require('cookie-parser')
  , session = require('cookie-session')
  , express_sess = require('express-session')
  , app = express();

app.use(cookieParser())
//app.use(session({ keys: ['abc'], name: 'user' }));
app.use(express_sess({ secret: 'abc', key: 'user'}));
app.get('/', function (req, res, next) {
    res.end(JSON.stringify(req.cookies));
    console.log(req.session)
    console.log(req.cookies)
});

app.listen(3000);

看完输出结果依旧很困惑。对于connect-session,输出都是空对象。对于express-session,输出为

req.session: { cookie: { 
     path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true 
   } 
}
req.cookie: {user: 's:aJ97vKA5CCwxqdTj0AV1siRQ.fWusS5+qfCKICtwkfrzcZ/Gq8P0Qdx/kx8mTBhoOhGU'}

怎么解释这2个中间件的表现,又或者什么时候该用它们?

回到顶部