了解RESTful API的基本要素

In this article, I will first introduce the key concepts related to an API. We will thoroughly discuss what is an API and fundamentals of the web. Then we will go on to build our own RESTful API using Typescript and Express. It’s a beginner-friendly post and even if you don't know Typescript, I think you will enjoy learning the fundamentals.

在本文中,我将首先介绍与API相关的关键概念。 我们将彻底讨论什么是API和网络基础。 然后,我们将继续使用Typescript和Express构建自己的RESTful API。 这是一篇适合初学者的文章,即使您不了解Typescript,我认为您也会喜欢学习基础知识。

了解RESTful API的基本要素_第1张图片
src src

介绍: (Introduction:)

Client: Accesses the functionalities made available by a server — for example, a web browser

客户端:访问服务器提供的功能-例如Web浏览器

Server: Provides functionality to other softwares or hardware called Clients.

服务器:为称为客户端的其他软件或硬件提供功能。

Resource: Anything on the web that is accessible by a client. And a server is the one who hosts these resources. The Search for a resource is made using the URL or an IP of the resource. How a client wants to access/interact with these resources depends on a text string provided by the client also known as an HTTP Request. This text string contains the specifications of the Resource the client is asking for. There are several HTTP request methods that define an HTTP request. The most common HTTP Request methods:

资源:客户端可以访问的任何网络内容。 服务器是托管这些资源的服务器。 使用资源的URL或IP进行资源搜索。 客户端如何访问/与这些资源交互取决于客户端提供的文本字符串,也称为HTTP Request 。 此文本字符串包含客户端要求的资源的规范。 有几种定义HTTP请求的HTTP请求方法 。 最常见的HTTP请求方法:

  1. GET Request for retrieving a resource from a server unchanged. Sending a request to read this article.

    GET请求从服务器检索资源的请求保持不变。 发送请求以阅读本文。

  2. POST Request to ask the server to store new information. Publishing a new medium article is an example of a POST request.

    POST请求以请求服务器存储新信息。 发布新的中篇文章是POST请求的示例。

  3. PUT Request is used to update an existing resource. Editing this medium article after publication would be an example of a PUT request.

    PUT请求用于更新现有资源。 发布后编辑此中篇文章将是PUT请求的示例。

  4. DELETE Requests are used to remove a resource from the server. Example- When I delete this article after publishing.

    DELETE请求用于从服务器中删除资源。 示例-发布后删除本文时。

HTTP Responses are what is sent by a server to the Client in response to an HTTP request. There are several HTTP Response status codes that indicate whether a specific HTTP request had been made successfully or not, a full list of which could be found here. An example of an unsuccessful request is the 404 page that pops up when making an invalid request.

HTTP响应是服务器响应HTTP请求而发送到客户端的内容。 有几种HTTP响应状态代码 ,用于指示是否已成功发出特定的HTTP请求,可以在此处找到其完整列表。 请求失败的一个示例是发出无效请求时弹出的404页面。

了解RESTful API的基本要素_第2张图片

In a nutshell, the web is a cycle of requests and responses that flow between a client and a server. Here is a great article if the reader wants to dig deep into HTTP.

简而言之,Web是在客户端和服务器之间流动的请求和响应的循环。 如果读者想深入了解HTTP, 这是一篇很棒的文章。

API (Application Programming Interface): Simply put, a software delegate, that allows two applications to interact with each other. People often confuse an API with a Database and a Server, but actually, it is neither of them. An API is a code (set of rules) and a part of a server that governs the access points of the requests made to the server, which in turn can access a database. It is the API that receives all the requests and sends the responses. Ex — If you type medium.com in your browser (the client) to read some articles, you are actually interacting with the medium’s API, that receives your request and sends the response to your browser, which in turn interprets the response and displays the article.

API(应用程序编程接口):简而言之,是一个软件委托,它允许两个应用程序相互交互。 人们经常将API与数据库和服务器混淆,但实际上两者都不是。 API是代码(规则集),是服务器的一部分,它控制对服务器的请求的访问点,而这些访问点又可以访问数据库。 API接收所有请求并发送响应。 例如-如果您在浏览器(客户端)中输入medium.com来阅读一些文章,则实际上是在与该媒体的API交互,该API接收您的请求并将响应发送到浏览器,浏览器反过来解释该响应并显示文章。

What is an API endpoint? Simply put, a URL of a resource. Each endpoint is a location where APIs can access the resources hosted by a server. These Endpoints define where resources can be accessed by APIs and play a key role in ensuring the proper functioning of the software that interacts with that API.

什么是API端点? 简而言之,资源的URL。 每个端点都是API可以访问服务器托管资源的位置。 这些端点定义了API可以在哪里访问资源,并且在确保与该API交互的软件的正常运行中起着关键作用。

了解RESTful API的基本要素_第3张图片
src src

What is an API call? — The process of making a request to the endpoints on a server by exposing a path. For Example — When reading this article you are actually making a request to an endpoint on the medium server, which is the URL of this article— XYZ.

