node.js fs.readdir 不显示目录中的任何文件
node.js fs.readdir doesn't show any files in directory
我知道这可能是一个近乎愚蠢的问题,但我正在敲我的头,我根本无法靠自己或在老好人的帮助下解决这个问题 google。
我有一个 express 4 应用程序,其中在一个路径中执行脚本中的一个函数以列出数据目录中所有可用的 json 文件。
我的目录结构是这样的:
/ - app root directory with app.js
/routes - routes_tags.js is stored here
/modules - tags_list.js is stored here
/data/rfidTagData - directory that holds json files
app.js 需要 routes_tags.js:
var tagRoutes = require("./routes/routes_tags.js")(app);
routes_tags.js 需要 tags_list.js:
var taglist = require('../modules/tags_list.js');
所以 app.js 需要 routes_tags.js,这又需要 tags_list.js 在路由 app.get(/tags).
app.get("/tags", function(req, res) {
下面你会看到我的tags_list.js
代码
var path = require('path');
var fs = require('fs');
//var taglist = function(tagDirectory, serverAddr, debug){
var taglist = function(app){
// get global app variables
var DEBUG = app.get('DEBUG');
var svrAddr = app.get('svrAddr');
var rfidTagDir = path.join('/', app.get('rfidTagDir'));
var responseContent = '';
if (DEBUG) {
console.log('list of tags requested - provided data: ');
console.log('directory: ' + rfidTagDir);
}
try {
fs.readdir(rfidTagDir, function(err, items) {
console.log(items);
if (!items.length) {
// directory appears to be empty
console.error("nothing to read in directory "+rfidTagDir);
responseContent = '{\'response\': \'warning\', \'message\': \'nothing to read from directory '+rfidTagDir+'\'}';
} else {
if (app.DEBUG) console.log(items);
responseContent = "{\'response\': \'info\', \'description\': \'list of all stored rfid tags\', \'tags\': ["
for (i in items) {
var tag = items[i].toString().substring(0,items[i].indexOf('.'));
responseContent += "{\'tag\': \'" + tag + "\', \'endpoint\': \'"+svrAddr+"/tags/tag/" + tag + "\', \'file\': \'"+items[i]+"\'}"
// we only need to add the , after an array element in the json
// structure, if there are sukzessive elements.
if (i<items.length-1) responseContent += ",";
}
responseContent += "]}"
}
});
} catch (err) {
console.error("could not read directory "+rfidTagDir+" to list available tags \nException output: " + err.toString());
responseContent = '{\'response\': \'error\', \'message\': \'could not read directory '+rfidTagDir+'\', \'exception\': \' '+err.toString()+'\'}' ;
}
if (DEBUG) {console.log(responseContent)}
return responseContent;
}
module.exports = taglist;
现在,输出是没有输出,尽管事实上我在目录 rfidTagDir 中有 5 个 json 文件。
rfidTagDir 顺便在 app.js 中全局设置为:
app.set('rfidTagDir', 'data/rfidTagData');
有人可以指出我在这里犯的从专业角度来看可能是愚蠢的错误吗?我就是不明白为什么它不起作用。
此致,
基督徒
Ps.: 你可能已经知道了,因为我是 node.js 的新手所以请原谅我相当不优雅的编码风格。
天哪!
抱歉,在我发布问题的那一刻,我想我会尝试最后一件事 - 结果成功了。
我更改了 app.js 中 rfidTagDir 的定义,从
app.set('rfidTagDir', 'data/rfidTagData');
到
app.set('rfidTagDir', './data/rfidTagData');
现在唯一剩下的就是 json 响应内容没有正确创建。但我想无论如何我都会想继续努力,因为用这种手工制作的 json 结构编码是相当糟糕的。
尽管如此,下次我会确保在发布之前更彻底地解决我的问题。
我知道这可能是一个近乎愚蠢的问题,但我正在敲我的头,我根本无法靠自己或在老好人的帮助下解决这个问题 google。
我有一个 express 4 应用程序,其中在一个路径中执行脚本中的一个函数以列出数据目录中所有可用的 json 文件。
我的目录结构是这样的:
/ - app root directory with app.js
/routes - routes_tags.js is stored here
/modules - tags_list.js is stored here
/data/rfidTagData - directory that holds json files
app.js 需要 routes_tags.js:
var tagRoutes = require("./routes/routes_tags.js")(app);
routes_tags.js 需要 tags_list.js:
var taglist = require('../modules/tags_list.js');
所以 app.js 需要 routes_tags.js,这又需要 tags_list.js 在路由 app.get(/tags).
app.get("/tags", function(req, res) {
下面你会看到我的tags_list.js
代码var path = require('path');
var fs = require('fs');
//var taglist = function(tagDirectory, serverAddr, debug){
var taglist = function(app){
// get global app variables
var DEBUG = app.get('DEBUG');
var svrAddr = app.get('svrAddr');
var rfidTagDir = path.join('/', app.get('rfidTagDir'));
var responseContent = '';
if (DEBUG) {
console.log('list of tags requested - provided data: ');
console.log('directory: ' + rfidTagDir);
}
try {
fs.readdir(rfidTagDir, function(err, items) {
console.log(items);
if (!items.length) {
// directory appears to be empty
console.error("nothing to read in directory "+rfidTagDir);
responseContent = '{\'response\': \'warning\', \'message\': \'nothing to read from directory '+rfidTagDir+'\'}';
} else {
if (app.DEBUG) console.log(items);
responseContent = "{\'response\': \'info\', \'description\': \'list of all stored rfid tags\', \'tags\': ["
for (i in items) {
var tag = items[i].toString().substring(0,items[i].indexOf('.'));
responseContent += "{\'tag\': \'" + tag + "\', \'endpoint\': \'"+svrAddr+"/tags/tag/" + tag + "\', \'file\': \'"+items[i]+"\'}"
// we only need to add the , after an array element in the json
// structure, if there are sukzessive elements.
if (i<items.length-1) responseContent += ",";
}
responseContent += "]}"
}
});
} catch (err) {
console.error("could not read directory "+rfidTagDir+" to list available tags \nException output: " + err.toString());
responseContent = '{\'response\': \'error\', \'message\': \'could not read directory '+rfidTagDir+'\', \'exception\': \' '+err.toString()+'\'}' ;
}
if (DEBUG) {console.log(responseContent)}
return responseContent;
}
module.exports = taglist;
现在,输出是没有输出,尽管事实上我在目录 rfidTagDir 中有 5 个 json 文件。
rfidTagDir 顺便在 app.js 中全局设置为:
app.set('rfidTagDir', 'data/rfidTagData');
有人可以指出我在这里犯的从专业角度来看可能是愚蠢的错误吗?我就是不明白为什么它不起作用。
此致,
基督徒
Ps.: 你可能已经知道了,因为我是 node.js 的新手所以请原谅我相当不优雅的编码风格。
天哪!
抱歉,在我发布问题的那一刻,我想我会尝试最后一件事 - 结果成功了。
我更改了 app.js 中 rfidTagDir 的定义,从
app.set('rfidTagDir', 'data/rfidTagData');
到
app.set('rfidTagDir', './data/rfidTagData');
现在唯一剩下的就是 json 响应内容没有正确创建。但我想无论如何我都会想继续努力,因为用这种手工制作的 json 结构编码是相当糟糕的。
尽管如此,下次我会确保在发布之前更彻底地解决我的问题。