oof
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
class Router {
|
||||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default new class Router {
|
||||
#mimes;
|
||||
constructor() {
|
||||
this.routes = new Map();
|
||||
};
|
||||
@@ -12,11 +16,32 @@ class Router {
|
||||
post() {
|
||||
this.route("POST", arguments);
|
||||
};
|
||||
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
|
||||
if(!this.#mimes) {
|
||||
this.#mimes = new Map();
|
||||
(await fs.readFile("/etc/mime.types", "utf-8"))
|
||||
.split("\n")
|
||||
.filter(e => !e.startsWith("#") && e)
|
||||
.map(e => e.split(/\s{2,}/))
|
||||
.filter(e => e.length > 1)
|
||||
.forEach(m => m[1].split(" ").forEach(ext => this.#mimes.set(ext, m[0])));
|
||||
}
|
||||
this.get(route, async (req, res) => {
|
||||
try {
|
||||
return res.reply({
|
||||
type: this.#mimes.get(req.url.path.split(".").pop()).toLowerCase(),
|
||||
body: await fs.readFile(path.join(dir, req.url.path.replace(route, "")))
|
||||
});
|
||||
} catch {
|
||||
return res.reply({
|
||||
code: 404,
|
||||
body: "404 - file not found"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
const router = new Router();
|
||||
export default router;
|
||||
export const routes = router.routes;
|
||||
|
||||
Map.prototype.getRegex = function(path, method, tmp) {
|
||||
return (!(tmp = [...this.entries()].filter(r => r[0].exec(path) && r[1].method.includes(method))[0])) ? false : tmp[1].f;
|
||||
Map.prototype.getRoute = function(path, method, tmp) {
|
||||
return (!(tmp = [...this.entries()].filter(r => ( r[0] === path || r[0].exec?.(path) ) && r[1].method.includes(method) )[0])) ? false : tmp[1].f;
|
||||
};
|
||||
|
Reference in New Issue
Block a user