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();
// 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 };