Dynamic routes with Express.js - is it even possible?

Each time I update the database with a new menu item, I try to update the routing with another route. Here is my sad ugly attempt:

Here, in app.js, I check the menu database and shazaam ... routes are created on the fly at startup. Cool:

// in app.js //
var attachDB = function(req, res, next) {
    req.contentdb = db.content;
    req.menudb = db.menu;
    req.app = app;  // this is the express() app itself
    req.page = PageController;
    next();
};
db.menu.find({}, function (err, menuitems){ 
    for(var i=0; record = menuitems[i]; i++) {
        var menuitem = record.menuitem;
        app.all('/' + menuitem, attachDB, function(req, res, next) {
            console.log('req from app all route: ',req)
            PageController.run(menuitem, req, res, next);
        }); 
    }

    http.createServer(app).listen(config.port, function() {
        console.log(
            '\nExpress server listening on port ' + config.port
        );
    });
});

Not truly elegant, but it is a proof of concept. Now here is the problem: when I save the new menu item in the Admin.js file, the database is updated, the router seems to be updated, but something about the request just explodes after clicking the menu link with a dynamically created route

, , , , - , , , , . , Admin.js, :

// in Admin.js //
menuItem: function(req, res, callback) {
    var returnMenuForm = function() {
        res.render('admin-menuitem', {}, function(err, html) {
            callback(html);
        });
    };
    var reqMenudb = req.menudb,
        reqContentdb = req.contentdb,
        reqApp = req.app,
        reqPage = req.page;

    if(req.body && req.body.menuitemsubmitted && req.body.menuitemsubmitted === 'yes') {
        var data = { menuitem: req.body.menuitem };
        menuModel.insert( data, function(err) {
            if (err) {
                console.log('Whoa there...',err.message);
                returnMenuForm();
            } else {
                // data is inserted....great. PROBLEM...the routes have not been updated!!!  Attempt that mimics what I do in app.js here...
                reqApp.all('/' + data.menuitem, function(req, res, next) {
                     // the 2 db references below are set with the right values here
                    req.contentdb = reqContentdb;
                    req.menudb = reqMenudb;
                    next();
                }, function(req, res, next) {
                    reqPage.run(data.menuitem, req, res, next);
                });

                returnMenuForm();
            }
        });
    } else {
        returnMenuForm();
    }
},

admin . log app.routes, , . , , undefined.

:

// in PageController.js //
module.exports = BaseController.extend({ 
    name: "Page",
    content: null,
    run: function(type, req, res, next) {
        model.setDB(req.contentdb);  /* <-- problem here, req.contentdb is undefined which causes me problems when talking to the Page model */
        var self = this;
        this.getContent(type, function() {
            var v = new View(res, 'inner');
            self.navMenu(req, res, function(navMenuMarkup){
                self.content.menunav = navMenuMarkup;
                v.render(self.content);
            });
        });
    },
    getContent: function(type, callback) {
        var self = this;
        this.content = {}
        model.getlist(function(records) {
            if(records.length > 0) {
                self.content = records[0];
            }
            callback();
        }, { type: type });
    }

,

// in Model.js //
module.exports = function() {

    return {
        setDB: function(db) {
            this.db = db;
        },
        getlist: function(callback, query) {
            this.db.find(query || {}, function (err, doc) { callback(doc) });
        },

, 'this' getlist undefined .

, - app.js. ? , , .

+3
2

:

  • .
  • , .

, async setTimeout, dallall db.

// menuitems is cached here in this module. You can make an initial load from db instead.
var menuitems = [];
// getting them is simple, always just get the current array. We'll use that.
var getMenuItems = function() {
    return menuitems;
}

// this executes when we have already inserted - calls the callback
var addMenuItemHandler = function(newItem, callback) {
    // validate that it not empty or that it does not match any of the existing ones
    menuitems.push(newItem);
    // remember, push item to local array only after it added to db without errors
    callback();
}
// this one accepts a request to add a new menuitem
var addMenuItem = function(req, res) {
    var newItem = req.query.newitem;

    // it will do db insert, or setTimeout in my case
    setTimeout(function(newItem){
        // we also close our request in a callback
        addMenuItemHandler(newItem, function(){
            res.end('Added.');
        });

    }, 2000);
};

module.exports = {
    addMenuItem: addMenuItem,
    getMenuItems: getMenuItems
}

, menuhandler.js. .

var menuHandler = require('./menuhandler');
var app = express();
// config, insert middleware etc here

// first, capture your static routes - the ones before the dynamic ones.
app.get('/addmenuitem', menuHandler.addMenuItem);
app.get('/someotherstaticroute', function(req, res) {
    var menu = menuHandler.getMenuItems();
    res.render('someview', {menu: menu});
});


// now capture everything in your menus.
app.get('/:routename', function(req, res){
    // get current items and check if requested route is in there.

    var menuitems = menuHandler.getMenuItems();
    if(menuitems.indexOf(req.params.routename) !== -1) {
        res.render('myview', {menu: menuitems});
    } else {
        // if we missed the route, render some default page or whatever.
    }
});

app.get('/', function(req, res) {
    // ...
});

db, ( elementsitems ), ( 1 db-, ).

: , Model.js. , this :

{
    setDB: function(db) {
        this.db = db;
    },
    getlist: function(callback, query) {
        this.db.find(query || {}, function (err, doc) { callback(doc) });
    }
}

, db. - app , -.

(reqApp = req.app), , . , ( ) , .

+3

db . , db.

-

app.all('*', function(req, res) {
    //read from your menu db and do the the route management yourself
});
+3

All Articles