我有以下rest来获取快递应用程序中的订单
http://localhost:2000/api/orders/chemists?orderBy=date&startDate=date1&endDate=date2
http://localhost:2000/api/orders/chemists?orderBy=chemist&startDate=date1&endDate=date2
我的问题如下..。
router.get("/chemists?orderBy=date", ...)
router.get("/chemists?orderBy=chemist", ...) 当我与邮递员进行查询时,上面的查询正在执行,而不是下面的查询。关于如何通过REST进行结构的任何建议。谢谢。
发布于 2020-04-03 19:27:08
您不会将查询字符串放入Express路由定义中。如果要使用该URL结构,则需要一个路由处理程序,并根据req.query中的值使用一个req.query
router.get("/chemists", (req, res) => {
if (req.query.orderBy === "date") {
// handle /chemists?orderBy=date
} else if (req.query.orderBy === "chemist") {
// /chemists?orderBy=chemist
} else {
// handle neither chemist or date specified
}
});如果您真的非常非常希望在Express中为它们提供不同的路线,则必须将URL设计更改为如下所示:
/chemists/date
/chemists/person然后你可以为每个人宣布一条单独的路线。由于这个排序顺序实际上只是请求的一个属性(无论哪种方式都请求相同的资源),所以(在REST设计中)它是查询字符串中的第一个选项,只有一个路由。
https://stackoverflow.com/questions/61018566
复制相似问题