We now have a youtube channel. Subscribe!

Express.js | Sessions

Express.js | Sessions


Hello folks! Welcome back to a new edition of our tutorial on ExpressJS. In this tutorial guide, we will be discussing about sessions in ExpressJS.

HTTP is stateless; to associate a request to any other request, you need a way to store user data between HTTP requests. Cookies and URL parameters are both suitable ways to transport data between the client and the server. But they're both readable and on the client side. Sessions solve this problem. You assign the client an ID and it makes further requests making use of that ID. Information associated with the client is stored on the server linked to this ID.

We are going to require Express-session, so install it using the following code.

npm install --save express-session

We will put the session and cookie-parser in place. In this example, we'll use the default store for storing sessions, i.e., MemoryStore. Never use this in production environments. The session middleware handles all things for us, i.e., creating the session, setting the session cookie and creating of the session object in req object.

Anytime we make a request from the same client once again, we will have their session information stored with us (given the server was not restarted). We can add much more properties to the session object.

Example

In the following example, we'll create a view counter for a client.

var express = require('express');
var cookieParser = require('cookie-parser');
var session = require('express-session');

var app = express();

app.use(cookieParser());
app.use(session({secret: "Shh, its a secret!"}));

app.get('/', function(req, res){
   if(req.session.page_views){
      req.session.page_views++;
      res.send("You visited this page " + req.session.page_views + " times");
   } else {
      req.session.page_views = 1;
      res.send("Welcome to this page for the first time!");
   }
});
app.listen(3000);


What the above code does is, when a user visits the site, it creates a new session for the user and assigns them a cookie. When next the user comes, the cookie is checked and the page_view session variable updates accordingly.

Output

If you run the app and go to localhost:3000, the following output will be displayed -

session_first

If you revisit the page, the page counter will increase. The page in the below screenshot was refreshed 15 times.

session_15


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial guide, we will be discussing about Authentication in ExpressJS.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain