node.js - Dynamic routing from database data -
i'm thinking implement small cms fun of it, , i'd user able create sites , pages cms.
for example there form asking user "site" name , under site user able create site tree of n levels deep saved in db.
ex: "blah/article", "blah/blah/blah/article".
my issue how implement such routing in expressjs? how can declare routes dynamically based on user has created , per site?
if example have 2 sites: site1 , site2.
localhost:3000/site1/somepathhere/blah/blah
localhost:3000/site2/someotherpathhere/blah/blah/blah
i want routes each site dynamic (no idea user create) , want each site routes apply site , not globally.
so far playing around code i'm not sure if there better way achieve this.
app.param('site', function(req, res, next, site) { req.site = site; next(); }); app.get('/:site', function(req, res) { res.send(req.site + "<br><pre>" + json.stringify(req.params, null, 4) + "</pre>"); }); app.get('/:site/*', function(req, res) { res.send(req.site + "<br><pre>" + json.stringify(req.params, null, 4) + "</pre>"); });
express routing,essentially playing(parsing , analyzing) strings. can implement url parser take routes read database.
Comments
Post a Comment