Node.js is a server-side JavaScript runtime environment that is fast, cross-platform, modular and can build efficient and stable server-side applications. When developing web applications, SESSION will be used, so how to get the request SESSION information in Node.js? This article will introduce how to obtain the request SESSION from the aspects of the concept of SESSION, the corresponding modules of Session in Node.js and specific API information.
1. The concept of SESSION
SESSION is a cross-request mechanism used to store user information and operations. By saving the SESSION variable, users can stay logged in when visiting different pages of the website. state and pass data between different pages. SESSION is a server-side state retention method, which assigns a unique ID to each session, and then saves the ID on the client (usually in the client's cookie) to achieve communication and communication between the server and the client. track. For each new session, a new ID is created for tracking.
In web development, SESSION can be used to optimize security, improve user experience, realize user specific needs, etc.
2. The use of Session in Node.js
In Node.js, there is a commonly used SESSION module express-session, which can add session support to Express applications. We might as well learn how to use it:
1. Install the express-session module
Enter the following command on the command line:
npm install express-session
2. Introduce express- into the project session:
In your project, add the following code:
var express = require('express'); var session = require('express-session'); var app = express();
3. Use express-session middleware
In your project, add the following code:
app.use(session({ secret: 'keyboard cat',//secret的值建议使用随机字符串 cookie: { maxAge: 60000 }, resave: true, saveUninitialized: true }))
Among them:
4. Set and obtain SESSION
in your project , you can set and obtain SESSION through the following code:
Setting:
req.session.userName="tom";
Getting:
var userName = req.session.userName;
Next, we will use examples to explain how Get request SESSION information in Node.js.
3. Specific API information
In order to better understand how to obtain the requested SESSION information, let’s first understand the API corresponding to SESSION in Node.js.
req.session
This is the request middleware of session, which can realize dialogue control by writing req.session. Usage example is:
req.session.userName='xiaoming';
The above code implements adding userName
to the session. In Express, conversation information is stored in a session, which is an object that can be manipulated like a normal JavaScript object.
req.session.destroy
This attribute indicates that when the user exits, the data saved in the session will be cleared. Usage examples are:
req.session.destroy(function(err) { // cannot access session here })
When the session is destroyed, the callback function will be executed.
4. Example Demonstration
Next, we use an example to demonstrate how to obtain the request SESSION information.
1. Create the project
First, initialize the project and create the main.js file:
mkdir node-app && cd node-app npm init touch main.js
2. Install express and express-session and introduce
Enter the following command in the command line to install express and express-session and import:
npm install express --save npm install express-session --save
Write the following code in main.js:
const express = require('express') const session = require('express-session') const app = express() app.use(session({ secret: 'keyboard cat',//secret的值建议使用随机字符串 cookie: { maxAge: 60000 }, resave: true, saveUninitialized: true })) app.get('/login', (req, res) => { req.session.userName = 'Qiming' res.send('login success') }) app.get('/home', (req, res) => { let userName = req.session.userName if (userName) { res.send(`welcome ${userName}`) } else { res.send('please login first') } }) const server = app.listen(3000, () => { console.log(`app is running at http://localhost:${server.address().port}`) })
In the above code:
3. Run the project and test
Run the following command in the terminal:
node main.js
Open the browser and visit http: //localhost:3000/login, get the "login success" message, visit http://localhost:3000/home, get the "welcome Qiming" message, indicating that the SESSION is obtained successfully.
5. Summary
In this article, we have learned about the concept of SESSION, the use of SESSION in Node.js, specific API information and a demonstration example, hoping to help everyone better understand Learn how to get request SESSION information in Node.js. In actual projects, how to use SESSION needs to be decided according to the actual situation, and can be implemented according to business needs.
The above is the detailed content of nodejs gets request session. For more information, please follow other related articles on the PHP Chinese website!