How to set sessions in Node JS

I am working with the session today in my current node js project. Actually i am creating a login page where i need to save user data in session and display him successfully logged in page.
First of all you need to install express node module "express-session". Below is the steps to set session in node js -

Step 1- First install session module by running below command

npm install express-session --save

Step 2- Initialization Session module on your server page like below

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

Step 3- Now store your data into session like below 

var user_id = 25;
sess.user_id = user_id;

Step 4- Now it time to Access stored Session like below


Don't forget to leave a comment below if you like this.

Happy Coding :)


Previous Post Next Post