Session Management using express-session Module in Node.js

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:

  1. You can visit the link Install express-session module. You can install this package by using this command.
    npm install express-session
  2. After installing express-session you can check your express-session version in command prompt using the command.
    npm version express-session
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
})

chevron_right


Steps to run the program:

  1. The project structure will look like this:
    project structure
  2. Make sure you have install express and express-session module using following commands:
    npm install express
    npm install express-session
  3. Run index.js file using below command:
    node index.js

    Output of above command

  4. Now to set your session, just open browser and type this URL:
    http://localhost:3000/

  5. 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.




My Personal Notes arrow_drop_up

To obtain a position of responsibility that utilizes my skills and keen to work in an environment where I can enrich my knowledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.