内心宁静_什么是宁静路线以及如何使用它们

内心宁静

Chances are that you have already heard about REST APIs even before you stumbled upon this article. But maybe you never fully understood what that meant, or as to why it is used so frequently. The crux of the matter is that, it is just a set of constraints in API design.

很有可能您在偶然发现本文之前就已经听说过REST API。 但是,也许您从未完全理解过它的含义,或者为何如此频繁地使用它。 问题的关键在于,这只是API设计中的一组约束

But why would this be necessary? Can’t an API endpoint have any pattern a developer wishes to keep? After all, an API is just an interface for data transfer, it would work regardless of the pattern of the server requests, as long as it correctly matches the API design. Right?

但是为什么这是必要的? API端点不能有开发人员希望保留的任何模式吗? 毕竟,API只是数据传输的接口,只要服务器正确匹配API设计,它就可以工作,而与服务器请求的模式无关。 对?

In case you had this dialogue with yourself, you would be right. But you would also have to deal with the server security issues, portability issues, platform-compatibility issues and scalability issues among other things! So it is a wiser choice to choose RESTful design for your API. Apart from all these advantages, REST APIs can provide a sanity check for most developers since it makes it easier to collaborate.

如果您与自己进行这种对话,那是对的。 但是您还必须处理服务器安全性问题可移植性问题平台兼容性问题可伸缩性问题! 因此,为API选择RESTful设计是一个明智的选择。 除了所有这些优点之外,REST API还可以为大多数开发人员提供健全性检查,因为它可以简化协作。

I hope that I have managed to convince you that RESTful routing is essential, now let us see what the fuss is all about!

我希望我已经成功说服了您RESTful路由是必不可少的,现在让我们看看所有这些大惊小怪!

Let us first understand why it is called RESTful routing in the first place. Note that ‘RESTful routes’ and ‘REST API endpoints’ are used interchangeably when trying to describe the concept. Here REST is an acronym for Representational State Transfer. Before we can understand what this means, it’s important that you have some basic understanding of how clients and servers communicate.

首先让我们了解为什么将其称为RESTful路由。 请注意,在尝试描述概念时,“ RESTful路由”和“ REST API端点”可互换使用。 REST是代表性状态转移的缩写。 在我们理解这意味着什么之前,对客户端和服务器如何通信有一些基本的了解是很重要的。

The gist of the client-server architecture is that a browser (client) is able to send multiple requests to the server. Here, the server needs no information regarding the client itself, hence it also termed as a stateless transaction of data. This implies that the server will send a response corresponding to the current state of the client. RESTful design leverages this concept to simplify API design!

客户端-服务器体系结构的要点是浏览器(客户端)能够向服务器发送多个请求。 在这里,服务器不需要有关客户端本身的信息,因此也称为数据的无状态交易。 这意味着服务器将发送与客户端当前状态相对应的响应。 RESTful设计利用此概念来简化API设计!

TL;DR - it is called RESTful design because the client initiates a change in its state through a server request sent using a representation of its current state.

TL; DR-之所以称为RESTful设计,是因为客户端通过使用其当前状态表示形式发送的服务器请求启动其状态更改。

To simplify it further, RESTful routes are a standard set of rules that are used to carry out the CRUD operations, by using a set of HTTP verbs to make server requests.

为了进一步简化,RESTful路由是一组标准规则,用于通过使用一组HTTP动词进行服务器请求来执行CRUD操作。

If you feel like you could use some more information regarding the client-server architecture, you can find it in the following article.

如果您觉得可以使用有关客户端-服务器体系结构的更多信息,可以在下面的文章中找到它。

If the previous section seemed a tad bit confusing, it’s completely fine to ignore the unnecessary details, and just learn about the implementation alone, as long as you understand the steps involved in the actual API design.

如果上一节有点令人困惑,则可以忽略不必要的细节,而仅了解实现即可,只要您了解实际API设计中涉及的步骤即可。

In a REST API, there are just seven endpoints to perform any of the CRUD operations. They all use a HTTP verb to perform server requests based on the operation needed. An acronym that can help you remember all these routes, in the order that they need to be declared is INCSEUD (ink-say-ud).

