Roland
Roland

Reputation: 9721

convert object to express dynamic routes

I have an object that looks like:

{
    api: {
        test: function () {}
    },
    routes: {
        docs: {
            options: function () {},
            usage: function () {}
        },
        index: function () {},
        bugs: {
            report: function () {}
        }
    }
}

Which is a object that mapped my application controllers by walking the directory tree and requiring each file inside the dir if there was a file. The file returns a module.exports function.

What I'm trying to do, is have the following routing in express:

application.get("/", controllers.routes.index);
application.get("/partials/index", controllers.routes.partials.index);
application.get("/partials/documentation/amd", controllers.routes.partials.documentation.amd);
application.get("/partials/documentation/api", controllers.routes.partials.documentation.api);
application.get("/partials/documentation/jquery", controllers.routes.partials.documentation.jquery);
application.get("/partials/documentation/options", controllers.routes.partials.documentation.options);
application.get("/partials/documentation/usage", controllers.routes.partials.documentation.usage);
application.get("/partials/installation/bower", controllers.routes.partials.installation.bower);
application.get("/partials/installation/source", controllers.routes.partials.installation.source);
application.get("/partials/compatibility/methods", controllers.routes.partials.compatibility.methods);
application.get("/partials/compatibility/support", controllers.routes.partials.compatibility.support);
application.get("/partials/bugs/requests", controllers.routes.partials.bugs.requests);
application.get("/partials/bugs/report", controllers.routes.partials.bugs.report);
application.get("*", controllers.routes.index);

somehow dynamic instead of hardcoding each one. I cannot figure out a logic to do so, I was thinking of doing something like:

var keys = Object.keys(controllers), //where controllers is my object containing the routes and api, what I just wrote one block of code above
    length = keys.length - 1;

while (length >= 0) {
    ... something that will check the object if it will contain any other object and will do a recursive iteration, then will somehow concatenate the parent object keys ...
}

I'm not sure how I could get all the parent keys of the subobject (something like routes docs options), because that is what I actually need to compose the route string that is passed to express.

Upvotes: 0

Views: 630

Answers (1)

Trevor Dixon
Trevor Dixon

Reputation: 24382

Something like this (http://jsfiddle.net/trevordixon/hym76/):

function registerRoutes(path, routes, level) {
    var level = level || 0;

    for (var p in routes) {
        if (typeof routes[p] === 'function') {
            if (p === 'index' && level === 0) app.get(path, routes[p]);
            else app.get(path + p, routes[p]);
        } else {
            // Recursive call; adds ancestors to the path with each call
            registerRoutes(path + p + '/', routes[p], level+1);
        }
    }
}

registerRoutes('/', controllers.routes);
registerRoutes('/api/', controllers.api);

Upvotes: 1

Related Questions