From 1e59a3983df3f464be83dac6eba5da28d3c28ff3 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sun, 11 Apr 2021 15:28:41 +0430 Subject: a little refactor --- server.js | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/server.js b/server.js index 8ebab87..a9f2973 100755 --- a/server.js +++ b/server.js @@ -25,29 +25,16 @@ app.set("views", path.join(__dirname, "views")); app.set("view engine", "ejs"); app.engine("ejs", require("ejs").__express); -app.get("/$", (req, res) => { - let readStream = fs.createReadStream( - path.join(__dirname, "mds", "cstruct2luatable.md"), - "utf-8" - ); - readStream.on("data", (chunk) => { - res.render("index", { - cache: true, - data: { - blogHttp: mit.render(chunk), - mds: fs.readdirSync(path.join(__dirname, "mds"), "utf-8"), - }, - }); - }); -}); - -app.get("/mds/:mdname$", (req, res) => { - if (req.params["mdname"] == "") { - res.write("nothing requested!"); - } +function renderAndSend(req, res) { try { + let viewPath; + if (req.path == "/") { + viewPath = "mds/cstruct2luatable.md"; + } else { + viewPath = req.path; + } let readStream = fs.createReadStream( - path.join(__dirname, req.path), + path.join(__dirname, viewPath), "utf-8" ); readStream.on("data", (chunk) => { @@ -62,6 +49,17 @@ app.get("/mds/:mdname$", (req, res) => { } catch (err) { console.log(err); } +} + +app.get("/$", (req, res) => { + renderAndSend(req, res); +}); + +app.get("/mds/:mdname$", (req, res) => { + if (req.params["mdname"] == "") { + res.write("nothing requested!"); + } + renderAndSend(req, res); }); app.listen(9000); -- cgit v1.2.3