Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

Maples7/api-usage-rate

Repository files navigation

Build StatusCoverage Statusnpm version
NPMNPM

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 asELK, 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 ornpm install api-usage-rate --save

Requirement

  • a running redis service

Example

Talk is cheap, show you the code:

constpath=require('path');constexpress=require('express');constroutes=require('express-mount-routes');constapiRate=new(require('api-usage-rate'))();constapp=express();// moute a middleware who records data of api usage rate in redisapp.use(apiRate.record());// see https://github.com/Maples7/express-mount-routesroutes(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 APIsapiRate.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. Keyapi-usage-rate refers to a zset who holds number of hits in field${req.method}:${req.path} and keyapi-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 optionalconstapiRate=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 reviewtest.js andtest_page.js in this project for more information of usage.

LICENSE

MIT

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp