+public route
+res.reply +version bump
This commit is contained in:
parent
110c3ddb0e
commit
ee264a2802
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "flumm-express",
|
||||
"version": "1.0.0",
|
||||
"name": "flummpress",
|
||||
"version": "1.0.1",
|
||||
"description": "Express für arme",
|
||||
"main": "src/index.mjs",
|
||||
"repository": {
|
||||
|
|
|
@ -8,8 +8,6 @@ import views from "./views.mjs";
|
|||
|
||||
export default class flummpress {
|
||||
constructor(opts) {
|
||||
this.Router = router;
|
||||
this.views = views;
|
||||
this.opts = { ...{
|
||||
views: path.resolve() + "/src/views",
|
||||
routes: path.resolve() + "/src/routes"
|
||||
|
@ -20,8 +18,9 @@ export default class flummpress {
|
|||
return this;
|
||||
})();
|
||||
}
|
||||
|
||||
listen(...args) {
|
||||
return http.createServer((req, res, r) => {
|
||||
return http.createServer(async (req, res, r) => {
|
||||
req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, ''));
|
||||
req.url.qs = querystring.parse(req.url.query);
|
||||
|
||||
|
@ -29,9 +28,15 @@ export default class flummpress {
|
|||
.on("data", d => void (data += d))
|
||||
.on("end", () => void resolve(Object.fromEntries(Object.entries(querystring.parse(data)).map(([k, v]) => [k, decodeURIComponent(v)])))));
|
||||
|
||||
console.log(`[${(new Date()).toLocaleTimeString()}] ${req.method} ${req.url.pathname}`);
|
||||
|
||||
!(r = router.routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : r(req, res);
|
||||
res.reply = ({
|
||||
code = 200,
|
||||
type = "text/html",
|
||||
body
|
||||
}) => res.writeHead(code, { "Content-Type": `${type}; charset=utf-8` }).end(body);
|
||||
|
||||
!(r = router.routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : await r(req, res);
|
||||
console.log(`[${(new Date()).toLocaleTimeString()}] ${res.statusCode} ${req.method}\t${req.url.pathname}`);
|
||||
}).listen(...args);
|
||||
}
|
||||
};
|
||||
export { router, views };
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default new class {
|
||||
#mimes;
|
||||
constructor() {
|
||||
this.routes = new Map();
|
||||
}
|
||||
|
@ -21,6 +23,31 @@ export default new class {
|
|||
post() {
|
||||
this.route("POST", arguments);
|
||||
}
|
||||
async static(dir = path.resolve() + "/public", route = /^\/s/) {
|
||||
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, "")), "utf-8")
|
||||
});
|
||||
} catch {
|
||||
return res.reply({
|
||||
code: 404,
|
||||
body: "404 - file not found"
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Map.prototype.getRegex = function(path, method) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user