diff options
author | terminaldweller <thabogre@gmail.com> | 2021-04-11 10:58:41 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2021-04-11 10:58:41 +0000 |
commit | 1e59a3983df3f464be83dac6eba5da28d3c28ff3 (patch) | |
tree | afa57a0ef604ad0acbe788573572f0a1154a879a /server.js | |
parent | added caching (diff) | |
download | blog-1e59a3983df3f464be83dac6eba5da28d3c28ff3.tar.gz blog-1e59a3983df3f464be83dac6eba5da28d3c28ff3.zip |
a little refactor
Diffstat (limited to '')
-rwxr-xr-x | server.js | 40 |
1 files changed, 19 insertions, 21 deletions
@@ -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); |