diff options
-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); |