express-sequelize-traffic 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/README.md +146 -4
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -30,9 +30,9 @@ npm install
30
30
  npm run build
31
31
  ```
32
32
 
33
- ## Sequelize Setup
33
+ ## Quick Setup
34
34
 
35
- The package defines a `TrafficLog` model for you. You can either manage the table through your own migrations or call `traffic.sync()` for quick setup.
35
+ The package defines a `TrafficLog` model for you. You can either manage the table through your own migrations or call `traffic.sync()` for quick setup. `traffic.sync()` wraps Sequelize model sync and creates the `traffic_logs` table if it does not already exist.
36
36
 
37
37
  ```js
38
38
  import { Sequelize } from "sequelize";
@@ -46,7 +46,9 @@ await sequelize.authenticate();
46
46
  await traffic.sync();
47
47
  ```
48
48
 
49
- ## Express Usage
49
+ ## ES Module Setup
50
+
51
+ Use this form when the target app uses `"type": "module"` or native ESM imports.
50
52
 
51
53
  ```js
52
54
  import express from "express";
@@ -83,15 +85,155 @@ app.use("/traffic-dashboard", traffic.dashboard);
83
85
  server.listen(3000);
84
86
  ```
85
87
 
86
- CommonJS applications can use the published `require(...)` entry:
88
+ ## CommonJS Setup
89
+
90
+ The package also publishes a CommonJS entry, so `require(...)` works directly.
87
91
 
88
92
  ```js
89
93
  const express = require("express");
90
94
  const http = require("node:http");
91
95
  const { Sequelize } = require("sequelize");
92
96
  const { createTrafficTracker } = require("express-sequelize-traffic");
97
+
98
+ const app = express();
99
+ const server = http.createServer(app);
100
+
101
+ const sequelize = new Sequelize(process.env.DB_URL, {
102
+ logging: false,
103
+ });
104
+
105
+ async function start() {
106
+ const traffic = createTrafficTracker({
107
+ sequelize,
108
+ getUserId: (req) => req.user?.id || req.headers["x-user-id"] || null,
109
+ getSessionId: (req) => req.sessionID || req.headers["x-session-id"] || null,
110
+ slowRouteThresholdMs: 1000,
111
+ ignoredRoutes: ["/health", "/favicon.ico"],
112
+ trackIp: false,
113
+ trackUserAgent: true,
114
+ dashboard: {
115
+ enabled: true,
116
+ mountPath: "/traffic-dashboard",
117
+ username: process.env.TRAFFIC_ADMIN_USER,
118
+ password: process.env.TRAFFIC_ADMIN_PASS,
119
+ },
120
+ });
121
+
122
+ await sequelize.authenticate();
123
+ await traffic.sync();
124
+
125
+ app.use(traffic.middleware);
126
+ traffic.attachRealtime(server);
127
+ app.use("/traffic-dashboard", traffic.dashboard);
128
+
129
+ app.get("/health", (_req, res) => {
130
+ res.json({ ok: true });
131
+ });
132
+
133
+ server.listen(3000);
134
+ }
135
+
136
+ start().catch((error) => {
137
+ console.error("Startup failed:", error);
138
+ process.exit(1);
139
+ });
140
+ ```
141
+
142
+ ## Passing User Details
143
+
144
+ The package does not assume how authentication works in your app. You pass user and session details through resolver functions.
145
+
146
+ ```js
147
+ const traffic = createTrafficTracker({
148
+ sequelize,
149
+ getUserId: (req) =>
150
+ req.user?.id || req.auth?.sub || req.headers["x-user-id"] || null,
151
+ getSessionId: (req) =>
152
+ req.sessionID || req.cookies?.sessionId || req.headers["x-session-id"] || null,
153
+ });
154
+ ```
155
+
156
+ Common sources are:
157
+
158
+ - `req.user.id` from Passport or your own auth middleware
159
+ - `req.auth.sub` from JWT middleware
160
+ - `req.sessionID` from `express-session`
161
+ - custom request headers in internal systems
162
+
163
+ Rules to follow:
164
+
165
+ - Register auth and session middleware before `traffic.middleware`
166
+ - Return `null` when user or session data is unavailable
167
+ - If you omit `getUserId` or `getSessionId`, those fields are stored as `NULL`
168
+
169
+ ## Why It Tracks More Than Application Routes
170
+
171
+ This package is request middleware. If you mount it globally with:
172
+
173
+ ```js
174
+ app.use(traffic.middleware);
175
+ ```
176
+
177
+ it will see every request that reaches that point in the Express stack, not just your business endpoints.
178
+
179
+ That includes:
180
+
181
+ - API routes
182
+ - page routes
183
+ - health checks
184
+ - static files
185
+ - 404 requests
186
+ - dashboard API requests
187
+ - dashboard frontend asset requests
188
+
189
+ Internally, the tracker records the route from `req.route?.path`, then falls back to `req.path`, then `req.originalUrl`. That is why it can still log requests even when Express does not resolve them to a named application route.
190
+
191
+ ## How To Track Only Application Routes
192
+
193
+ If you want to limit tracking to business endpoints, use one or more of these patterns.
194
+
195
+ Mount the tracker only on the route prefix you care about:
196
+
197
+ ```js
198
+ app.use("/api", traffic.middleware);
199
+ ```
200
+
201
+ Register static assets or health checks before the tracker:
202
+
203
+ ```js
204
+ app.use("/public", express.static("public"));
205
+ app.get("/health", (_req, res) => res.json({ ok: true }));
206
+
207
+ app.use(traffic.middleware);
93
208
  ```
94
209
 
210
+ Ignore route groups with regex:
211
+
212
+ ```js
213
+ const traffic = createTrafficTracker({
214
+ sequelize,
215
+ ignoredRoutes: [
216
+ "/health",
217
+ "/favicon.ico",
218
+ /^\/traffic-dashboard(\/|$)/,
219
+ /^\/public(\/|$)/,
220
+ ],
221
+ });
222
+ ```
223
+
224
+ Use a function when the skip logic depends on route naming conventions:
225
+
226
+ ```js
227
+ const traffic = createTrafficTracker({
228
+ sequelize,
229
+ ignoredRoutes: [
230
+ (route) => route.startsWith("/internal/"),
231
+ ],
232
+ });
233
+ ```
234
+
235
+ If you mount the tracker globally, global tracking is expected behavior.
236
+
95
237
  ## Dashboard Usage
96
238
 
97
239
  The dashboard is optional. Set `dashboard.enabled` to `true`, build the frontend assets, attach realtime support, and mount the provided router.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-sequelize-traffic",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Privacy-first, self-hosted Express request analytics with Sequelize storage and an optional realtime dashboard.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",