在REST API中,只有七个端点可以执行任何CRUD操作。 它们都使用HTTP动词根据所需的操作执行服务器请求。 可以帮助您记住所有这些路线的首字母缩写词是INCSEUD (ink-say-ud)。

Consider an example wherein you are creating a CRUD application by using MongoDB to maintain a record of all the dogs you have (why not). Refer the table to take a look at the 7 RESTful routes.

考虑一个示例,其中您正在使用MongoDB维护所有狗的记录(为什么没有)来创建CRUD应用程序。 请参考下表以了解7种RESTful路由。

A table illustrating the different RESTful API endpoints
A table illustrating the different RESTful API endpoints
一张表,说明了不同的RESTful API端点
  • Index: This is the API endpoint that will return all documents stored in a particular collection. In order to make use of this endpoint, you will need to make a GET request to the server.

    索引:这是API端点,它将返回存储在特定集合中的所有文档。 为了利用此端点,您将需要向服务器发出GET请求。

  • New: This is an endpoint that is used to display a form, which is used to fill the data as per the Document Schema. Note that this route does not add new documents to the collection yet.

    新增:这是一个用于显示表单的端点,该端点用于根据“文档模式”填充数据。 请注意,此路由尚未将新文档添加到集合中

  • Create: This is the route that is meant to add a new document to the collection. You would need to make a POST request to send data to this API endpoint.

    创建:这是将新文档添加到集合中的途径。 您将需要发出POST请求,以将数据发送到此API端点。

  • Show: This is the endpoint that returns a particular document in JSON format.

    Show :这是返回JSON格式的特定文档的端点。

  • Edit: This will display a form to update a particular document. You can use the SHOW route to fetch a particular document’s data and use that to pre-populate the form fields. Note that this will not make any changes to the collection yet.

    编辑:这将显示一个表单来更新特定文档。 您可以使用SHOW路由来获取特定文档的数据,并使用该数据来预先填充表单字段。 请注意,这不会对集合进行任何更改。

  • Update: This is used to update the document of a particular id, by making a PUT request to the server. This will not return the updated document, instead it is common to redirect a user to the SHOW page of the particular document.

    更新:用于通过向服务器发出PUT请求来更新特定id的文档。 这不会返回更新的文档,而是将用户重定向到特定文档的SHOW页面是很常见的。

  • Destroy: This will delete a particular document from the collection, by making a DELETE request to the server. After this process, it is common to redirect a user to the INDEX page.

    销毁:通过向服务器发出DELETE请求,这将从集合中删除特定文档。 完成此过程后,通常会将用户重定向到INDEX页面。

In case you aren’t familiar with MongoDB or the Mongoose ODM, you can go through the following article, which gives an introduction to the topic.

如果您不熟悉MongoDB或Mongoose ODM,可以阅读下面的文章,其中对该主题进行了介绍。

The best way to grasp the importance of these RESTful routes, is by building a CRUD application using them. To do that using Express.js and MongoDB, you can take a look at the following article.

掌握这些RESTful路由的重要性的最佳方法是使用它们构建CRUD应用程序。 要使用Express.js和MongoDB做到这一点,您可以看一下以下文章。

In case you prefer a video tutorial to better understand the workflow involved in creating a REST API using Express and Mongo, you can check out this video from Traversy Media.

如果您希望通过视频教程更好地了解使用Express和Mongo创建REST API所涉及的工作流程,则可以从Traversy Media观看此视频。

Building a RESTful API
构建一个RESTful API

That’s it! You now know what a RESTful API means and what the 7 RESTful routes are. You also have a few resources to go ahead and build one yourself. It is a very important concept in full stack development, but all you need to build is a single CRUD application in order to learn it thoroughly!

而已! 您现在知道RESTful API的含义以及7条RESTful路由是什么。 您也有一些资源可以自己建立。 在全栈开发中,这是一个非常重要的概念,但是您只需要构建一个CRUD应用程序就可以全面学习它!

Lastly, consider going through the following article to take a look at some best practices for building a REST API of your own.

最后,请考虑阅读以下文章,以了解一些构建自己的REST API的最佳实践。

翻译自: https://medium.com/@rajat_m/what-are-restful-routes-and-how-to-use-them-929129ae7bf6

内心宁静

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值