node session management - express-session

Express-session is the middleware that really saves data on the server, and it relies on cookie-parser.

app.jsIntroduced in,  mounted after app.use(cookieParser())  :

var session = require('express-session');
app.use(session([options]));

Also talk about a few commonly used  options:

{
     'secret': 'ruidoc', #Signature, which must be consistent with the signature string set by the cookie in cookie-parser
     'cookie' : {
         'maxAge': 90000
    },
    'name': 'session_id'     # Generate the name key of the cookie in the browser, the default is connect.sid
}

Because a cookie is created to save the sessionId when the session is created   , the cookie.maxAge in options can be regarded as the valid duration of the session .

 

create session

# create a session
req.session.key = value

# create multiple sessions
req.session = {
    key1: value1,
    key2: value2
}

 

get session

var session = req.session # Get the session set
 var value = req.session.key # Get the value of the session named key

 

destroy session

req.session.destroy() # Clear all sessions
req.session.key.destroy() # Destroy the value of the session named key

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383045&siteId=291194637