@hanykholey/axiscloud-sdk
v1.0.0
Published
Express middleware for per-IP rate limiting and request logs batching with GeoIP lookup.
Maintainers
Readme
AxisCloud SDK (Rate Limiting + Request Logs)
A small library that provides rate limiting + request logs batching as an Express middleware.
The package name in package.json is: axiscloud-sdk.
Overview
- Rate limiting is applied per IP using
node-cache. - Captures request data (User-Agent / IP / Path / Country) and buffers logs.
- When buffered logs reach 30 logs, they are sent in one batch to a backend endpoint.
- Country is resolved via
GeoLite2-Country.mmdbusingmaxmind, with per-IP country caching.
Requirements
- Node.js (recommended 18+)
- Express (this library exposes a middleware)
GeoLite2-Country.mmdbmust exist in the project root (or the process working directory at runtime)
Installation
If you are using the project locally from source:
npm install
npm run buildIf you are installing it as a package (depending on your publishing setup):
npm i axiscloud-sdkNote: the library depends on Express + got + maxmind + node-cache.
Quick Start (Express Middleware)
import express from "express";
import { Logs } from "axiscloud-sdk";
const app = express();
app.use(express.json());
// Logs(apiKey, countRequest, timeCache)
// apiKey: your project key
// countRequest: max allowed requests per IP within the window
// timeCache: window size in seconds
app.use(Logs("YOUR_API_KEY", 100, 60));
app.get("/Test", (req, res) => {
res.status(200).json({ message: "api is work" });
});
app.listen(3001, () => console.log("Server is Start..."));Configuration
Main exported function:
Logs(apiKey: string, countRequest: number, timeCache: number)
Parameters
apiKey- Sent along with logs to the backend.
countRequest- Maximum number of requests per IP within the
timeCachewindow. - When exceeded, the middleware responds with 429:
{"message":"To Many Requests"}
- Maximum number of requests per IP within the
timeCache- Window size (in seconds) used to count requests.
Sending Logs to the Backend
Logs are buffered and sent when the buffer size becomes >= 30 to a hardcoded endpoint:
POST http://localhost:3000/CreateLog
Request body:
{
"sentLogs": [
{
"userAgent": "...",
"ip": "...",
"path": "/...",
"country": "EG"
}
],
"apiKey": "YOUR_API_KEY"
}Important Notes
- GeoIP database path is currently used like this:
maxmind.open("./GeoLite2-Country.mmdb")- This means
GeoLite2-Country.mmdbmust be available in the process working directory when your server runs.
backendUrlandfrontendUrlare currently hardcoded (localhost) in the source. If you want them to be configurable via options/env vars, tell me and I’ll refactor it.
Build
npm run buildBuild output will be generated in dist/.
