In this Expressjs route file I'm trying to get (recursively) all the JSON files inside a ./data directory.
Actually I can console.log the file ehere you can see the A Mark, but I can't find the way to send the whole complete bunch of paths to the view once the async stuff finalized.
Some help would be really appreciated.
This is the data ./data structure:
--- dir1
`-- json1.json
`-- json2.json
--- dir2
`-- json3.json
--- dir3
const express = require('express'),
router = express.Router(),
fs = require('fs'),
path = require('path')
;
let scan = function (directoryName = './data') {
return new Promise((resolve, reject) => {
fs.readdir(directoryName, function (err, files) {
if (err) reject(err);
files.map((currentValue, index, arr) => {
let fullPath = path.join(directoryName, currentValue);
fs.stat(fullPath, function (err, stat) {
if (err) reject(err);
if (stat.isDirectory()) {
scan(fullPath);
} else {
console.log(currentValue); <= (A mark)
//resolve();
}
});
});
});
})
};
router.get('/', (req, res, next) => {
scan()
.then(data => res.render('list', {
title: 'List',
data: data
}))
.catch(next);
});
module.exports = router;