node express js 4.x设置cookie
通过Google搜出来的结果有
https://github.com/pillarjs/cookies
https://github.com/expressjs/cookie-parser
https://github.com/expressjs/cookie-session
把我搞混了,我现在需要的功能是检测cookie,设置session。
也就是常规的判断seesion状态,如果无seesion,登录后设置cookie这样一个功能,有没有简单的demo实现?
noteli
10 years ago
Answers
我是这样用的:
//...
var cookieParser = require('cookie-parser');
var session = require('express-session');
//...
app.use(cookieParser());
app.use(session({
key: 'session',
secret: 'keyboardcat',
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
//...
router.get('/login', function(req, res, next) {
// 读取 cookie
var user = req.session.user;
if (user) {
return res.redirect('/');
}
//..
});
router.post('/login', function(req, res, next) {
//...
// 设置 cookie
req.session.user = user;
return res.redirect('/');
});
有追求的美男子
answered 10 years ago
demo: http://www.hubwiz.com/course/549a704f88dba0136c371703/ ,这是个nodejs+mongodb的实战的demo,里面有session的设置。
Kirin.P
answered 10 years ago