From 6dd5ceabde1bb0871874700960e04d253d017499 Mon Sep 17 00:00:00 2001 From: Sainan Date: Thu, 16 May 2024 01:31:14 +0200 Subject: [PATCH] improve: use express.static to serve webui (#225) --- src/routes/webui.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/routes/webui.ts b/src/routes/webui.ts index 5a16b5e4..0010f2e0 100644 --- a/src/routes/webui.ts +++ b/src/routes/webui.ts @@ -3,22 +3,20 @@ import path from "path"; const webuiRouter = express.Router(); +// Redirect / to /webui/ webuiRouter.get("/", (_req, res) => { res.redirect("/webui/"); }); -const rootDir = path.join(__dirname, "../.."); - -webuiRouter.get("/webui/", (req, res) => { - if (req.path != "/webui/") { - res.redirect("/webui/"); - } else { - res.sendFile(path.join(rootDir, "static/webui/index.html")); +// Redirect /webui to /webui/ +webuiRouter.use("/webui", (req, res, next) => { + if (req.originalUrl === "/") { + return res.redirect("/webui/"); } + next(); }); -webuiRouter.get("/webui/script.js", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/script.js")); -}); +// Serve static files from the webui directory +webuiRouter.use("/webui", express.static(path.join(__dirname, "../..", "static/webui"))); export { webuiRouter };