0

I'm trying to require/run all the files in a folder but having issues:

Inside Endpoints: index.js;

const { Router } = require("express");
const router = Router();

router.get("/", async (req, res) => {
  res.render("./../../views/home.ejs");
});

module.exports = router;

Inside Core-routers (routers) core.js;

const { readdirSync } = require("fs");
const findEndpoints = readdirSync(__dirname+"/../endpoints");
const endpoints = findEndpoints.filter((c) => c.split(".").pop() === "js");
class Routers {
  constructor(app) {
    this.app = app;
  }
  load() {
    for (let i = 0; i < endpoints.length; i++) {
      if (!endpoints.length) throw Error("No Endpoints Found.");
      this.app.use(require(`${__dirname}/../endpoints/${endpoints[i]}`)("/"));
    }
}
module.exports = Routers;

can someone explain how I can get it load all the endpoints and make them usuable

Johnty
  • 246
  • 1
  • 2
  • 18
  • there are a lot of problems here. If `endpoints.length` is 0 you will never go into the body of the for loop, you should check/throw before the for loop. – about14sheep Feb 15 '22 at 03:08
  • Also I can not see the kind of logic you are doing for instantiating the routers you are importing but `app.use` takes in the route first and then the instantiated router. – about14sheep Feb 15 '22 at 03:14
  • theres also the issue of it rendering ("*") (404/error) pages combined therefore the site is always gonna display 404. @about14sheep However I found the solution for loading the files however still face the aforementioned issue. – Johnty Feb 15 '22 at 03:14
  • I think you need to change the logic here, although this might look clever (I would argue it doesn't); it can lead to many hard-to-debug errors and is difficult to read. What if you had a route in the endpoints path you didn't want imported here? – about14sheep Feb 15 '22 at 03:18
  • If you look at my so-called "solution" you might get a better understanding? @about14sheep – Johnty Feb 15 '22 at 03:19

1 Answers1

0

One possible solution however doesnt prevent the 404 or otherwise known as the "*" pages automatically loads and prevents any other page being loaded.

const { readdirSync } = require("fs");
const findEndpoints = readdirSync(`${__dirname}/../endpoints`);
const endpoints = findEndpoints.filter((c) => c.split(".").pop() === "js");

class Routers {
  constructor(app) {
    this.app = app;
  }
  loadEndpoints() {
    for (let i = 0; i < endpoints.length; i++) {
      if (!endpoints.length) throw Error("No Endpoints Found.");
      console.log(endpoints[i]);
      this.app.use("",require(`${__dirname}/../endpoints/${endpoints[i]}`);
    }
  }
}
module.exports = Routers;

If anyone has a solution to prevent above mentioned issue, feel free to contribute.

Johnty
  • 246
  • 1
  • 2
  • 18
  • you will need to have the name of the route as well, so if your files are named after the routes you might be able to do ``/{endpoints[i]}`` as the first argument to `app.use` – about14sheep Feb 15 '22 at 03:28
  • Hmm, thats a possibility, I also have a issue with ```js app.use("/assets",express.static(path.join(__dirname, "assets"))); ``` it never actually loads the assets :/ – Johnty Feb 15 '22 at 03:51
  • try moving the require statement outside of the app.use – about14sheep Feb 15 '22 at 03:55