Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use session in node

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people don't understand the knowledge points of this article "how to use session in node", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to use session in node".

In node, session is used for interaction between visitors and websites, and is used to track the identity of client users. It is called session information;session will be passed to the web server and matched with access information when accessing the http address requested by the browser;session data is stored on the server and cannot be stored permanently.

Operating environment for this tutorial: Windows 10, NodeJS version 12.19.0, Dell G3 PC.

What is the use of session in node?

Session is called session information, located on the web server, mainly responsible for the interaction between visitors and the website. When the browser requests http address, it will be passed to the web server and matched with the access information. When the website is closed, it means that the session has ended. The website cannot access the information, so it cannot save permanent data. We cannot access and disable the website.

Cookies and sessions are ways to track the identity of a client user. They provide great convenience for multi-page web, allowing the server to clearly identify different users, but they also have certain differences.

Difference: cookie data is stored on the client side, session data is stored on the server side.

session

installation module

cnpm install express-session

introduced

var express = require('express');var session = require('express-session');var app = express();app.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: true, cookie: {maxAge: 60000}}));

use

router.get('/set', function (req, res, next) { //Set session req.session.username = 'zhangsan'; res.send('ok');});router.get('/get', function (req, res, next) { //Get session console.log(req.session); console.log(req.session.username); res.send ('session 'value:'+ req.session);});

Note: session only exists in the current session, a broken connection, session disappears

The above is the content of this article about "how to use session in node". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report