Files
runners-calendar/backend/src/app.ts
Vaka.pro c04dc35075
Some checks failed
CI / build-and-test (pull_request) Has been cancelled
chore(backend): log Host/Origin and status for request debugging
2026-04-10 22:26:53 +03:00

48 lines
1.4 KiB
TypeScript

import express, { Request, Response, NextFunction } from "express";
import cors from "cors";
import { config } from "./config";
import healthRouter from "./routes/health";
import racesRouter from "./routes/races";
export function createApp(): express.Express {
const app = express();
// TEMP: compare Host vs Origin for 200 vs 401 debugging — remove when done
app.use((req, res, next) => {
const host = req.headers.host;
const origin = req.headers.origin;
res.on("finish", () => {
console.log(
JSON.stringify({
tag: "temp-req-headers",
host,
origin: origin ?? null,
method: req.method,
path: req.originalUrl ?? req.url,
status: res.statusCode,
}),
);
});
next();
});
app.use(
cors({ origin: config.corsOrigin, methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"] }),
);
app.use(express.json());
app.use("/api", healthRouter);
app.use("/api", racesRouter);
app.use((err: unknown, _req: Request, res: Response, _next: NextFunction) => {
if (err instanceof SyntaxError && "body" in err) {
res.status(400).json({ error: "validation_error", details: ["Invalid JSON in request body"] });
return;
}
console.error("[app] Unhandled error:", err);
res.status(500).json({ error: "unknown_error", details: ["Internal server error"] });
});
return app;
}