Ver Fonte

Add returning of directories to the frontend for the main page.

Lee Morgan há 5 anos atrás
pai
commit
7cb8baf404
4 ficheiros alterados com 43 adições e 9 exclusões
  1. 1 1
      controllers/travel.js
  2. 30 0
      controllers/writing.js
  3. 5 8
      routes.js
  4. 7 0
      views/main/index.html

+ 1 - 1
travel.js → controllers/travel.js

@@ -10,7 +10,7 @@ module.exports = {
                     let newPath = `${path}/${response[i]}`;
                     let newObject = ({
                         name: response[i].replace(/-/g, " "),
-                        path: newPath.substring(newPath.indexOf("/images") + 7, newPath.length),
+                        path: newPath.substring(newPath.indexOf("/travel") + 7, newPath.length),
                         contents: []
                     });
 

+ 30 - 0
controller.js → controllers/writing.js

@@ -1,6 +1,36 @@
 const Comment = require("./models/comment.js");
 
+const fs = require("fs");
+
 module.exports = {
+    listDirectories: function(req, res){
+        const searchDir = function(path, currentObject){
+            let response = fs.readdirSync(path);
+
+            for(let i = 0; i < response.length; i++){
+                if(response[i].includes(".") === false){
+                    let newPath = `${path}/${response[i]}`;
+                    let newObject = ({
+                        name: response[i].replace(/-/g, " "),
+                        path: newPath.substring(newPath.indexOf("/writing") + 7, newPath.length),
+                        contents: []
+                    });
+
+                    currentObject.contents.push(newObject);
+                    searchDir(newPath, newObject);
+                }
+            }
+
+            return currentObject;
+        }
+
+        let result = searchDir(`${__dirname}/content`, {
+            contents: []
+        });
+
+        return res.json(result.contents[0].contents);
+    },
+
     /*
     GET: return all comments for the specific article
     req.params.article = String

+ 5 - 8
routes.js

@@ -1,5 +1,5 @@
-const controller = require("./controller.js");
-const travel = require("./travel.js");
+const writing = require("./controllers/writing.js");
+const travel = require("./controllers/travel.js");
 
 module.exports = function(app){
     let views = `${__dirname}/views`;
@@ -11,14 +11,11 @@ module.exports = function(app){
     //WRITING
     app.get("/writing/style", (req, res)=>{res.sendFile(`${views}/writing/index.css`)});
     app.get("/writing/code", (req, res)=>{res.sendFile(`${views}/writing/index.js`)});
-    app.get("/writing/comments/:article", controller.getComments);
-    app.post("/writing/comments", controller.createComment);
-    app.get("/writing/touchscreens", (req, res)=>{res.sendFile(`${views}/writing/touchscreens.html`)});
-    app.get("/writing/tablets", (req, res)=>{res.sendFile(`${views}/writing/tablets.html`)});
+    app.get("/writing/comments/:article", writing.getComments);
+    app.post("/writing/comments", writing.createComment);
+    app.get("/writing/directories", writing.listDirectories);
 
     //IMAGES
-    app.get("/images/touchscreen", (req, res)=>{res.sendFile(`${views}/images/touchscreen.jpeg`)});
-    app.get("/images/tablet", (req, res)=>{res.sendFile(`${views}/images/tablet.jpeg`)});
     app.get("/images/subline", (req, res)=>{res.sendFile(`${views}/images/subline.png`)});
     app.get("/images/budgeteer", (req, res)=>{res.sendFile(`${views}/images/budgeteer.jpeg`)});
     app.get("/images/sudoku", (req, res)=>{res.sendFile(`${views}/images/sudoku.png`)});

+ 7 - 0
views/main/index.html

@@ -186,6 +186,13 @@
                 };
             }
 
+            fetch("/writing/directories")
+                .then(response => response.json())
+                .then((response)=>{
+                    //do things
+                })
+                .catch((err)=>{});
+
             fetch("/travel/directories")
                 .then(response => response.json())
                 .then((response)=>{