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

103
Visualizações
Fs file handler for api

So I want to make a routes folder that has subfolders that contain routes but I don't know how to use fs with it...

I only know how to get files in the routes folder and not in subfolders

here is my file handler code

const { readdirSync } = require('fs');


module.exports = function(app){
    readdirSync(__dirname).forEach(function(file) {
        if (file == "index.js") return;
        var name = file.substr(0, file.indexOf('.'));
        const route = require('./' + name)
                app.get(`/${route.name}`, async (req, res) => {
                route.run(req, res)                    
                })
    });
}

it gets the files from the routes folder

-routes
  |__index.js
  |__route.js
  |__route.js

I want to make it so it gets routes from subfolders

-routes
  |__index.js
  |
  |__image routes
     |__route1.js

I can't find any help online...

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

0

If you do not mind using external libraries i recommend using glob

What you want to do can be achieved with one function call:

const glob = require("glob");

glob("**/*.js", {cwd: __dirname}, (error, matches) => {
    if (error) return;
    console.log(matches);
})

This uses glob patterns to much files in a given directory, returning the whole paths like: image-routes/route1.js or index.js etc.

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