简体   繁体   English

RESTful API应该执行哪些操作?

[英]What operations should a RESTful API have?

I'm new to express and apis, and I'm currently building a RESTful API with Express. 我是Express和API的新手,目前正在使用Express构建RESTful API。 For example let's say this is my model. 例如,假设这是我的模型。

Sheep = {
 name: String,
 age: Number,
 color: String
}

Aside from basic CRUD operations I saw from this tutorial , like create a Sheep, read a Sheep by id, update a Sheep by id, and remove a Sheep by id are there other operations I should include in my api? 除了从本教程中看到的基本CRUD操作之外,例如创建羊,按id读取羊,按id更新羊和按id删除羊,我还应该在api中包含其他操作吗?

In particular I'm wondering if I should make routes for find a sheep by name/age/color for example and how in best practice I would handle the routes for that. 特别是我想知道是否应该通过名称/年龄/颜色来确定要寻找绵羊的路线,以及在最佳实践中如何处理该路线。

I'm probably lacking information, but by the tutorial above, the route to find a sheep by id is "/sheeps/:sheepId" . 我可能缺少信息,但是在上面的教程中,通过id查找绵羊的途径是"/sheeps/:sheepId" How would I make a route to find by name? 我该如何寻找按名称查找的路线? If I do "/sheeps/:sheepName" would that not be identical to find sheep by id because if I go to my browser and go for example: "localhost:8080/sheeps/someNameOrId" how would the browser or in particular the client side recognize if it is a name or an id? 如果我执行"/sheeps/:sheepName" ,那么通过id查找羊就不一样了,因为如果我转到浏览器并进入例如: "localhost:8080/sheeps/someNameOrId" ,浏览器或客户端将如何边识别出它是名称还是ID?

I'm working with AngularJS as my client if that could help build a more helpful answer. 我正在以AngularJS作为客户端,如果那可以帮助建立更有用的答案。

Yours is a very classical case where we need to write RESTful resources for each opertaion. 您的情况非常经典,我们需要为每种操作编写RESTful资源。 As you rightly said there is no way for REST to know if you are passing ID or Name. 正如您正确地说的那样,REST无法知道您是在传递ID还是Name。 Ideally, in this case, you need to group the operations per resource. 理想情况下,在这种情况下,您需要对每个资源的操作进行分组。

In your case, you need to do CRUD byId or byName, then you need to write your REST resource as "/sheeps/ id /:sheepID" or "/sheeps/ name /:sheepName". 对于您的情况,您需要执行CRUD byId或byName,然后需要将REST资源写为“ / sheeps / id /:sheepID”或“ / sheeps / name /:sheepName”。

First of all, while REST has its standards and best practices, it is not a protocol. 首先,尽管REST有其标准和最佳实践,但它不是协议。 So, people might suggest different solution to a problem and they all might be right in there own sense. 因此,人们可能会针对问题提出不同的解决方案,并且从他们自己的角度来看,他们可能都是正确的。

Now coming to your question, in principal the uri should represent a resource on which you want to do an operation. 现在来问您的问题,原则上uri应该代表您要对其进行操作的资源。 It may or may not have the query parameters for a search, filtering etc. So, the representation /sheeps/{sheep id/name} is perfect for the sheep. 它可能具有或不具有用于搜索,过滤等的查询参数。因此,表示形式/ sheeps / {sheep id / name}非常适合绵羊。 Now for your problem statement, below could be valid RESTful ways of implementation: 现在为您的问题陈述,以下可能是有效的RESTful实现方式:

  • /sheeps?sheepId='123' AND /sheeps?sheepName='Eve' / sheeps?sheepId ='123'和/ sheeps?sheepName ='Eve'
  • /sheeps/{sheepId} AND /sheeps/name/{sheepName} / sheeps / {sheepId}和/ sheeps / name / {sheepName}

Syntactically they are all fine. 从语法上讲,它们都很好。 Now, if we think without getting too focused about the term REST, we would find that in both of our options the performance of our API is going to be same. 现在,如果我们认为不必太关注REST一词,就会发现在这两种选择中,API的性能都将是相同的。 So, it is more about readability, which means which option gives a better view of what we are doing. 因此,更多的是关于可读性,这意味着哪个选项可以更好地了解我们在做什么。 I think in both the cases a programmer would easily understand what we are doing. 我认为在这两种情况下,程序员都将很容易理解我们在做什么。

So, get rid if the thought that only one option is right and choose what you think is better. 因此,如果只有一种选择是正确的想法就摆脱,然后选择您认为更好的选择。

Other thing to notice in the above implementation is that when you do a search, like /sheeps?sheepId='123', and if you don't find something matching the parameters, you would ideally pass 204 No Content. 在上述实现中要注意的另一件事是,当您执行搜索时(例如/ sheeps?sheepId ='123'),并且如果找不到与参数匹配的内容,则最好传递204 No Content。 While in case of /sheeps/{sheepId} it should be 404 Not Found. 如果是/ sheeps / {sheepId},则应为404 Not Found。 What makes more sense in your case? 在您的情况下,什么更有意义? Choose as per your understanding. 根据您的理解选择。

This was for the sheepId and sheepName conflict. 这是因为SheepId和SheepName冲突。 Now for the requirement of getting sheeps by name/age/color, the below approach seems very logical. 现在,由于需要按名称/年龄/颜色来获取绵羊,以下方法似乎非常合乎逻辑。

  • /sheeps?age='5' /羊?年龄= '5'
  • /sheeps?color='black' /羊?颜色=“黑”
  • /sheeps?age='5';color='black' ?/羊年龄= '5';颜色= '黑'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM