I am trying to read all image files in the folders recursively then return data to the browser. However, the data is not returned to the outside of function even the data is displayed on console inside the function. How can I fix it? Appreciate it for anyhelp! Here is my code:
var express = require('express');
var app = express();
var http = require('http');
var fs = require('fs');
var path = require("path");
//var flist = require('./fs-readdir-ar-fp.js');
app.use(express.static(__dirname + "/public"));
var filelist = function(dir) {
fs.readdir(dir,function(err,list){
list.forEach(function(file){
var file2 = path.resolve(dir, file);
fs.stat(file2,function(err,stats){
if(stats.isDirectory()) {
filelist(file2);
}
else {
//console.log(file2); // <-- data is displayed on console
}
})
})
})
};
flistArr = filelist('images');
console.log(flistArr); // <-- No data is displayed on console???
var server = http.createServer(function(req, res){
res.writeHead(200,{'Content-Type': 'application/json'});
res.end(JSON.stringigy(flistArr));
});
app.listen(3000);
console.log('3000 is the magic port');