This commit is contained in:
2025-03-24 14:35:08 +01:00
parent 5c8a4c1edc
commit 277f5a313e
10 changed files with 81 additions and 234 deletions

10
dist/router.js vendored
View File

@@ -9,19 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
import fs from "node:fs";
import path from "node:path";
import Tpl from "./template.js";
export default class Router {
constructor() {
this.routes = [];
this.mimes = new Map();
}
importRoutesFromPath(p_1) {
return __awaiter(this, arguments, void 0, function* (p, tpl = false) {
importRoutesFromPath(p) {
return __awaiter(this, void 0, void 0, function* () {
const dirEntries = yield fs.promises.readdir(path.resolve() + '/' + p, { withFileTypes: true });
for (const tmp of dirEntries) {
if (tmp.isFile() && (tmp.name.endsWith(".mjs") || tmp.name.endsWith(".js"))) {
const routeModule = (yield import(`${path.resolve()}/${p}/${tmp.name}`)).default;
this.use(routeModule(this, tpl));
this.use(routeModule(this));
}
else if (tmp.isDirectory()) {
yield this.importRoutesFromPath(p + '/' + tmp.name);
@@ -75,9 +74,6 @@ export default class Router {
this.routes = [...this.routes, ...obj.routes];
this.sortRoutes();
}
if (obj instanceof Tpl) {
this.tpl = obj;
}
}
get(path, ...callback) {
this.registerRoute(path, "get", callback);