JSFiddle - React, Tailwind, and code Playground

by Trevor Dixon

HTML

<pre>

JavaScript

var app = {get: function(path, fn) { document.write('app.get(\'' + path + '\', ' + fn.toString() + ');\n\n'); }};

controllers = {
    api: {
        test: function (req, res) {
    res.send('test');
}
    },
    routes: {
        docs: {
            options: function () {

},
            usage: function () {

}
        },
        index: function () {
    res.send('home');
},
        bugs: {
            report: function () {}
        },
        
        partials: {
            index: function() {},
            something: function() {}
        }
    }
}

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);