From ee264a2802764e8c3b9188a44fc74a9b68645b0e Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 23 Mar 2020 15:12:15 +0100 Subject: [PATCH] +public route +res.reply +version bump --- package.json | 4 ++-- src/index.mjs | 17 +++++++++++------ src/router.mjs | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 45c206e..6d5d8eb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.mjs b/src/index.mjs index 7bde947..c341b65 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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 }; diff --git a/src/router.mjs b/src/router.mjs index 9a401c3..26bf54f 100644 --- a/src/router.mjs +++ b/src/router.mjs @@ -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) {