1

I am working on creating a static node.js server that just serves up the plain html, css, and javascript that is in the specified directory. I am trying to get the server to read every subdirectory and route the url to the file it specifies. However it only reads the root directory.

var fs = require('fs');

var array = fs.readdirSync(__dirname);
function getAllSub(array){
    for (i = 0; i < array.length; i++){
    if (array[i].indexOf(".") == (-1))
        {                     
            array = array.concat(array[i] + "/" + fs.readdirSync(__dirname + "/" +     array[i]));
        }
    if (array[i].indexOf("/") != (-1)){
        var foldcon = array[i].substr(array[i].indexOf("/") + 1);
        var folder = array[i].substr(0, array[i].indexOf("/"));
        foldcon = foldcon.split(",");
        for (n = 0; n < foldcon.length; n++){
            foldcon[n] = folder + "/" + foldcon[n]
            if (foldcon[n].indexOf(".") == (-1)){ 
                console.log([foldcon[n]]);
                foldcon[n] = getAllSub([foldcon[n]]);          

            }
        }
        array.splice(i, 1, foldcon);

    }

}


return array;
}
array = getAllSub(array);
console.log(array);

Right now this code reads the directory and it recognizes if an item in the array of files is a folder, however it doesn't add the files from the subdirectories into the array properly. Right now it kinda goes all infinite recursion, and I can't really figure out how to stop it. This isn't meant to be something I am actually going to use, I just thought it would be a good project to work on to introduce myself to the basics of node.js

edited^

1
  • That's not how things work here. You should re-accept my answer and start another question for this question Commented Apr 29, 2012 at 3:59

2 Answers 2

3

I know it's late, but this is the right answer for a recursive solution to reading file paths in sub-folders:

var fs = require("fs");

/**
 * Recurse through a directory and populate an array with all the file paths beneath
 * @param {string} path         The path to start searching
 * @param {array} allFiles      Modified to contain all the file paths
 */
function readdirSyncRecursive(path, allFiles) {
    var stats = fs.statSync(path);
    if (stats.isFile()) {
        // base case
        allFiles.push(path);
    } else if (stats.isDirectory()) {
        // induction step
        fs.readdirSync(path).forEach(function(fileName) {
            readdirSyncRecursive(path + "/" + fileName, allFiles);
        });
    }
}

var allFiles = [];
readdirSyncRecursive("/path/to/search", allFiles);
console.info(allFiles);
Sign up to request clarification or add additional context in comments.

Comments

2

var fs = require('fs');

var array = fs.readdirSync(__dirname);
for (i = 0; i < array.length; i++){
    if (array[i].indexOf(".") == (-1))
    {
        // you need to use the return value from concat
        array = array.concat(array[i] + "/" + fs.readdirSync(__dirname + "/" + array[i]));
        console.log('if executed');
    }

}
console.log(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.