npm install --save express-session
var session = require('express-session')
设置session-cookie
const Options = {
secret: 'keyboard cat', // 必须,This is the secret used to sign the session ID cookie
genid: function(req) {
return genuuid(); // use UUIDs for session IDs, 默认使用 uid-safe library 来生成 IDs.
},
name: 'session_id', // 设置 session ID cookie 名字, 默认 'connect.sid'
proxy: undefined, // Trust the reverse proxy when setting secure cookies (via the "X-Forwarded-Proto" header).
resave: false,
rolling: '',
saveUninitialized: true,
store: '', // The session store instance, defaults to a new MemoryStore instance
unset: '', // Control the result of unsetting req.session
cookie: {
domain: 'localhost:3000',
expires: null, // 过期时间,一般只设maxAge,不直接设置expires
httpOnly: true, // HttpOnly = true 的 cookie 不能被 js 获取
maxAge: 60 * 60 * 1000,
path: '/', // 默认域的根路径
sameSite: false, // 是否禁用第三方 cookie 的设置,true , false , strict, lax 四种模式
secure: false // 设置了 secure = true 只能由 https 发送
}
// cookie default value is { path: '/', httpOnly: true, secure: false, maxAge: null }
};
空出没填参数的表示不清楚
app.use(cookieParser(Options.secret));
!注意先后关系
app.use(session(Options))