I\'m trying to create a simple CMS with express.js that dynamically creates routes. It gets a JSON from a database that looks like this:
pagesfromdb = {
As @supermova said in the comments, it is possible to update Express on-the-fly. Another architecture to consider is the one similar to classic CMSs (like Wordpress, for example). In other CMSs, all requests go to the same 'callback' and on every request you look up in the database what page to serve for that URL.
app.get('/*', function (req, res) {
db.findPage({ slug: req.url}, function (err, pageData) {
res.render('page-template', {
pageContent: pageData.content,
pageTitle: pageData.title
});
});
});
There is a significant speed decrease as a result of this method, but in the end I think it is more sane. If speed is a huge issue you can set up a caching system (like Varnish) but there will be headaches with the approach of modifying Express routes on-the-fly. For example, what if you have to scale to two web servers? How do you keep them in sync if server A gets the 'create page' request and so it knows to update its routes, but what about server B? With every request going to the database you will be able to scale horizontally better.