smart-session

Node.js session handling middleware

Usage no npm install needed!

<script type="module">
  import smartSession from 'https://cdn.skypack.dev/smart-session';
</script>

README

smart-session

Gitter

An ExpressJS Middleware which manages your Session Hassle with a horizontal scaling factor. Node-Redis uses queue to execute your query so I decided to create multiple clients to redis in case you are developing a Super high End Web Product.

How to use

var express = require("express")
var session = require("smart-session");
var app = express();
var config = {
    "redis":{
      "ip": "0.0.0.0",
    },
    "session":{
      "cookieName": "someCoolSessionCookieName" // doesn't matter if you don't give one
    }
}
app.use(session(config));
app.get("/winkCat/:catName", function(request, response){
    if (request.params.catName == "LittleWayne"){
      request.session.add("auth", {"name":"Wayne", wink: true});
      request.session.add("canWink", true);
    }
    if (request.session.canWink){
      response.json({
          "message" : "Oh My Love!"
      });
    } else {
      response.json({
        "message" : "You ain't shit!"
      });
    }
  request.session.update(); //donot forget to do this at the end of each request to achieve scaling! 
}

Well that was just an old fashioned way, isn't it? Lets be smart!

var express = require("express")
var session = require("smart-session");
var app = express();
var config = {
    "redis":{
      "ip": "0.0.0.0",
    },
    "session":{
      "cookieName": "somethingWhichSoundsLikeMIAgentName" // if not then I have a default one
    }
}
app.use(session(config));
app.get("/winkCat/:catName", function(request, response){
    if (request.params.catName == "BruceWayne"){
      request.session.auth = {"name":"DirtyCat", wink: true};
      request.session.canWink =  true;
    }
    if (request.session.canWink){
      response.json({
          "message" : "Oh My Love!"
      });
    } else {
      response.json({
        "message" : "You ain't shit!"
      });
    }
}
// Add this as the last middleware and just stay cool!
app.use(function(req, res, next){
  request.session.update();
  next();
});

Methods

request.session.add(key, value)

Where Key can be any text value and Value, Well value can be anything, JSON object, buffer, I don't know whatever you wanna save.

request.session.update()

Well, when you are done messing up with the session variable, just hit this function and it will save all your mess in a gentle way

request.session.remove(key)

C'mon, the name reads the whole, remove a session variable

request.session.destroy()

Satan = Initialise->Satan()
Satan->KillTheSession()

Scaling

var config = {
    "redis": {
        // Redis details
    },
    clientLimit: someIntegerValueThatDefinesYourScaling //Wooh! That's too long
}

ToDos

  • Redis authentication(a bit lazy will do in few weeks)
  • More Fun!