S
S
squadbrodyaga2020-12-24 19:12:30
Express.js
squadbrodyaga, 2020-12-24 19:12:30

Is it possible to use require to include all files from a folder at once?

Hello, is it possible to use require() or some other method
to connect all files from one folder at once?

I need it to immediately connect all the routes at once. Now I do it like this:

app.use([
  require('./routes/home'),
  require('./routes/auth'),
  require('./routes/about'),
  require('./routes/premium'),
])

Is it possible to do it somehow? Or will you have to write all the require manually?
app.use([
  require('./routes'),
])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-12-24
@squadbrodyaga

loop through all the files in the folder, then write their paths to an array.

let arr = [];
let files = fs.readdirSync(папка);
for (let key in files) {
 // ваш код //
}
app.use(arr)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question