Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

79
Visualizações
Loading multiple express endpoint files to main app

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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda