express-slack-stats
v1.1.1
Published
Express middleware to send API stats and exit codes to Slack
Downloads
26
Maintainers
Readme
express-slack-stats
📊 Express middleware to send API request stats and process exit alerts to Slack. Supports endpoint stats, status codes, latency averages, and exit code notifications — all delivered as clean Slack Block Kit messages.
✨ Features
Tracks:
- Hits per endpoint (with param normalization, e.g.
/users/123→/users/_) - Methods (
GET,POST, etc.) - Status codes (
200,404,500, …) - Average response time
- Hits per endpoint (with param normalization, e.g.
Aggregates stats into periodic Slack reports (configurable with cron syntax)
Reports Node.js process exit codes and uncaught errors
Lightweight & simple to drop into any Express app
Written in TypeScript with full type definitions
📦 Installation
npm install express-slack-statsor
yarn add express-slack-stats🚀 Usage
import express from "express";
import expressSlackStats from "express-slack-stats";
const app = express();
expressSlackStats(app, {
webhookUrl: process.env.SLACK_WEBHOOK as string, // required
schedule: "*/10 * * * *", // optional, default: every 5 min
includeExitCodes: true, // optional, default: true
sendEmptyReports: false, // optional, default: false
includeCharts: true, // optional, default: false
});
app.get("/users/:id/profile", (req, res) => {
res.json({ ok: true });
});
app.listen(3000, () => console.log("Server running"));⚙️ Options
| Option | Type | Default | Description |
| ------------------ | --------- | --------------- | --------------------------------------------------------------------------- |
| webhookUrl | string | required | Slack Incoming Webhook URL |
| schedule | string | "*/5 * * * *" | Cron syntax for how often to send stats (e.g. "*/10 * * * *" = every 10m) |
| includeExitCodes | boolean | true | Whether to report process exit codes and crashes to Slack |
| sendEmptyReports | boolean | false | Whether to send reports when no API activity was recorded |
| includeCharts | boolean | false | Whether to include ASCII charts in the Slack reports |
📊 Example Slack Report
Basic Table Format:
📊 API Stats Report
Endpoint | Hits | Status | Avg Time
----------------------------|------|--------------|----------
GET /api/users/:id | 25 | 200:20,404:5 | 145.2ms
POST /api/users | 12 | 201:10,400:2 | 89.7ms
GET /api/products/:id | 8 | 200:8 | 67.3ms
POST /api/login | 12 | 200:11,401:1 | 89.5msWith Charts (when includeCharts: true):
📊 API Stats Report
Endpoint | Hits | Status | Avg Time
----------------------------|------|--------------|----------
GET /api/users/:id | 45 | 200:44,500:1 | 120.3ms
POST /api/login | 12 | 200:11,401:1 | 89.5ms
API Endpoint Hit Distribution:
GET /api/users/:id │████████████████████ 45
POST /api/login │█████████ 12
GET /api/products │███ 5
Status Code Distribution:
200 OK │■■■■■■■■■■■■■■■ 50 (80.6%)
401 Unauthorized │■■ 8 (12.9%)
500 Server Error │■ 4 (6.5%)(Delivered as a Slack Block message with header, table, optional charts, and timestamp.)
