api-usage-rate
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
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();
app.use(apiRate.record());
routes(app, path.join(__dirname, 'controllers'));
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
- constructor
const apiRate = new (require('api-usage-rate'))({
connectRedis,
ignorePathes,
flushdb,
});
apiRate.record()
takes no arguments.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