Session Management using express-session Module in Node.js Last Updated : 28 Apr, 2020 Comments Improve Suggest changes 16 Likes 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. Create Quiz Comment G gouravhammad Follow 16 Improve G gouravhammad Follow 16 Improve Article Tags : Node.js Node.js-Misc Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like