The operation state of nodejs maintains technical cookie s and session s

Posted by poring on Mon, 27 Dec 2021 03:03:30 +0100

catalogue

3.1,cookie

3.2,session

  • Because http is a stateless protocol, the browser request server is stateless.
  • Stateless: when a user requests, the browser and server cannot know what the user has done before. Each request is a new request.
  • Stateless reason: the browser communicates with the server using socket. After the server returns the request result to the browser, the current socket connection will be closed, and the server will destroy the page object after processing the page.
  • Sometimes it is necessary to maintain the user's browsing state, such as whether the user has logged in, which products have been browsed, etc
  • There are two main ways to maintain the status:
    • Use cookies to store information on the client
    • Use Session to store information on the server side

Stateless protocol:

  1. The protocol has no memory for transaction processing
  2. There is no context for the same url request
  3. Each request is independent. Its execution and results are not directly related to the previous request and subsequent requests. It will not be directly affected by the previous request response or the subsequent request response
  4. The state of the client is not saved in the server. The client must bring its own state to the server every time
  5. if time could stop at the moment when we first met

Status example:

  • Status:
    • A: What did you have for lunch today?
    • B: Eat a large plate of chicken.
    • A: How does it taste?
    • B: Not bad. It's delicious.
  • Stateless:
    • A: What did you have for lunch today?
    • B: Eat a large plate of chicken.
    • A: How does it taste?
    • B: ??? Ah? What? What does it taste like?
  • So you need cookie s:
    • A: What did you have for lunch today?
    • B: Eat a large plate of chicken.
    • A: How did the big plate of chicken you ate this noon taste?
    • B: Not bad. It's delicious.

3.1,cookie

characteristic:

1. The cookie is generated by the server and stored in a short piece of text information on the browser side

2. Cookies are stored as keys and values

3. When the browser accesses the server of a website, it will automatically send all cookie s related to the website to the server in the request header

4. Cookies are based on domain name security

5. Cookies have expiration time. By default, they expire after the browser is closed

use:

First install and import: Cookie parser

Register in app:

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

Set Cookie: res.cookie ('name ',' node ', {maxage: 1000 * 60 * 60 * 2});

Get cookie: let name = req cookies["name"]

The complete code is as follows:

// 1. Install cookie parser
// 2. Introduce a cookie parser and register it in the app
const cookieParase = require('cookie-parser');
app.use(cookieParase());

app.get("/setCookie",(req,res)=>{
    //Set cookie s
    res.cookie('name', "node", {maxAge: 1000 * 60 * 60 * 2 });    // Expiration time: in milliseconds
    res.cookie('age', 11); 
    res.send("Set cookie")
})

app.get("/getCookie",(req,res)=>{
    //Get cookie information
    let name = req.cookies["name"];
    let age = req.cookies["age"];
    res.send(`obtain cookie, ${name}, ${age}`);
})

3.2,session

characteristic:

1. The session data is saved on the server side

2. session is stored in the form of keys and values

3. The session depends on the cookie, and the identification of the client corresponding to each session information is saved in the cookie

use:

First install and import: Cookie session

const cookieSession = require('cookie-session');

Register in app:

app.use(cookieSession({
    name:"my_session",  //Name is the session name. Just start a string yourself
    keys:["$^%%&^&%$RT%&TYGSGSFRR554785432$#@$$#%$@#%"], / / keys required for internal encryption. Keys are random strings
    maxAge: 1000 * 60 * 60 * 24   // Expiration time
}))

Set session: req session["name"] = "session_node"

Get session: let name = req session["name"]

The complete code is as follows:

// 1. Install: yarn add cookie session first     
//    If an error is reported, add the version number later, and then press enter all the time- session@2.0.0
// 2. Set to app  
const cookieSession = require('cookie-session');
app.use(cookieSession({
    name:"my_session",  //Name is the session name. Just start a string yourself
    keys:["$^%%&^&%$RT%&TYGSGSFRR554785432$#@$$#%$@#%"], / / keys required for internal encryption. Keys are random strings
    maxAge: 1000 * 60 * 60 * 24   // Expiration time
}))

app.get("/setSession",(req,res)=>{
    //Set session
    req.session["name"] = "session_node"
    req.session["age"] = 11 
    
    res.send("Set Session")
})

app.get("/getSession",(req,res)=>{
    //Get session information
    let name = req.session["name"]
    let age = req.session["age"]
    res.send(`obtain Session, ${name}, ${age}`);
})

Topics: node.js