Some checks failed
CI / build-and-test (pull_request) Has been cancelled
Frontend 0.3.1, backend 1.2.2; drop debug middleware from app.ts. Made-with: Cursor
29 lines
916 B
TypeScript
29 lines
916 B
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();
|
|
|
|
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;
|
|
}
|