mirror of
https://github.com/Shineii86/AniKotoAPI.git
synced 2026-10-22 10:55:10 +00:00
fix: health endpoint typo and request counter middleware order
This commit is contained in:
+19
-16
@@ -57,6 +57,24 @@ import { categoryRoutes } from "./category.route.js";
|
|||||||
* createApiRoutes(app, jsonResponse, jsonError);
|
* createApiRoutes(app, jsonResponse, jsonError);
|
||||||
*/
|
*/
|
||||||
const createApiRoutes = (app, jsonResponse, jsonError) => {
|
const createApiRoutes = (app, jsonResponse, jsonError) => {
|
||||||
|
// ══════════════════════════════════════════════════════════════
|
||||||
|
// REQUEST COUNTER (must be before all routes)
|
||||||
|
// ══════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
const startTime = Date.now();
|
||||||
|
let requestCount = 0;
|
||||||
|
let errorCount = 0;
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
if (req.path.startsWith("/api")) {
|
||||||
|
requestCount++;
|
||||||
|
res.on("finish", () => {
|
||||||
|
if (res.statusCode >= 400) errorCount++;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
// ══════════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════════
|
||||||
// HOME
|
// HOME
|
||||||
// ══════════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════════
|
||||||
@@ -332,21 +350,6 @@ categoryRoutes(app, jsonResponse, jsonError);
|
|||||||
// HEALTH & STATS
|
// HEALTH & STATS
|
||||||
// ══════════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
const startTime = Date.now();
|
|
||||||
let requestCount = 0;
|
|
||||||
let errorCount = 0;
|
|
||||||
|
|
||||||
// Middleware to count requests
|
|
||||||
app.use((req, res, next) => {
|
|
||||||
if (req.path.startsWith("/api")) {
|
|
||||||
requestCount++;
|
|
||||||
res.on("finish", () => {
|
|
||||||
if (res.statusCode >= 400) errorCount++;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
// ---- FEATURE: Health check endpoint ----
|
// ---- FEATURE: Health check endpoint ----
|
||||||
app.get("/api/health", (req, res) => {
|
app.get("/api/health", (req, res) => {
|
||||||
const uptime = Math.floor((Date.now() - startTime) / 1000);
|
const uptime = Math.floor((Date.now() - startTime) / 1000);
|
||||||
@@ -359,7 +362,7 @@ app.get("/api/health", (req, res) => {
|
|||||||
results: {
|
results: {
|
||||||
status: "healthy",
|
status: "healthy",
|
||||||
version: "1.7.2",
|
version: "1.7.2",
|
||||||
uptime: `${hours}h ${minutes}m ${s}s`,
|
uptime: `${hours}h ${minutes}m ${seconds}s`,
|
||||||
uptimeSeconds: uptime,
|
uptimeSeconds: uptime,
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
node: process.version,
|
node: process.version,
|
||||||
|
|||||||
Reference in New Issue
Block a user