Im using the fs module of node.js to read all the files of a directory and return their content, but the array i use to store the content is always empty.
server-side:
app.get('/getCars', function(req, res){
var path = __dirname + '/Cars/';
var cars = [];
fs.readdir(path, function (err, data) {
if (err) throw err;
data.forEach(function(fileName){
fs.readFile(path + fileName, 'utf8', function (err, data) {
if (err) throw err;
files.push(data);
});
});
});
res.send(files);
console.log('complete');
});
ajax function:
$.ajax({
type: 'GET',
url: '/getCars',
dataType: 'JSON',
contentType: 'application/json'
}).done(function( response ) {
console.log(response);
});
Thanks in advance.
readdirSync,readFileSync) and still didn't get the expected result.