如何检查多个文件中的统计数据目录和发送的JSON到客户端?

问题描述 投票:0回答:1

让我们假设我在目录中的三个文件,我想检查统计所有这些文件并发送birthtimes到客户端,它总是返回一个文件统计数据,你可以在数据看到。我怎样才能从目录中的所有文件统计?

app.js

  var path = './logs/ditLogs'
  fs.stat(path, function (err,stats) {
        console.log('STATS',stats);
        fileData.birthtime = stats.birthtime;
        //callback(stats.mtime);
    });

数据

{ birthtime: Tue Jul 12 2016 09:33:14 GMT-0400 (Eastern Daylight Time),
  filename: ['server.log', 'server1.log' ] }
node.js filesystems
1个回答
1
投票

异步图书馆是要走的路。 http://caolan.github.io/async/docs.html

我建议是这样的

const fs = require('fs');
const path = require('path');
const async = require('async'); // install with: npm install --save async

var dirPath = './logs/ditLogs';
// this will get you list of all files. in directory
var files = fs.readdirSync(dirPath);
var objToReturn = {};
// then using async do like this
async.eachSeries(files, function (file, callback) {
    var filePath = path.join(dirPath, file);
    fs.stat(filePath, function(err, stats) {
        // write stats data into objToReturn 
        callback();
   });
}, function(err) {
    // final callback when all files completed here send objToReturn to client
});

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.