api-health-middleware
v1.0.5
Published
Express middleware to report per-request API health to a monitoring backend
Downloads
23
Maintainers
Readme
# api-health-middleware
**Express middleware to report per-request API health to a monitoring backend.**
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.
---
## Installation
```bash
# From npm
npm install api-health-middleware
# Or locally via file
npm install /path/to/api-health-middlewareUsage
const express = require('express');
const healthMonitor = require('api-health-middleware');
const app = express();
// Initialize middleware with your API key
app.use(
healthMonitor({
apiKey: "YOUR_API_KEY"
})
);
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});Configuration Options
| Option | Type | Description | Required | | ------ | ------ | ---------------------------------- | -------- | | apiKey | string | Your API key for the health server | Yes |
How It Works
Measures request duration using
process.hrtime.bigint().Captures:
- HTTP method
- Endpoint URL
- Status code
- Response time in milliseconds
- IP and User-Agent
Automatically ignores logging requests sent to
/logs/healthto prevent recursion.Non-blocking and fails silently if the health server is unreachable.
Notes
- Requires Node.js 18+ for native
fetch. For Node < 18, installnode-fetchand import it:
const fetch = require('node-fetch');- Compatible with Express.js v4+.
Development
Clone the repository and link locally:
cd api-health-middleware
npm link
# In your app project
npm link api-health-middlewareLicense
MIT © Tarun Thakur
