siliconchris
siliconchris

Reputation: 633

node.js fs.readdir doesn't show any files in directory

I know this is probably something near to a stupid question, but I'm banging my head and I simply can't solve th problem by myself or with the help of good old google.

I have an express 4 app in which in one route a function from a script is executed to list all available json files from a data directory.

My directory structure is this:

/                 - 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 requires routes_tags.js:

    var tagRoutes = require("./routes/routes_tags.js")(app);

routes_tags.js requires tags_list.js:

    var taglist = require('../modules/tags_list.js');

So app.js requires routes_tags.js which in turn requires tags_list.js in the route app.get(/tags).

    app.get("/tags", function(req, res) {

Below you'll see my code of 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;

Now, the output is, that there is NO output albeit the fact, that I have 5 json-files in the directory rfidTagDir.

rfidTagDir by the way is set globally in app.js as:

 app.set('rfidTagDir', 'data/rfidTagData');

Could someone point me to my, from a professional standpoint, probably stupid error I did here? I simply can't see why it should not work.

Best regards,

Christian

Ps.: You probably already know this, by I'm rather new to node.js so please excuse my rather inelegant coding style.

Upvotes: 0

Views: 1257

Answers (1)

siliconchris
siliconchris

Reputation: 633

OMG!

I'm sorry, the moment I posted the question I thought I'd just try out one last thing - and that did the trick.

I changed the definition of the rfidTagDir in app.js from

  app.set('rfidTagDir', 'data/rfidTagData');

to

  app.set('rfidTagDir', './data/rfidTagData');

Now the only thing that remains, is that the json response content is not correctly created. But I guess I'll want to work on that anyways, cause it's just rather bad coding with this hand crafted json structure.

Nevertheless, I'll make sure next time, I try solving my issues even more thoroughly before posting.

Upvotes: 2

Related Questions