How to manage sessions? Use redis to manage sessions under nodejsexpress

Source: Internet
Author: User
This article mainly introduces how to use redis to manage sessions under nodejsexpress, which has some reference value. If you are interested, you can refer to it. This article mainly introduces how to use redis to manage sessions under nodejs express, which has some reference value. If you are interested, you can refer to it.

Session implementation principle

There are many ways to implement authentication requests. One of the most widely accepted methods is to use the Session ID generated by the server side and the Cookie of the browser to manage sessions. Generally, the following four steps are involved:

1. The Session ID generated by the server

2. Session ID stored on the server and client

3. Extract Session ID from HTTP Header

4. Obtain the requester identity information from the server Hash Based on the Session ID

Session management using Express and Redis

var session = require('express-session');var RedisStrore = require('connect-redis')(session);var config={"cookie" : {  "maxAge" : 1800000}, "sessionStore" : {  "host" : "192.168.0.13",  "port" : "6379",  "pass" : "123456",  "db" : 1,  "ttl" : 1800,  "logErrors" : true}app.use(session({  name : "sid",  secret : 'Asecret123-',  resave : true,  rolling:true,  saveUninitialized : false,  cookie : config.cookie,  store : new RedisStrore(config.sessionStore)}));

Implementation Stack
Call the code after the express-session is instantiated (github.com/expressjs/session)

if (!req.sessionID) {  debug('no SID sent, generating session'); generate(); next(); return;}

Generate method call (github.com/expressjs/session)

store.generate = function(req){req.sessionID = generateId(req);req.session = new Session(req);req.session.cookie = new Cookie(cookieOptions); if (cookieOptions.secure === 'auto') {   req.session.cookie.secure = issecure(req, trustProxy);  } };

Call store. set (sid, session, callback) (github.com/expressjs/session) during RedisStrore instantiation)

Store. set calls RedisStore. prototype. set (github.com/tj/connect-redis), where the seat hashkey uses the prefix + sessonId, and the default prefix is 'sess'. When multiple applications share and do not share the same redis session service, be sure to set prefix

RedisStore.prototype.set = function (sid, sess, fn) {  var store = this;  var args = [store.prefix + sid];  if (!fn) fn = noop;  try {   var jsess = store.serializer.stringify(sess);  }  catch (er) {   return fn(er);  } args.push(jsess); if (!store.disableTTL) {   var ttl = getTTL(store, sess);   args.push('EX', ttl);   debug('SET "%s" %s ttl:%s', sid, jsess, ttl);  } else {   debug('SET "%s" %s', sid, jsess);  } store.client.set(args, function (er) {   if (er) return fn(er);   debug('SET complete');   fn.apply(null, arguments);  }); };

Store. client. set is called (github.com/NodeRedis/node_redis)

Finally, the native redis. hset method is called.

The above is how to manage the session? Use redis to manage sessions in nodejs express. For more information, see other related articles in the first PHP community!

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.