My website has a route named date
where I display all posts from a specific date, for example: /date/26-12-2015
I also added basic pagination to prevent displaying all data at once. For instance, /date/26-12-2015/2
would show the second page of posts.
However, I encountered an issue when trying to create a route that counts all articles published on a given day using /date/26-12-2015/count
. The route mistakenly interprets it as a page parameter.
What should be my approach here? Should I filter the page parameter if it contains 'count', or is there a better way to handle this routing scenario?
Below is a snippet of my code:
router.get('/:date/:page', function(req, res){
var db = req.db;
var collection = db.get('collectionXYZ');
collection.find([...], function(e, docs){
res.json(docs);
});
});
// This section gets bypassed (as it's mistaken for a parameter of the page route)
router.get('/:date/count', function(req, res){
var dateStart = new Date(req.params.date);
var db = req.db;
var collection = db.get('collectionXYZ');
collection.count([...], function(e, docs){
res.json(docs);
});
});