Answer the question
In order to leave comments, you need to log in
[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;
}
Answer the question
In order to leave comments, you need to log in
Do you have redux-thunk or redux-saga installed, or is it all done with bare redux?
child_process.execSync(`find ${process.cwd()}/Modules -type f`).toString().split('\n')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question