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

api-usage-rate

v1.0.0

Published

An Express-based middleware to trace usage rate of APIs and show them in real time with great visualization tools.

Downloads

7

Readme

api-usage-rate

Build Status Coverage Status npm version
NPM NPM

An Express-based middleware to trace usage rate of APIs and show them in real time with great visualization tools.

Why?

I definitely know that you can get the api usage rate of your application through logs, even with some awesome tools such as ELK, but they are too heavy and very high resource-consuming.

So I make this tiny tool. No more, no less, just clearly show you the usage rate of APIs.

With these data, you can

  • make your application better by listening to what your users really like
  • remove old code of your app promptly and keep your code is always brandly new

Usage

Installation

yarn add api-usage-rate or npm install api-usage-rate --save

Requirement

  • a running redis service

Example

Talk is cheap, show you the code:

const path = require('path');
const express = require('express');
const routes = require('express-mount-routes'); 
const apiRate = new (require('api-usage-rate'))();

const app = express();
// moute a middleware who records data of api usage rate in redis
app.use(apiRate.record());
// see https://github.com/Maples7/express-mount-routes
routes(app, path.join(__dirname, 'controllers'));
// moute routes for Express:
// - /api-usage-rate: a static page to visualize api usage rate
// - /api-data: JSON data about hits number of APIs
apiRate.mouteRoutes(app);

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

Notes:

  • app.use(apiRate.record());: this middleware would record every hits of APIs in your redis. Key api-usage-rate refers to a zset who holds number of hits in field ${req.method}:${req.path} and key api-hits-counter holds sum of hits of all APIs. You can check your redis.
  • apiRate.mouteRoutes(app);: like the comment in example code says, routes /api-usage-rate and /api-data can be used with your browser once your app is launched.

API

  1. constructor
// all params are optional
const apiRate = new (require('api-usage-rate'))({
  connectRedis, // Info required to connect to redis, Object or String, default to `undefined`, see https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options for more instruction
  ignorePathes, // Array of RegExp, any req.path tested true with RegExp in the array would be ignore, default to `[/^\/api-data*/, /^\/api-usage-rate*/, /js$/, /css$/]`
  flushdb, // whether delete data stored in key `api-usage-rate` and `api-hits-counter` while a new instance of class ApiUsageRate is generated, default to false. And don't worry, this switch would not affect other data in redis (since v0.1.0)
});
  1. apiRate.record() takes no arguments.
  2. apiRate.mouteRoutes(app): app = express().

You are welcomed to review test.js and test_page.js in this project for more information of usage.

LICENSE

MIT