I have a route method to return a list of filenames in a http response.
The data structure I am using for the response is eventually populated, albeit after the response is returned, meaning I get an empty and useless response.
I can see the datastructure is populated in the end, as the output the contents to the log show.
Code:
app.get('/hello',function(req,res){
var paths = {};
fs.readdir('./uploads', function(err, items) {
console.log(items);
for (var i=0; i<items.length; i++) {
paths[i] = items[i];
console.log(items[i]);
}
});
res.json(this,paths);
});
res.json(this.paths)returns an empty JSON response?