1.Cookie

安装解析客户端传来的cookie的中间件

npm i cookie-parser --save

引入并使用

const cookieParser = require('cookie-parser');
app.use(cookieParser()); 

2.Session以及保存Session的Redis

安装express-session以及redis的中间件

npm i express-session connect-redis --save

引入并且使用

const session = require('express-session')
const RedisStore = require('connect-redis')(session)
const redisClient = require('./db/redis.js')
//连接redis
const sessionStore = new RedisStore({
  client: {
     port:xxxx,
     host:xxxx
  }
})

//处理 session
app.use(session({
  secret: "wumaoorg", //自定义密匙
  cookie: {
    // path: '/', // 默认配置可不写
    // httpOnly: true, // 默认配置可不写
    maxAge: 24 * 60 * 60 * 1000 //设置过期时间
  },
  store: sessionStore
}))
Last modification:March 19th, 2020 at 08:37 pm
If you think my article is useful to you, please feel free to appreciate