api-health-middleware 1.0.2 → 1.0.4
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 +60 -93
- package/package.json +9 -11
- package/src/index.js +3 -1
- package/src/middleware.js +17 -12
package/README.md
CHANGED
|
@@ -1,135 +1,102 @@
|
|
|
1
|
-
|
|
1
|
+
````markdown
|
|
2
|
+
# api-health-middleware
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
[](LICENSE)
|
|
5
|
-
[](https://www.npmjs.com/package/api-health-middleware)
|
|
4
|
+
**Express middleware to report per-request API health to a monitoring backend.**
|
|
6
5
|
|
|
7
|
-
|
|
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.
|
|
6
|
+
This middleware logs API requests, response times, and status codes to a remote health endpoint. Designed to be lightweight and non-blocking, it works with any Express.js application.
|
|
11
7
|
|
|
12
8
|
---
|
|
13
9
|
|
|
14
|
-
##
|
|
10
|
+
## Installation
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
12
|
+
```bash
|
|
13
|
+
# From npm
|
|
14
|
+
npm install api-health-middleware
|
|
25
15
|
|
|
26
|
-
|
|
16
|
+
# Or locally via file
|
|
17
|
+
npm install /path/to/api-health-middleware
|
|
18
|
+
````
|
|
27
19
|
|
|
28
|
-
|
|
20
|
+
---
|
|
29
21
|
|
|
30
|
-
|
|
31
|
-
npm i api-health-middleware
|
|
22
|
+
## Usage
|
|
32
23
|
|
|
33
|
-
|
|
34
|
-
1. Import and Initialize Middleware
|
|
24
|
+
```js
|
|
35
25
|
const express = require('express');
|
|
36
|
-
const app = express();
|
|
37
|
-
|
|
38
|
-
// Import health monitor middleware
|
|
39
26
|
const healthMonitor = require('api-health-middleware');
|
|
40
27
|
|
|
41
|
-
|
|
28
|
+
const app = express();
|
|
29
|
+
|
|
30
|
+
// Initialize middleware with your API key
|
|
42
31
|
app.use(
|
|
43
32
|
healthMonitor({
|
|
44
|
-
apiKey:
|
|
33
|
+
apiKey: "YOUR_API_KEY"
|
|
45
34
|
})
|
|
46
35
|
);
|
|
47
36
|
|
|
48
|
-
app.get('/
|
|
49
|
-
res.
|
|
37
|
+
app.get('/hello', (req, res) => {
|
|
38
|
+
res.send('Hello World!');
|
|
50
39
|
});
|
|
51
40
|
|
|
52
|
-
app.listen(3000, () =>
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
41
|
+
app.listen(3000, () => {
|
|
42
|
+
console.log('Server running on port 3000');
|
|
43
|
+
});
|
|
44
|
+
```
|
|
96
45
|
|
|
97
|
-
|
|
46
|
+
---
|
|
98
47
|
|
|
99
|
-
|
|
48
|
+
## Configuration Options
|
|
100
49
|
|
|
101
|
-
|
|
50
|
+
| Option | Type | Description | Required |
|
|
51
|
+
| ------ | ------ | ---------------------------------- | -------- |
|
|
52
|
+
| apiKey | string | Your API key for the health server | Yes |
|
|
102
53
|
|
|
54
|
+
---
|
|
103
55
|
|
|
104
|
-
|
|
56
|
+
## How It Works
|
|
105
57
|
|
|
106
|
-
|
|
58
|
+
* Measures request duration using `process.hrtime.bigint()`.
|
|
59
|
+
* Captures:
|
|
107
60
|
|
|
108
|
-
|
|
61
|
+
* HTTP method
|
|
62
|
+
* Endpoint URL
|
|
63
|
+
* Status code
|
|
64
|
+
* Response time in milliseconds
|
|
65
|
+
* IP and User-Agent
|
|
66
|
+
* Automatically ignores logging requests sent to `/logs/health` to prevent recursion.
|
|
67
|
+
* Non-blocking and fails silently if the health server is unreachable.
|
|
109
68
|
|
|
110
|
-
|
|
69
|
+
---
|
|
111
70
|
|
|
112
|
-
|
|
71
|
+
## Notes
|
|
113
72
|
|
|
114
|
-
|
|
73
|
+
* Requires Node.js **18+** for native `fetch`.
|
|
74
|
+
For Node < 18, install `node-fetch` and import it:
|
|
115
75
|
|
|
116
|
-
|
|
76
|
+
```js
|
|
77
|
+
const fetch = require('node-fetch');
|
|
78
|
+
```
|
|
117
79
|
|
|
118
|
-
|
|
80
|
+
* Compatible with Express.js v4+.
|
|
119
81
|
|
|
120
|
-
|
|
82
|
+
---
|
|
121
83
|
|
|
84
|
+
## Development
|
|
122
85
|
|
|
123
|
-
|
|
86
|
+
Clone the repository and link locally:
|
|
124
87
|
|
|
125
|
-
|
|
88
|
+
```bash
|
|
89
|
+
cd api-health-middleware
|
|
90
|
+
npm link
|
|
126
91
|
|
|
127
|
-
|
|
92
|
+
# In your app project
|
|
93
|
+
npm link api-health-middleware
|
|
94
|
+
```
|
|
128
95
|
|
|
129
|
-
|
|
96
|
+
---
|
|
130
97
|
|
|
131
|
-
|
|
98
|
+
## License
|
|
132
99
|
|
|
100
|
+
MIT © Tarun Thakur
|
|
133
101
|
|
|
134
|
-
---
|
|
135
102
|
|
package/package.json
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-health-middleware",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Express middleware to report per-request API health to a monitoring backend",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"start"
|
|
7
|
+
"start": "node index.js"
|
|
8
8
|
},
|
|
9
|
-
"keywords": [
|
|
9
|
+
"keywords": [
|
|
10
|
+
"api",
|
|
11
|
+
"health",
|
|
12
|
+
"monitoring",
|
|
13
|
+
"express",
|
|
14
|
+
"middleware"
|
|
15
|
+
],
|
|
10
16
|
"author": "Tarun Thakur",
|
|
11
17
|
"license": "MIT",
|
|
12
|
-
"type": "module",
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"axios": "^1.13.4"
|
|
15
|
-
},
|
|
16
18
|
"peerDependencies": {
|
|
17
19
|
"express": ">=4"
|
|
18
20
|
}
|
|
19
|
-
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
package/src/index.js
CHANGED
package/src/middleware.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
const INGEST_ENDPOINT = "https://devops-lite-backend.onrender.com";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export function apiHealth({ apiKey }) {
|
|
3
|
+
function apiHealth({ apiKey }) {
|
|
6
4
|
if (!apiKey) throw new Error("apiKey is required");
|
|
7
5
|
|
|
8
6
|
return function apiHealthMiddleware(req, res, next) {
|
|
@@ -18,7 +16,7 @@ export function apiHealth({ apiKey }) {
|
|
|
18
16
|
|
|
19
17
|
const payload = {
|
|
20
18
|
timestamp: new Date().toISOString(),
|
|
21
|
-
|
|
19
|
+
method: req.method,
|
|
22
20
|
endpoint: req.route?.path || req.originalUrl,
|
|
23
21
|
fullUrl: req.originalUrl,
|
|
24
22
|
statusCode: res.statusCode,
|
|
@@ -32,18 +30,23 @@ export function apiHealth({ apiKey }) {
|
|
|
32
30
|
userAgent: req.headers["user-agent"] || null,
|
|
33
31
|
};
|
|
34
32
|
|
|
35
|
-
//
|
|
36
|
-
|
|
33
|
+
// Timeout handling using AbortController
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const timeout = setTimeout(() => controller.abort(), 2000);
|
|
36
|
+
|
|
37
|
+
fetch(INGEST_ENDPOINT, {
|
|
38
|
+
method: "POST",
|
|
37
39
|
headers: {
|
|
38
40
|
"x-api-key": apiKey,
|
|
39
41
|
"Content-Type": "application/json",
|
|
40
42
|
},
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
body: JSON.stringify(payload),
|
|
44
|
+
signal: controller.signal,
|
|
45
|
+
})
|
|
46
|
+
.catch(() => {}) // Silent fail
|
|
47
|
+
.finally(() => clearTimeout(timeout));
|
|
45
48
|
|
|
46
|
-
} catch
|
|
49
|
+
} catch {
|
|
47
50
|
// Never break main request
|
|
48
51
|
}
|
|
49
52
|
});
|
|
@@ -51,3 +54,5 @@ export function apiHealth({ apiKey }) {
|
|
|
51
54
|
next();
|
|
52
55
|
};
|
|
53
56
|
}
|
|
57
|
+
|
|
58
|
+
module.exports = apiHealth;
|