In this article, we will explain to you how to Node js session Management Using Express Session module. so we will give you a simple example of how to store data in session in node js.

sometimes we need to store data into the server-side. At that time we use session.

We will create a node js application with express-session (express-session example). in this example, we use the npm express-session. An express-session is a nodejs npm package that helps to manage sessions in the nodejs application.

Node Js Application Setup

First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.

mkdir my_node_app
cd my_node_app
npm init

Install NPM Package

After the done setup node js application, we will install the express, cookie-parser and express-session npm package. so you can see the following npm command.

npm install express cookie-parser express-session --save

How To Set Session Data Using Express Session

In this step, We will set session data using express session in node js application. so you can see our nodejs session example.

var express=require('express');
var cookieParser = require('cookie-parser'); 
var session = require('express-session');
var app = express();
app.use(cookieParser());
app.use(session({secret:'secretkey', saveUninitialized : true, resave : true})); 

app.get('/set_session',function(req,res){
	// To set session data
	req.session.username = 'admin';
    res.send('set session username ...!');
});

app.listen(3000,function(){
	console.log("Express Started on Port 3000");
});

How To Get Session Data Using Express Session

In this step, We will get the session data using express session in node js application. so you can see our express-session example.

var express=require('express');
var cookieParser = require('cookie-parser'); 
var session = require('express-session');
var app = express();
app.use(cookieParser());
app.use(session({secret:'secretkey', saveUninitialized : true, resave : true})); 

app.get('/get_session',function(req,res){
	// To get session data
	res.send('Username: ' + (req.session.username== undefined?"NOT FOUND":req.session.username));
});

app.listen(3000,function(){
	console.log("Express Started on Port 3000");
});

How To Destroy Session Data Using Express Session

In this step, We will destroy the session data using express session in node js application.

var express=require('express');
var cookieParser = require('cookie-parser'); 
var session = require('express-session');
var app = express();
app.use(cookieParser());
app.use(session({secret:'secretkey', saveUninitialized : true, resave : true})); 

app.get('/destroy_session',function(req,res){

	// To destroy session data
	req.session.username.destroy(function(err) {
        if(err){
			res.send('Error destroying session');
        }else{			
			res.send('Session destroy successfully');
        }
    });
});

app.listen(3000,function(){
	console.log("Express Started on Port 3000");
});

Run Node js Application

we will run the node js application using the below command. so you can follow the below command.

node server.js

Now you can run the example using the below Url in the browser.

http://localhost:3000/set_session
http://localhost:3000/get_session
http://localhost:3000/destroy_session