improve: use express.static to serve webui (#225)

This commit is contained in:
Sainan 2024-05-16 01:31:14 +02:00 committed by GitHub
parent 376b75be39
commit 6dd5ceabde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,22 +3,20 @@ import path from "path";
const webuiRouter = express.Router(); const webuiRouter = express.Router();
// Redirect / to /webui/
webuiRouter.get("/", (_req, res) => { webuiRouter.get("/", (_req, res) => {
res.redirect("/webui/"); res.redirect("/webui/");
}); });
const rootDir = path.join(__dirname, "../.."); // Redirect /webui to /webui/
webuiRouter.use("/webui", (req, res, next) => {
webuiRouter.get("/webui/", (req, res) => { if (req.originalUrl === "/") {
if (req.path != "/webui/") { return res.redirect("/webui/");
res.redirect("/webui/");
} else {
res.sendFile(path.join(rootDir, "static/webui/index.html"));
} }
next();
}); });
webuiRouter.get("/webui/script.js", (_req, res) => { // Serve static files from the webui directory
res.sendFile(path.join(rootDir, "static/webui/script.js")); webuiRouter.use("/webui", express.static(path.join(__dirname, "../..", "static/webui")));
});
export { webuiRouter }; export { webuiRouter };