SESSION MANAGEMENT USING EXPRESS-SESSION NODE.JS

Vijin_Vinod
3 min readFeb 6, 2023

--

Session management is a crucial aspect of web development. It enables the server to remember the state of a user between multiple requests. In this blog, we will discuss how to manage sessions using the Express Session middleware in a Node.js web application.

What is Express Session?

Express Session is a middleware for Node.js that provides a simple way to manage sessions in an Express web application. It allows you to store information about a user’s session on the server-side, making it accessible across multiple requests.

Getting Started Guide for Express Session

  1. This is a Node.js project. It uses NPM to manage its dependencies. You need to create a new project directory and initialize the node app using:
npm init -y

2. Install Express and Express Session: To use Express Session, you first need to have Express installed in your project. You can install Express by running the following command in your terminal:

npm install express
npm install express-session

3. Require Express Session in your Express application: In your server.js file, require the express-session module and use it in your Express application with the “app.use” method. Here's an example of how to use Express Session in your application:

const express = require('express');
const session = require('express-session');

const app = express();

app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));

The “app.use” method in Express is used to mount middleware functions at a specified path. In the case of the Express Session middleware, the app.use method is used to initialize the Express Session module and make it available for use in the application.

The code “app.use(session({...}))” is mounting the Express Session middleware with the options passed in the object. The options passed in the object include:

secret: a string used to sign the session ID cookie.

resave: a boolean value that determines whether the session should be saved even if it hasn't been modified. If set to false, the session will only be saved if it has been modified.

saveUninitialized: a boolean value that determines whether a new, uninitialized session should be saved even if it has not been modified.

cookie: an object that contains options for the session ID cookie. For example, the secure property is set to false, indicating that the cookie can be sent over an unencrypted connection.

With the “app.use” method, the Express Session middleware is mounted for use in the application, allowing you to store and access session information in the req.session object.

4. Set values in the req.session object: You can use the req.session object to store information about a user's session. For example, after a user logs in, you can store their user ID in the session:

app.post('/login', function(req, res) {
req.session.userId = user.id;
res.redirect('/');
});

5. Access values in the req.session object: To access the values stored in the req.session object, simply read the appropriate property. For example, to access the user ID stored in the session, you can use the following code:

app.get('/', function(req, res) {
console.log(req.session.userId);
res.send('Hello World');
});

6. Destroy sessions: When a user logs out, it’s important to destroy their session to ensure that their information is no longer accessible on the server. To destroy a session, simply set the req.session object to null:

app.get('/logout', function(req, res) {
req.session.destory();
res.redirect('/');
});

OR


app.get('/logout', function(req, res) {
req.session = null;
res.redirect('/');
});

Conclusion

Session management is a crucial aspect of web development and Express Session makes it easy to manage sessions in an Express application. By using sessions, you can store information about a user’s session on the server-side and access it across multiple requests. With the steps outlined in this blog, you should be able to implement session management in your own Express applications.

--

--