在传统的REST中,我们应该像这样定义API:
我应该如何定义另一个‘获取一个’端点,以便通过除id以外的任何其他字段查询数据?例如:
我明白了:
SyntaxError: Invalid regular expression: /^?title=whatever\/?$/: Nothing to repeat
at RegExp (native)发布于 2016-06-04 17:22:32
ID应该是唯一的标识符。给定一个ID,您最多应该返回一个资源。这就是为什么像GET /api/things/:id这样的URI是有意义的。
对于其他属性,可能是唯一的,也可能不是唯一的,您可以有多个结果,所以使用GET /api/things端点并传递查询参数:/api/things?title=mytitle。
app.get('/api/things', function (req, res) {
console.log(req.query.title); //mytitle
ThingModel.find({
title: req.query.title
}, function (err, things) {
res.send(things);
});
});https://stackoverflow.com/questions/37632932
复制相似问题