Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!