I am making a rest api. And I have multiple optional params. Here is the solution i have taken, but is there another solution? The url can be
www.myurl.com/
or
www.myurl.com/faqid/22
or
www.myurl.com/faqid/22/locale/english
Here is my implementation
var getFaq = (req, res) => {
let faqDetails = FAQ.map(obj => obj);//it is a mock json for now
faqDetails = req.params.faqid ? faqDetails.filter(obj => obj.id == req.params.faqid) : faqDetails;
faqDetails = req.params.topic ? faqDetails.filter(obj => obj.topic == req.params.topic) : faqDetails;
return res.status(200).send(faqDetails);
}
router.get('/:faqid/topic/:topic', getFaq)
router.get('/:faqid', getFaq)
router.get('/', getFaq)