npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hy-visitor-counter

v1.0.1

Published

Nodejs module that counts the number of visitors accessing an application or website

Downloads

2

Readme

API Visitor Counter Node JS

Get Started

Install Modules

npm i hy-visitor-counter

Import Modules

const visitorCounter = require('hy-visitor-counter');

Create Middleware to Process Request

app.use((req, res, next) => {
    visitorCounter.incrementVisitor(req);
    next();
});

Create Middleware Total Visitors

app.use((req, res, next) => {
    res.locals.totalVisitors = visitorCounter.getVisitorCount();
    next();
});

Endpoint Visitors

app.get('/api/visitors', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;

    const visitors = visitorCounter.getVisitors().slice(startIndex, endIndex);

    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(res.locals.totalVisitors / limit),
        visitors: visitors
    });
});

Endpoint Daily Visitors

app.get('/api/stats/daily', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;

    const dailyVisits = visitorCounter.getDailyVisits();
    const slicedData = Object.entries(dailyVisits).slice(startIndex, endIndex);
    const slicedDailyVisits = Object.fromEntries(slicedData);

    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(dailyVisits).length / limit),
        dailyVisits: slicedDailyVisits
    });
});

Endpoint Weekly Visitors

app.get('/api/stats/weekly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;

    const weeklyVisits = visitorCounter.getWeeklyVisits();
    const slicedData = Object.entries(weeklyVisits).slice(startIndex, endIndex);
    const slicedWeeklyVisits = Object.fromEntries(slicedData);

    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(weeklyVisits).length / limit),
        weeklyVisits: slicedWeeklyVisits
    });
});

Endpoint Monthly Visitors

app.get('/api/stats/monthly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;

    const monthlyVisits = visitorCounter.getMonthlyVisits();
    const slicedData = Object.entries(monthlyVisits).slice(startIndex, endIndex);
    const slicedMonthlyVisits = Object.fromEntries(slicedData);

    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(monthlyVisits).length / limit),
        monthlyVisits: slicedMonthlyVisits
    });
});

Endpoint Annually Visitors

app.get('/api/stats/yearly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;

    const yearlyVisits = visitorCounter.getYearlyVisits();
    const slicedData = Object.entries(yearlyVisits).slice(startIndex, endIndex);
    const slicedYearlyVisits = Object.fromEntries(slicedData);

    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(yearlyVisits).length / limit),
        yearlyVisits: slicedYearlyVisits
    });
});

Use Curl Example

Without Pagination

  • Endpoint to get visitor information without pagination curl "http://localhost:3000/api/visitors"
  • Endpoint to get daily visit statistics without pagination curl "http://localhost:3000/api/stats/daily"
  • Endpoint to get weekly visit statistics without pagination curl "http://localhost:3000/api/stats/weekly"
  • Endpoint to get monthly visit statistics without pagination curl "http://localhost:3000/api/stats/monthly"
  • Endpoint to get annual visit statistics without pagination curl "http://localhost:3000/api/stats/yearly"

With Pagination

  • Endpoint to get visitor information with pagination curl "http://localhost:3000/api/visitors?page=1&limit=10"
  • Endpoint to get visitor information with pagination curl "http://localhost:3000/api/visitors?page=1&limit=10"
  • Endpoint to get daily visit statistics with pagination curl "http://localhost:3000/api/stats/daily?page=1&limit=10"
  • Endpoint to get weekly visit statistics with pagination curl "http://localhost:3000/api/stats/weekly?page=1&limit=10"
  • Endpoint to get monthly visit statistics with pagination curl "http://localhost:3000/api/stats/monthly?page=1&limit=10"
  • Endpoint to get annual visit statistics with pagination curl "http://localhost:3000/api/stats/yearly?page=1&limit=10"

With Filtering

  • Endpoint to get daily visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/daily?year=2024&month=4&day=25"
  • Endpoint to get weekly visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/weekly?year=2024&week=17"
  • Endpoint to get monthly visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/monthly?year=2024&month=4"
  • Endpoint to get annual visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/yearly?&year=2024"

With Pagination & Filtering

  • Endpoint to get daily visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/daily?page=1&limit=10&year=2024&month=4&day=25"
  • Endpoint to get weekly visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/weekly?page=1&limit=10&year=2024&week=17"
  • Endpoint to get monthly visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/monthly?page=1&limit=10&year=2024&month=4"
  • Endpoint to get annual visit statistics with pagination & filtering curl "http://localhost:3000/api/stats/yearly?page=1&limit=10&year=2024"

Full Code Example

const express = require('express');
const visitorCounter = require('hy-visitor-counter');

const app = express();
const port = 3000;

app.use((req, res, next) => {
    visitorCounter.incrementVisitor(req);
    next();
});

app.use((req, res, next) => {
    res.locals.totalVisitors = visitorCounter.getVisitorCount();
    next();
});

app.get('/api/visitors', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const visitors = visitorCounter.getVisitors().slice(startIndex, endIndex);
    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(res.locals.totalVisitors / limit),
        visitors: visitors
    });
});

app.get('/api/stats/daily', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const dailyVisits = visitorCounter.getDailyVisits();
    const slicedData = Object.entries(dailyVisits).slice(startIndex, endIndex);
    const slicedDailyVisits = Object.fromEntries(slicedData);
    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(dailyVisits).length / limit),
        dailyVisits: slicedDailyVisits
    });
});

app.get('/api/stats/weekly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const weeklyVisits = visitorCounter.getWeeklyVisits();
    const slicedData = Object.entries(weeklyVisits).slice(startIndex, endIndex);
    const slicedWeeklyVisits = Object.fromEntries(slicedData);
    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(weeklyVisits).length / limit),
        weeklyVisits: slicedWeeklyVisits
    });
});

app.get('/api/stats/monthly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const monthlyVisits = visitorCounter.getMonthlyVisits();
    const slicedData = Object.entries(monthlyVisits).slice(startIndex, endIndex);
    const slicedMonthlyVisits = Object.fromEntries(slicedData);
    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(monthlyVisits).length / limit),
        monthlyVisits: slicedMonthlyVisits
    });
});

app.get('/api/stats/yearly', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const yearlyVisits = visitorCounter.getYearlyVisits();
    const slicedData = Object.entries(yearlyVisits).slice(startIndex, endIndex);
    const slicedYearlyVisits = Object.fromEntries(slicedData);
    res.json({
        totalVisitors: res.locals.totalVisitors,
        page: page,
        limit: limit,
        totalPages: Math.ceil(Object.keys(yearlyVisits).length / limit),
        yearlyVisits: slicedYearlyVisits
    });
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});