什么是API调用? 通过公开路径向服务器上的端点发出请求的过程。 例如,当您阅读本文时,您实际上是在向中型服务器上的端点发出请求,这是本文的URL — XYZ

What is API Testing? Sending a combination of HTTP requests to the server and checking whether a proper response is obtained at the client’s end or not.

什么是API测试? 将HTTP请求的组合发送到服务器,并检查在客户端是否获得了正确的响应。

Web Service: A means by which two machines communicate with each other over a network. The AWS EC2 instances are a good example of a web service that provides secure, resizable compute capacity in the cloud. Here basically there is a remote computer sitting in some part of the world and it listens to the requests made from your computer. In response to the request, webserver or the remote computer returns the requested resources. Note this request and response takes place over a NETWORK. So, a web service is essentially a resource made available over the internet/network.

Web服务:两台机器通过网络相互通信的一种方式 AWS EC2实例是Web服务的一个很好的示例,该服务在云中提供安全的,可调整大小的计算能力。 这里基本上是一台位于世界某些地方的远程计算机,它侦听您计算机的请求。 响应该请求,Web服务器或远程计算机返回请求的资源。 请注意,此请求和响应是通过NETWORK进行的 。 因此,Web服务本质上是通过Internet /网络提供的资源。

So, What is the difference between a Web Service and an API? The terms Web Service and API are often used interchangeably because of the similar definitions but there is a difference between the two.

那么,Web服务和API有什么区别? 由于具有相似的定义,因此经常互换使用Web服务和API这两个术语,但是两者之间存在差异。

  1. Web Services uses a network/internet to facilitate a connection between two remote computers whereas API’s doesn't need to be web-based, it can be offline too.

    Web服务使用网络/互联网来促进两台远程计算机之间的连接,而API不必基于Web,它也可以脱机。
  2. API defines the rules for one software program to interact with another. When this interaction involves a data transfer(of the resources requested) it is a web service.

    API定义了一个软件程序与另一个软件程序进行交互的规则。 当此交互涉及(请求的资源的)数据传输时,它就是一个Web服务。

Web Services are a subset of APIs. All Web Services are APIs but all APIs are not Web Services.

Web服务是API的子集。 所有的Web服务都是API,但所有的API都不是Web服务。

了解RESTful API的基本要素_第4张图片
src src

One of the major ways to implement a web service is the REST. REST stands for REpresentational State Transfer. REST is an architectural style that lays a certain set of guidelines and constraints that a developer should keep in mind when designing an application. RESTful web services are those which obey the REST principles. RESTful web services employ HTTP request methods to transmit information between two applications. Everything in the RESTful architecture is about the resources. REST structures the resource mainly in a JSON format but YAML, XML are also used. A RESTful application exposes information about its resources using these JSON, YAML, etc. representation and enables the clients to take action on those resources. Refer Wikipedia for detailed information on these principles.

REST是实现Web服务的主要方法之一。 REST代表代表性状态转移。 REST是一种体系结构样式,它提出了一组准则和约束,开发人员在设计应用程序时应牢记这些准则和约束。 RESTful Web服务是那些遵循REST原则的服务。 RESTful Web服务采用HTTP请求方法在两个应用程序之间传输信息。 RESTful体系结构中的所有内容都与资源有关。 REST主要以JSON格式构造资源,但也使用YAML,XML。 RESTful应用程序使用这些JSON,YAML等表示形式公开有关其资源的信息,并使客户端能够对这些资源采取措施。 有关这些原理的详细信息,请参阅Wikipedia 。

What is a REST API? An API designed using the REST set of rules. REST APIs are a standardized architecture for building web APIs using HTTP methods. When a REST API is called the server will transfer to the client a representation of the requested resource. An important requirement for an API to be REST is the utilization of the HTTP methods to make a request over the network.

什么是REST API? 使用REST规则集设计的API。 REST API是用于使用HTTP方法构建Web API的标准化体系结构。 调用REST API时,服务器将向客户端传输所请求资源的表示。 API要是REST的一个重要要求是利用HTTP方法通过网络发出请求。

Benefits of REST API:- No hassle of installing additional software within your application as the API is available over the internet.- A Flexible approach as the data and resources are serialized in JSON or XML format.

REST API的优点:-由于可以通过Internet获得API,因此无需在应用程序中安装其他软件。-灵活的方法,因为数据和资源以JSON或XML格式序列化。

A NOTE ON SOFTWARES AND FRAMEWORKS:Postman: An efficient tool for API testing. It also provides a rich interface that makes it easy to use, there is no need to write bulks of code in order to check the APIs functionality. and can be run on Mac, Windows, Linux, and Chrome Apps.

关于软件和框架的说明: 邮递员 一种用于API测试的有效工具。 它还提供了丰富的界面,使其易于使用,无需编写大量代码即可检查API功能。 并可以在Mac,Windows,Linux和Chrome应用上运行。

了解RESTful API的基本要素_第5张图片
postman.com postman.com

TypeScript: An open-source programming language developed by Microsoft. It is a typed superset of JavaScript that compiles to a plain JavaScript. It adds static typing and class-based object-oriented programming to JavaScript. And can be used to develop both client-side & server-side JavaScript Applications.

