From 4bcc579a79694cfd010bdd248d5eaa9af03f59fe Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Mon, 22 Jul 2024 19:05:21 +0200 Subject: [PATCH] Add environment checks to server.ts --- src/server.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index f902c80..cd79133 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,6 +3,8 @@ import mongoose, { CallbackError } from "mongoose"; import v1Router from "./routes/v1"; import { sendError } from "@utils/error"; import cors from "cors"; +import path from "node:path"; +import env from "./templates/env"; const MONGOOSE_STRING = process.env.MONGOOSE_STRING || "mongodb://localhost:27017"; @@ -20,7 +22,15 @@ app.use("/api/", v1Router); // <- Newest app.use("/api/v1/", v1Router); -app.use("/api/*", (req: Request, res: Response) => sendError(res, 404, 0, "The provided route could not be found")); +app.use("/api/*", (_req: Request, res: Response) => sendError(res, 404, 0, "The provided route could not be found")); + +if (process.env.NODE_ENV === "production") { + app.use(express.static(path.join(__dirname, "../build"))); + + app.get("*", (_req, res) => res.sendFile(path.join(__dirname, "../build", "index.html"))); +} else { + app.get("*", (_req, res) => res.status(500).send(env)); +} // Start the backend const run = () =>