Monday, April 11, 2016

Node.JS with Express Session

In here we will try to mange session in node application.

Here are the dependencies which is used in this sample

"dependencies":

    {
        "express": "^4.8.7",
        "express-session": "^1.7.6"
    }

express-session module needs express. Therefore you have to add express in your project also.

var express = require('express');
var session = require('express-session');
var app = express();

session can be initialized by below code. Here ‘secret‘ is used for cookie handling

app.use(session({secret: 'secret cat'}));

After creating Session, Session variables can be introduced as 'appsession.username'.

var appsession;
app.get('/',function(req,res){
    appsession=req.session;
    appsession.username;
});

 

[1] https://github.com/expressjs/body-parser

[2] https://github.com/expressjs/session

No comments:

Post a Comment