TypeScript Microsoft开发的一种开放源代码编程语言。 它是JavaScript的类型化超集,可编译为普通JavaScript。 它将静态类型化和基于类的面向对象编程添加到JavaScript。 并且可用于开发客户端和服务器端JavaScript应用程序。

了解RESTful API的基本要素_第6张图片
src src

Express: is a popular web framework built on top of node.js that provides a robust set of features for app/mobile development. It is written in JavaScript and hosted within the node.js runtime environment. Helps to quickly develop a robust API with a myriad of HTTP utility methods and Middlewares.

Express:是在node.js之上构建的流行Web框架,为应用程序/移动开发提供了一组强大的功能。 它是用JavaScript编写的,并托管在node.js运行时环境中。 使用多种HTTP实用程序方法和中间件帮助快速开发健壮的API。

Now, let’s build a RESTful API using Typescript and Express. We will build a simple To-Do application, which will enable us to:

现在,让我们使用Typescript和Express构建RESTful API。 我们将构建一个简单的待办事项应用程序,使我们能够:

  • Create a new To-Do task,

    创建一个新的待办任务,

  • Get a task with the corresponding id,

    获取具有相应ID的任务,

  • Get a list of all tasks,

    获取所有任务的列表,

  • Remove the task from the list once the task is completed.

    任务完成后,从列表中删除任务。

Here is our package.json file which helps npm identify the project and handle the dependencies.

这是我们的package.json文件,可帮助npm识别项目并处理依赖项。

package.json package.json

应用程式 (App.ts)

One of the main advantages of typescript is Typesafety, it means the compiler can catch potential problems in your application during development rather than running into exceptions at the runtime. Typescript uses structures called interfaces for type checking.

Typescript的主要优点之一是Typesafety,它意味着编译器可以在开发过程中捕获应用程序中的潜在问题,而不是在运行时遇到异常。 Typescript使用称为接口的结构进行类型检查。

We start by defining an interface Task that includes three properties taskID, description of the task, and the time of the creation of the task-createdAT. We import this interface in our App.ts file.

我们通过定义一个接口的任务 ,其中包括三个属性的taskid,任务的描述 ,创建任务- createdAT的时间开始。 我们将此接口导入到App.ts文件中。

interface.ts 接口

Now, we go to our main App.ts file. What we exactly do here is create a empty dictionary and based on our endpoints{Get, Post, Delete} we manipulate this dictionary. A Post request will add a new key to this dictionary. A Get request will fetch a particular key or all keys from the dictionary. A Delete request will remove a specific key from the dictionary.

现在,我们转到主App.ts文件。 我们在这里要做的就是创建一个空字典,并根据端点{Get,Post,Delete}操作该字典。 发布请求将向该词典添加新密钥。 Get请求将从字典中获取特定键或所有键。 删除请求将从字典中删除特定的键。

App.ts 应用程式

We use the tsc compiler to transpile our TypeScript code to normal JavaScript.

我们使用tsc编译器将TypeScript代码转换为普通JavaScript。

- % tsc App.ts
- % node App.js
server started at http://localhost:8111

Once the server is started, we can use Postman to test our API. Let’s see what happens when we fire a number of different requests to the server.

服务器启动后,我们可以使用Postman测试我们的API。 让我们看看当我们向服务器发出许多不同的请求时会发生什么。

使用POST请求方法创建一个新的待办任务。 (Create a new To-Do task using a POST request method.)

了解RESTful API的基本要素_第7张图片
POST request to create a new task POST请求以创建新任务

使用GET请求方法获取具有特定ID的任务。 (Get a task with a specific id using the GET request method.)

了解RESTful API的基本要素_第8张图片
GET request to fetch a specific task. GET请求以获取特定任务。

获取所有任务。 (GET all tasks.)

了解RESTful API的基本要素_第9张图片
GET request to fetch all tasks. GET请求以获取所有任务。

任务完成后,从列表中删除任务。 (Remove the task from the list once the task is completed.)

了解RESTful API的基本要素_第10张图片
DELETE request. 删除请求。

I really wish that you found the article interesting. We were built a simple API and test the same. Feel free to comment below in case of any questions/doubt. You can also reach out to me on LinkedIn.

我真的希望您觉得这篇文章有趣。 我们构建了一个简单的API并对其进行了测试。 如有任何疑问/疑问,请在下面发表评论。 您也可以在LinkedIn上与我联系。

Some useful Reference Articles:

一些有用的参考文章:

  1. The Request/Response Cycle of the Web

    Web的请求/响应周期

  2. What exactly IS an API?

    API到底是什么?

  3. 5 Examples of APIs We Use in Our Everyday Lives

    我们日常生活中使用的5个API示例

  4. What is REST — A Simple Explanation for Beginners

    什么是REST-初学者的简单解释

Thank You!!

谢谢!!

翻译自: https://levelup.gitconnected.com/understanding-the-nuts-and-bolts-of-a-restful-api-implementing-it-using-express-and-typescript-adf5200fd2a0

你可能感兴趣的:(python,java)