let express = require('express'); let cookieParser = require('cookie-parser'); let expressSession = require('express-session'); let ejs = require('ejs'); let app = express(); app.use(cookieParser());//使用时需要调用cookieParser(); //s%3AOiJBUiPT2BOjlZuZl1T7iqiFuLOEG8uP.uNNq5Z%2Fl7F9ALI5zsKb2Zm3KXgoABxoriHv873hZ%2BZ0 app.use(expressSession({ secret: ['aaaa','c'],//加密字符串,随机抽取 rolling: true, // 永远在线隔一段时间去刷新session resave: true, // // name: 'wpwpwp', //更改名称 最外面的 对象中 cookie:{ httpOnly: false, //这个代表 只能在服务器中获取 验证 maxAge: 1000*5,//时效 // path: '/aa' } })); // app.use(function (req, res, next) { // if(req.session.name){ // req.session.name = 1; // } // }); app.get('/',function (req, res) { // //获取session // console.log(req.session); // //设置session // req.session.name = 1; // res.send('end') console.log(req.session); req.session.num = 1; ejs.renderFile(__dirname+'/template/index.html',{},function (err,data) { res.send(data) }) }); app.listen(3000,function () { console.log('start') });
利用cookie-session 去判断用户是否登录。使用签名去判断。
let express = require('express'); let cookieParser = require('cookie-parser'); let expressSession = require('express-session'); let fileStore = require('session-file-store')(expressSession); let app = express(); app.use(cookieParser()); app.use(expressSession({ store: new fileStore(), secret: ['aaaa','c'], cookie:{ maxAge: 1000*60, } })); app.get('/',function (req, res) { console.log(req.session); //逻辑判断 //设置session 帮你写到 服务器的内存中 //又帮你写了一个 cookie携带这session信息 req.session.num = 1; res.send('end') }); app.listen(3000,function () { console.log('start') });