Include Module:
let session = require(`express-session`)
Use Middleware:
app.use(session({
secret: `your secret string`,
resave: false,
saveUninitialized: false,
cookie: {
// how long the session will last
// put null to have session end when client closes browser
maxAge: someMilliseconds
}
}))
Set Session Variable: Access the session object from the
request object, and add your own variables to the session object. Those
variables will be available to that client only:
request.session.yourVariable = something
Get Session Variable: Access the session object from the
request object, and access your variables from the session object. You can
access variables that were added by that client only:
request.session.yourVariable
Destroy Session: Access the session object from the
request object, and remove all variables that were added by that client:
request.session.destroy()