+public route

+res.reply
+version bump
This commit is contained in:
Flummi 2020-03-23 15:12:15 +01:00
parent 110c3ddb0e
commit ee264a2802
3 changed files with 40 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "flumm-express", "name": "flummpress",
"version": "1.0.0", "version": "1.0.1",
"description": "Express für arme", "description": "Express für arme",
"main": "src/index.mjs", "main": "src/index.mjs",
"repository": { "repository": {

View File

@ -8,8 +8,6 @@ import views from "./views.mjs";
export default class flummpress { export default class flummpress {
constructor(opts) { constructor(opts) {
this.Router = router;
this.views = views;
this.opts = { ...{ this.opts = { ...{
views: path.resolve() + "/src/views", views: path.resolve() + "/src/views",
routes: path.resolve() + "/src/routes" routes: path.resolve() + "/src/routes"
@ -20,8 +18,9 @@ export default class flummpress {
return this; return this;
})(); })();
} }
listen(...args) { listen(...args) {
return http.createServer((req, res, r) => { return http.createServer(async (req, res, r) => {
req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, '')); req.url = url.parse(req.url.replace(/(?!^.)(\/+)?%/, ''));
req.url.qs = querystring.parse(req.url.query); req.url.qs = querystring.parse(req.url.query);
@ -29,9 +28,15 @@ export default class flummpress {
.on("data", d => void (data += d)) .on("data", d => void (data += d))
.on("end", () => void resolve(Object.fromEntries(Object.entries(querystring.parse(data)).map(([k, v]) => [k, decodeURIComponent(v)]))))); .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}`); res.reply = ({
code = 200,
!(r = router.routes.getRegex(req.url.pathname, req.method)) ? res.writeHead(404).end(`404 - ${req.url.pathname}`) : r(req, res); 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); }).listen(...args);
} }
}; };
export { router, views };

View File

@ -1,6 +1,8 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path";
export default new class { export default new class {
#mimes;
constructor() { constructor() {
this.routes = new Map(); this.routes = new Map();
} }
@ -21,6 +23,31 @@ export default new class {
post() { post() {
this.route("POST", arguments); 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) { Map.prototype.getRegex = function(path, method) {