aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2021-04-11 10:58:41 +0000
committerterminaldweller <thabogre@gmail.com>2021-04-11 10:58:41 +0000
commit1e59a3983df3f464be83dac6eba5da28d3c28ff3 (patch)
treeafa57a0ef604ad0acbe788573572f0a1154a879a
parentadded caching (diff)
downloadblog-1e59a3983df3f464be83dac6eba5da28d3c28ff3.tar.gz
blog-1e59a3983df3f464be83dac6eba5da28d3c28ff3.zip
a little refactor
-rwxr-xr-xserver.js40
1 files 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);