使用Express和Sequelize创建动态的RESTful API

介绍 (Introduction)

In this article we will discuss what an API is, what it means to be RESTful, and how to implement these using Node.js. The Node.js packages we will be using will be Express for our API endpoints and Sequelize to query our database.

在本文中,我们将讨论API是什么,RESTful的含义以及如何使用Node.js来实现这些API。 我们将使用的Node.js包将Express用作API端点,并使用Sequelize来查询数据库。

Learning how to create an API is a delicate process. Developers just want to build endpoints fast, so they can quickly get something ready for a webpage to consume. However, learning how to make things RESTful will make your API more consistent, predictable, and scalable.

学习如何创建API是一个微妙的过程。 开发人员只想快速构建端点,以便他们可以快速准备好要使用的网页。 但是,学习如何使事物成为RESTful将使您的API更加一致,可预测和可扩展。

This article is assuming you know how to create an Express server and connect to a database with Sequelize.

本文假设您知道如何创建Express服务器并使用Sequelize连接到数据库。

A full code example is available if you just want to get into the code.

如果您只想进入代码,则可以使用完整的代码示例。

术语 (Terminology)

Let’s define what it means to build a RESTful API.

让我们定义构建RESTful API的含义。

Creating an API (Application Programming Interface) is how we can setup logical actions to perform certain functions in our application. For example, we can setup a function called Create Customer and that will provide us an interface, so we don’t have to understand how it will actually create the customer, all we need to know is how to call the function. It’s then up to the programmer to implement the requirements for the API.

创建API(A pplication P AGC软件覆盖整个院落)是我们如何建立合理的行动,在我们的应用程序执行某些功能。 例如,我们可以设置一个名为Create Customer的函数,该函数将为我们提供一个接口,因此我们不必了解它将如何实际创建客户,我们只需要知道如何调用该函数即可。 然后由程序员来实现API的要求。

Creating a RESTful (REpresentation State Transfer) API means we will follow a pattern of how to logically setup endpoints by using HTTP methods. For example, when you type in your browser to go to a webpage it will call the HTTP GET method to retrieve the webpage for display. We will discuss later all the methods we need to know to create our RESTful API.

创建一个RESTful(RE Presentations泰特贸易交接)API的手段,我们将遵循的模式如何建立逻辑端点通过使用HTTP方法。 例如,当您键入浏览器以访问网页时,它将调用HTTP GET方法来检索要显示的网页。 稍后我们将讨论创建RESTful API所需的所有方法。

入门 (Getting Started)

Before we start, let’s plan out what is required in order to build our API. For example, let’s say we have a React application that has a customer page. On this page the user can create, show all, update, and delete customers. The React application will then make API calls to our Express server, and in turn the API will perform the action on our database. We will also need to prefix our API endpoints with a version number (This is useful if you need to build more APIs but need to maintain older versions).

在开始之前,让我们计划一下构建API所需的条件。 例如,假设我们有一个具有客户页面的React应用程序。 用户可以在此页面上创建,显示全部,更新和删除客户。 然后,React应用程序将对我们的Express服务器进行API调用,然后API将对我们的数据库执行操作。 我们还需要在API端点前面加上版本号(如果您需要构建更多的API但需要维护较旧的版本,这将非常有用)。

RESTful端点 (RESTful Endpoints)

GET / api / v1 / customers (GET /api/v1/customers)

This will return an array of customers in our database.

这将在我们的数据库中返回一系列客户。

GET / api / v1 / customers /:id (GET /api/v1/customers/:id)

This will return one customer specified by the :id parameter.

这将返回一个由:id参数指定的客户。

POST / api / v1 / customers (POST /api/v1/customers)

This will create a customer in our database.

这将在我们的数据库中创建一个客户。

放置/ api / v1 / customers /:id (PUT /api/v1/customers/:id)

This will update a customer specified by the :id parameter.

这将更新由:id参数指定的客户。

删除/ api / v1 / customers /:id (DELETE /api/v1/customers/:id)

This will delete a customer specified b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值