如何检查目录中多个文件的统计信息并作为 json 发送给客户端?
how to check stats for multiple file in directory and send as json to client?
假设我在目录中有三个文件,我想检查所有这些文件的统计信息并将出生时间发送给客户端,它总是返回一个文件统计信息,正如您在数据中看到的那样。我如何获取目录中所有文件的统计信息?
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' ] }
异步库是必经之路。 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
});
希望对您有所帮助。
假设我在目录中有三个文件,我想检查所有这些文件的统计信息并将出生时间发送给客户端,它总是返回一个文件统计信息,正如您在数据中看到的那样。我如何获取目录中所有文件的统计信息?
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' ] }
异步库是必经之路。 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
});
希望对您有所帮助。