Session Management using express-session Module in Node.js Last Updated : 28 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Session management can be done in node.js by using the express-session module. It helps in saving the data in the key-value form. In this module, the session data is not saved in the cookie itself, just the session ID. Installation of express-session module: You can visit the link Install express-session module. You can install this package by using this command. npm install express-session After installing express-session you can check your express-session version in command prompt using the command. npm version express-session After that, you can create a folder and add a file for example index.js, To run this file you need to run the following command. node index.js Filename: index.js javascript const express = require("express") const session = require('express-session') const app = express() // Port Number Setup var PORT = process.env.port || 3000 // Session Setup app.use(session({ // It holds the secret key for session secret: 'Your_Secret_Key', // Forces the session to be saved // back to the session store resave: true, // Forces a session that is "uninitialized" // to be saved to the store saveUninitialized: true })) app.get("/", function(req, res){ // req.session.key = value req.session.name = 'GeeksforGeeks' return res.send("Session Set") }) app.get("/session", function(req, res){ var name = req.session.name return res.send(name) /* To destroy session you can use this function req.session.destroy(function(error){ console.log("Session Destroyed") }) */ }) app.listen(PORT, function(error){ if(error) throw error console.log("Server created Successfully on PORT :", PORT) }) Steps to run the program: The project structure will look like this: Make sure you have install express and express-session module using following commands: npm install express npm install express-session Run index.js file using below command: node index.js Now to set your session, just open browser and type this URL: http://localhost:3000/ Till now, you have set session and to see session value, type this URL: http://localhost:3000/session So this is how you can do session management in node.js using the express-session module. Comment More infoAdvertise with us Next Article Session Management using express-session Module in Node.js G gouravhammad Follow Improve Article Tags : JavaScript Web Technologies Node.js Write From Home Node.js-Misc +1 More Similar Reads How to handle sessions in Express ? ExpressJS is a small framework that works on top of Node web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. It adds helpful utilities to Node HTTP objects and facilitates the render 4 min read How to Manage Sessions and Cookies in Express JS? Express is a small framework that sits on top of NodeJS web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. It adds helpful utilities to NodeJS HTTP objects, it helps the rendering of 4 min read How to Display Flash Messages using connect-flash Module in Node.js ? Connect-flash module for Node.js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.Prerequisites Bef 3 min read How to Use Handle Get Request in Express.js ? Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in 2 min read Node.js Server Side Rendering (SSR) using EJS Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situ 3 min read Like