import path from "node:path"; import { fileURLToPath } from "node:url"; import cors from "cors"; import express from "express"; import { seedIfEmpty } from "./seed.js"; import { bootstrapRouter } from "./routes/bootstrap.js"; import { productsRouter } from "./routes/products.js"; import { catalogRouter } from "./routes/catalog.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const PUBLIC_DIR = path.resolve(__dirname, "..", "public"); seedIfEmpty(); const app = express(); app.use(cors()); app.use(express.json({ limit: "1mb" })); app.use("/api", bootstrapRouter); app.use("/api", productsRouter); app.use("/api", catalogRouter); app.use(express.static(PUBLIC_DIR)); app.get("*", (req, res, next) => { if (req.path.startsWith("/api")) return next(); res.sendFile(path.join(PUBLIC_DIR, "index.html")); }); const PORT = Number(process.env.PORT ?? 4000); app.listen(PORT, () => { console.log(`[apothecary] api listening on http://localhost:${PORT}`); });