Horje
How to Count Number of Visits to a Website using Express.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 article, we will see how to count the Number of views in Express Session in Express Js.

Prerequisites:

  • Basic knowledge of  Node.
  • Node.js installed (version 12+).
  • npm installed (version 6+).

Approach

  • Use Express.js with express-session to track website visits.
  • Store the session data on each visit, incrementing a counter.
  • Retrieve and display the count to show the number of visits.

Steps to Count Number of Visits using Express

Step 1: Project SetUp

To set up Node Project in Editor you can see here.

Step 2: Installing requires modules

npm install express
npm install express-session

Step 3: Call API

const session = require('express-session')

Project Structure:

project-structure-node

project sructure

Updated dependencies in package.json file

"dependencies": {
"express": "^4.19.2",
"express-session": "^1.18.0",
}

Example: This example illustrates the above approach.

Node
// index.js

// Call Express Api.
const express = require('express'),

// Call express Session Api.
session = require('express-session'),

app = express();

// Session Setup
app.use(
    session({

        // It holds the secret key for session
        secret: "I am girl",

        // 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: false,
        cookie: {
        })
);

// Get function in which send session as routes.
app.get('/session', function (req, res, next) {

    if (req.session.views) {

        // Increment the number of views.
        req.session.views++

        // Print the views.
        res.write('<p> No. of views: '
            + req.session.views + '</p>')
        res.end()
    } else {
        req.session.views = 1
        res.end(' New session is started')
    }
})

// The server object listens on port 3000.
app.listen(3000, function () {
    console.log("Express Started on Port 3000");
});

Run the index.js file using the below command.

node app.js

Now to set your session, just open the browser and type this URL.

http://localhost:3000/session

Output:




Reffered: https://www.geeksforgeeks.org


Express.js

Related
What is MEVN Stack ? What is MEVN Stack ?
Working of Express.js middleware and its benefits Working of Express.js middleware and its benefits
Requesting in http vs Requesting in Express.js Requesting in http vs Requesting in Express.js
How to validate if input in input field has full width string only using express-validator ? How to validate if input in input field has full width string only using express-validator ?
How to validate if input in input field has hexadecimal number only using express-validator ? How to validate if input in input field has hexadecimal number only using express-validator ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12