2
我正在休息api。我有多個可選參數。這是我採取的解決方案,但是還有其他解決方案嗎? URL可以是如何處理節點中的url +多個otpional查詢參數?
www.myurl.com/
或
www.myurl.com/faqid/22
或
www.myurl.com/faqid/22/locale/english
這是我實現
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)