S
S
Stanislav2019-11-16 04:23:22
React
Stanislav, 2019-11-16 04:23:22

[NodeJS] How to display a list of files in an array?

Good day to all.
I'm writing a small MVC application on `NodeJS` and got a little stuck with creating a router, or rather with one of the functions of this class.
Get to the point. I want my application to "scan" the 'Modules' folder, which will contain various application modules, for example, the admin panel module, or the frontend itself, which is available to the user.
At the moment my 'Modules' folder looks like this:
Modules/Frontend
_____Controller/...
_____Model/...
_____View/...
_____router.json
Modules/Backend
_____Controller/...
_____Model/...
_____View/...
_____router.json
What do I need to implement?
To begin with, I created a function that scans the modules folder and gets the router of each of the folders, but the problem is the following, the function writes the paths to each router path, but when it returns the result to me, it erases the array and I get an empty array. This is due to the fact that the array `let list = []` is defined at the beginning of the function - that is, an empty array, without this the function simply does not work.
Here is the entire code for the function itself:

function Search(startPath){
    // Название файла роутера
    let filter = 'route.json';
    let list = [];

    // Проверка, если имя папки не задано, то присваиваем ей нужное имя
    if(startPath === undefined)
      startPath = 'Modules';
    
    // console.log(`Проверка директории: ${startPath}, файл не найден.`);

    // Проверка на ошибку, есть ли такая директория.
    if (!fs.existsSync(startPath)){
      console.log("Нет такой директории: ",startPath);
      return;
    }

    // Цикл поиска файла
    let files = fs.readdirSync(startPath);
    for(let i=0; i<files.length; i++) {
      let filename=path.join(startPath,files[i]);
      let stat = fs.lstatSync(filename);
      // Если файл не найден, то запускаем цикл повторно
      if (stat.isDirectory()) {
        this.Search(filename,filter);
      }
      // Если файл найден, кладем его в массив.
      else if (filename.indexOf(filter)>=0) {
        list = [filename];

      }
    }
    return list;
  }

At the output, I need to get the paths to all routers, or "glue" all the files of the routers into one and give an array at the output.
How can such a thing be implemented?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya, 2018-11-04
@xxx123321

Do you have redux-thunk or redux-saga installed, or is it all done with bare redux?

S
SANTA2112, 2019-11-16
@SANTA2112

child_process.execSync(`find ${process.cwd()}/Modules -type f`).toString().split('\n')

Or you can take https://www.npmjs.com/package/glob

V
Vladimir, 2019-11-18
@HistoryART

Move the variable with the array to the global (per function) and use the promise

let list

const your_function = () => {
  return new Promise((resolve,reject) => {
    //your code

   resolve(list)
  })
}

const myList = your_function() //Array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question