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('plete');
});
ajax function:
$.ajax({
type: 'GET',
url: '/getCars',
dataType: 'JSON',
contentType: 'application/json'
}).done(function( response ) {
console.log(response);
});
Thanks in advance.
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('plete');
});
ajax function:
$.ajax({
type: 'GET',
url: '/getCars',
dataType: 'JSON',
contentType: 'application/json'
}).done(function( response ) {
console.log(response);
});
Thanks in advance.
Share Improve this question asked Mar 6, 2016 at 6:02 DCruz22DCruz22 8061 gold badge9 silver badges18 bronze badges 5- you are doing it wrong, you are sending results without knowing that fs.readFile is running async on each file, that means file is not read niether contents are pushed to array and array is already sent to client – Zeeshan Hassan Memon Commented Mar 6, 2016 at 6:21
- 2 I don't know who down-voted this question, If this questions is invalid one should ment while down-voting. All questions are not dumb, If some one is new to async style of paradigm he/she will do such coding. – Zeeshan Hassan Memon Commented Mar 6, 2016 at 7:27
-
I though that my problem was that i was using async methods but i tried using their sync counterparts (
readdirSync
,readFileSync
) and still didn't get the expected result. – DCruz22 Commented Mar 6, 2016 at 7:31 - 1 don't do sync coding, make most of async, see the simulated version here it works very well, github./zishon89us/node-cheat/blob/master/files/… – Zeeshan Hassan Memon Commented Mar 6, 2016 at 7:36
- It really works well, how can i learn more about the async functions on node.js?. Btw thanks for all your help! – DCruz22 Commented Mar 6, 2016 at 7:46
1 Answer
Reset to default 8Read content of all files inside a directory and send results to client, as:
choice 1 using npm install async
var fs = require('fs'),
async = require('async');
var dirPath = 'path_to_directory/'; //provice here your path to dir
fs.readdir(dirPath, function (err, filesPath) {
if (err) throw err;
filesPath = filesPath.map(function(filePath){ //generating paths to file
return dirPath + filePath;
});
async.map(filesPath, function(filePath, cb){ //reading files or dir
fs.readFile(filePath, 'utf8', cb);
}, function(err, results) {
console.log(results); //this is state when all files are pletely read
res.send(results); //sending all data to client
});
});
choice 2 using npm install read-multiple-files
var fs = require('fs'),
readMultipleFiles = require('read-multiple-files');
fs.readdir(dirPath, function (err, filesPath) {
if (err) throw err;
filesPath = filesPath.map(function (filePath) {
return dirPath + filePath;
});
readMultipleFiles(filesPath, 'utf8', function (err, results) {
if (err)
throw err;
console.log(results); //all files read content here
});
});
For plete working solution get this Github Repo and run read_dir_files.js
Happy Helping!