Node js Session Management Using Express Session
In this article, we will explain to you how to Node js session Management Using the Express Session module. 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. An express-session is a nodejs npm package that helps to manage session into 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.
1 2 3 | 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.
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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.
1 | node server.js |
Now you can run the example using the below Url in the browser.
1 2 3 | http://localhost:3000/set_session http://localhost:3000/get_session http://localhost:3000/destroy_session |