api-health-middleware 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -0
- package/package.json +1 -1
- package/src/middleware.js +35 -22
package/README.md
CHANGED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# health-monitor-middleware
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/api-health-middleware)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/api-health-middleware)
|
|
6
|
+
|
|
7
|
+
A lightweight **Express middleware** to automatically monitor API health.
|
|
8
|
+
It tracks requests, responses, errors, response time, and uptime, sending logs to your dashboard (Under Development) using an API key.
|
|
9
|
+
|
|
10
|
+
This package is plug-and-play — just install it, provide your API key, and it works out-of-the-box.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Automatic logging of all API requests
|
|
17
|
+
- Tracks:
|
|
18
|
+
- Endpoint (`/api/users`, `/login`, etc.)
|
|
19
|
+
- HTTP method (GET, POST, PUT, DELETE)
|
|
20
|
+
- Status code (200, 404, 500, etc.)
|
|
21
|
+
- Response time in milliseconds
|
|
22
|
+
- Success / error messages
|
|
23
|
+
- IP address and User-Agent
|
|
24
|
+
- Works with Express 4.x and above
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm i api-health-middleware
|
|
32
|
+
|
|
33
|
+
Usage
|
|
34
|
+
1. Import and Initialize Middleware
|
|
35
|
+
const express = require('express');
|
|
36
|
+
const app = express();
|
|
37
|
+
|
|
38
|
+
// Import health monitor middleware
|
|
39
|
+
const healthMonitor = require('api-health-middleware');
|
|
40
|
+
|
|
41
|
+
// Use middleware globally
|
|
42
|
+
app.use(
|
|
43
|
+
healthMonitor({
|
|
44
|
+
apiKey: 'YOUR_DASHBOARD_API_KEY', // Required
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
app.get('/api/users', (req, res) => {
|
|
49
|
+
res.json({ message: 'Hello users' });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
app.listen(3000, () => console.log('Server running on port 3000'));
|
|
53
|
+
|
|
54
|
+
2. How It Works
|
|
55
|
+
|
|
56
|
+
Middleware intercepts every request and response.
|
|
57
|
+
|
|
58
|
+
Captures metrics: endpoint, method, status, response time, IP, User-Agent.
|
|
59
|
+
|
|
60
|
+
Sends logs asynchronously to your monitoring dashboard using the API key.
|
|
61
|
+
|
|
62
|
+
Your dashboard aggregates metrics per project, showing:
|
|
63
|
+
|
|
64
|
+
Total API calls
|
|
65
|
+
|
|
66
|
+
Error count
|
|
67
|
+
|
|
68
|
+
Average response time
|
|
69
|
+
|
|
70
|
+
Uptime percentage
|
|
71
|
+
|
|
72
|
+
3. Middleware Configuration Options
|
|
73
|
+
Option Type Required Default Description
|
|
74
|
+
apiKey string ✅ Yes — API key generated from your dashboard for authentication
|
|
75
|
+
Example Log Object Sent to Dashboard
|
|
76
|
+
{
|
|
77
|
+
"timestamp": "2026-02-11T16:24:00.123Z",
|
|
78
|
+
"method": "GET",
|
|
79
|
+
"endpoint": "/users",
|
|
80
|
+
"statusCode": 200,
|
|
81
|
+
"responseTimeMs": 123,
|
|
82
|
+
"success": true,
|
|
83
|
+
"errorMessage": null,
|
|
84
|
+
"ip": "192.168.0.1",
|
|
85
|
+
"userAgent": "PostmanRuntime/7.28.4",
|
|
86
|
+
"projectId": "623d2f6b9e1b0a0012345678"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Example Metrics from Dashboard
|
|
90
|
+
|
|
91
|
+
Total API calls: 120
|
|
92
|
+
|
|
93
|
+
Error count: 5
|
|
94
|
+
|
|
95
|
+
Average response time: 135 ms
|
|
96
|
+
|
|
97
|
+
Uptime: 95.83%
|
|
98
|
+
|
|
99
|
+
Best Practices
|
|
100
|
+
|
|
101
|
+
Async logging – middleware sends logs asynchronously to avoid blocking API responses.
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
Use environments – differentiate production, staging, or development metrics.
|
|
105
|
+
|
|
106
|
+
Batch logs – optionally batch logs to reduce network calls.
|
|
107
|
+
|
|
108
|
+
Secure API key – never commit it publicly.
|
|
109
|
+
|
|
110
|
+
Contributing
|
|
111
|
+
|
|
112
|
+
We welcome contributions!
|
|
113
|
+
|
|
114
|
+
Fork the repo
|
|
115
|
+
|
|
116
|
+
Create a new branch (feature/my-feature)
|
|
117
|
+
|
|
118
|
+
Run tests:
|
|
119
|
+
|
|
120
|
+
npm test
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
Submit a pull request
|
|
124
|
+
|
|
125
|
+
License
|
|
126
|
+
|
|
127
|
+
MIT © Your Name
|
|
128
|
+
|
|
129
|
+
Acknowledgements
|
|
130
|
+
|
|
131
|
+
Inspired by Postman Monitors and lightweight API monitoring systems.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
package/package.json
CHANGED
package/src/middleware.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
|
|
3
|
-
const INGEST_ENDPOINT = "
|
|
4
|
-
|
|
3
|
+
const INGEST_ENDPOINT = "http://localhost:5000/logs/health";
|
|
5
4
|
|
|
6
5
|
export function apiHealth({ apiKey }) {
|
|
7
6
|
if (!apiKey) throw new Error("apiKey is required");
|
|
@@ -10,29 +9,43 @@ export function apiHealth({ apiKey }) {
|
|
|
10
9
|
const start = process.hrtime.bigint();
|
|
11
10
|
|
|
12
11
|
res.on("finish", () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
try {
|
|
13
|
+
// Prevent self-logging
|
|
14
|
+
if (req.originalUrl.includes("/logs/health")) return;
|
|
15
|
+
|
|
16
|
+
const durationMs =
|
|
17
|
+
Number(process.hrtime.bigint() - start) / 1_000_000;
|
|
18
|
+
|
|
19
|
+
const payload = {
|
|
20
|
+
timestamp: new Date().toISOString(),
|
|
21
|
+
Method: req.method,
|
|
22
|
+
endpoint: req.route?.path || req.originalUrl,
|
|
23
|
+
fullUrl: req.originalUrl,
|
|
24
|
+
statusCode: res.statusCode,
|
|
25
|
+
responseTimeMs: Math.round(durationMs),
|
|
26
|
+
success: res.statusCode < 400,
|
|
27
|
+
errorMessage:
|
|
28
|
+
res.statusCode >= 400
|
|
29
|
+
? res.statusMessage
|
|
30
|
+
: null,
|
|
31
|
+
ip: req.ip,
|
|
32
|
+
userAgent: req.headers["user-agent"] || null,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Fire and forget (do not await)
|
|
36
|
+
axios.post(INGEST_ENDPOINT, payload, {
|
|
27
37
|
headers: {
|
|
28
|
-
"x-api-key":
|
|
29
|
-
"Content-Type": "application/json"
|
|
38
|
+
"x-api-key": apiKey,
|
|
39
|
+
"Content-Type": "application/json",
|
|
30
40
|
},
|
|
31
|
-
timeout: 2000
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
console.log(err);
|
|
41
|
+
timeout: 2000,
|
|
42
|
+
}).catch(() => {
|
|
43
|
+
// silently fail (monitoring should never break main API)
|
|
35
44
|
});
|
|
45
|
+
|
|
46
|
+
} catch (err) {
|
|
47
|
+
// Never break main request
|
|
48
|
+
}
|
|
36
49
|
});
|
|
37
50
|
|
|
38
51
|
next();
|