Node. js middleware express-session Usage Details, nodeexpress Middleware

Source: Internet
Author: User

Node. js middleware express-session Usage Details, nodeexpress Middleware

This article introduces the node. js middleware express-session and shares it for your reference. Let's take a look at the details below:

I. Why use session?

Session runs on the server. When the client accesses the server for the first time, it can save the client's logon information.

When a customer accesses other pages, the customer's logon status can be determined and prompted, which is equivalent to logon interception.

Session can be used together with Redis or database for persistence. When the server fails, some customer information (Shopping Cart) will not be lost.

Ii. session workflow:

When the browser accesses the server and sends the first request, the server creates a session object, generates a key-value pair similar to key and value, and then) return to the browser (customer). the browser will carry the key (cookie) and find the correspondingsession(value) . The customer information is stored in the session.

Iii. Common Parameters of express-session:

Secret: A String type String that is used as the server side to generate the session signature.

Name: return the name of the client key. The default value is connect. sid. You can also set it yourself.

Resave: (allowed or not) when the client sends multiple requests in parallel, one of the requests overwrites and saves the session when the other request ends.

The default value is true. However, (later versions) may be invalid by default, so it is best to manually add it.

SaveUninitialized: whether to save the session to storage during initialization. The default value is true, but (later versions) may be invalid by default, so it is best to manually add.

Cookie: set the attribute returned to the front-end key. The default value is{ path: ‘/', httpOnly: true, secure: false, maxAge: null }.

Some methods of express-session:

Session.destroy(): Deletes a session, which is called when the client is detected to be closed.

Session.reload(): When the session has been modified, refresh the session.

Session.regenerate() : Initialize an existing session.

Session.save() : Save the session.

Iv. demo

// App. add the following code to js (existing codes do not need to be added) var express = require ('express '); var cookieParser = require ('cookie-parser '); var session = require ('express-session'); app. use (cookieParser ('sessiontest'); app. use (session ({secret: 'sessiontest', // consistent with resave: true, saveUninitialized: true} In cookieParser }));
// Modify router/index. js. When the first request is sent, we save a piece of user information. Router. get ('/', function (req, res, next) {var user = {name: "Chen-xy", age: "22", address: "bj"} req. session. user = user; res. render ('index', {title: 'test for nodejs session', name: 'sessiontest '});});
// Modify router/users. js to determine whether the user logs in. Router. get ('/', function (req, res, next) {if (req. session. user) {var user = req. session. user; var name = user. name; res. send (' '+ name +'. Welcome to my home. ');} Else {res. send (' You have not logged on, log on to the directory first and try again! ');}});

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.