Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda