Note that there are some explanatory texts on larger screens.

plurals
  1. POread folder tree and convert the tree into an object
    primarykey
    data
    text
    <p>I have the following folder structure:</p> <pre><code>+---controllers | | index.js | | | +---api | | index.js | | test.js | | | \---routes | | index.js | | | \---partials | | index.js | | | +---bugs | +---compatibility | +---documentation | | amd.js | | api.js | | jquery.js | | options.js | | usage.js | | | \---installation </code></pre> <p>I'm trying to create an object out of the tree that would look like this:</p> <pre><code>{ api: { index: require("index.js"), test: require("test.js") }, routes: { index: require("index.js"), partials: { index: require("index.js"), bugs: {}, compatibility: {}, documentation: { amd: require("amd.js"), api: require("api.js"), jquery: require("jquery.js"), options: require("options.js"), usage: require("usage.js") }, installation: {} } } } </code></pre> <p>I just cannot figure the logic to do it, I've managed only to get an array of the tree with the following code:</p> <pre><code>/** * Controllers */ var path = require("path"), util = require("util"), fs = require("fs"), wrench = require("wrench"); var controllers = {}, tree = wrench.readdirSyncRecursive(path.resolve(__dirname, "./")).filter(function (value) { return value !== "index.js"; }); var key, i = tree.length - 1; while (i &gt;= 0) { key = tree[i]; console.log(fs.lstatSync(path.resolve(__dirname, key)).isDirectory(), key.split(path.sep)); i--; } module.exports = controllers; </code></pre> <p>I'm not really sure how I'm supposed to create the object after I start looping the folder tree, but I'm thinking that I could only do it if I have some recursive function ?</p> <p><strong>EDIT</strong>:</p> <p>The reason why I'm trying to do it is because I'm trying to have some kind of dynamic routing for my express application.</p> <p>In my express application I would have something like:</p> <pre><code>application.get("/api/index", controllers.api.index); application.get("/api/test", controllers.api.test); application.get("/", controllers.routes.index); application.get("/partials/", controllers.routes.partials.index); application.get("/partials/documentation/amd", controllers.routes.partials.documentation.amd); ... </code></pre> <p>And each of those files would export something similar to:</p> <pre><code>exports.index = function (request, response) { return response.render("index"); }; </code></pre> <p>Depending on where the file is and maybe with some more logic (in case there is a model that it has to load).</p> <p>So probably there is a better way of managing dynamic routes than what I'm trying, if so I'm opened for suggestions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload