用于在用户登录后保持用户的回话

引入所需模块

1
2
3
4
5
6
7
8
9
10
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret: 'foo',
store: new MongoStore({
url: dburl,
collection: 'session'
})
}));

保存到本地

1
2
3
4
5
6
app.use(function(req, res, next) {
var _user = req.session.user
app.locals.user = _user
next()
})