node.js rest_使用Node.js和HarperDB构建REST API
node.js rest
If you are building an application using Node.js, it can get a little overwhelming since there are a variety of databases to choose from and different ways to build APIs. One way to reduce development time and focus on the problem you are trying to solve is to use Database as a service to store the data. The advantage of this approach is to use a cloud database system without purchasing hardware which can be cost and time effective.
如果您使用Node.js构建应用程序,则可能会有些不知所措,因为有多种数据库可供选择,并且构建API的方法也不同。 减少开发时间并专注于要解决的问题的一种方法是将数据库作为服务来存储数据。 这种方法的优点是无需购买硬件即可使用云数据库系统,这可以节省成本和时间。
One such database service is HarperDB Cloud. To build REST APIs rapidly this service allows us to perform all database operations using a single endpoint. It supports a variety of programming languages such as JavaScript, Java, Python, and so on. Some of the features of HarperDB are the following:
HarperDB Cloud就是这样一种数据库服务。 为了快速构建REST API,该服务使我们可以使用单个端点执行所有数据库操作。 它支持多种编程语言,例如JavaScript,Java,Python等。 HarperDB的一些功能如下:
- single endpoint API单端点API
- allow JSON, CSVs file insertions允许插入JSON,CSV文件
- support SQL queries for full CRUD operations支持SQL查询以进行完整的CRUD操作
- Supports Math.js and GeoJSON支持Math.js和GeoJSON
- limited database configuration required需要有限的数据库配置
In this post, let’s build a simple REST API using Node.js and HarperDB Cloud to store some data. We are also going to use Express as the framework to build the Node.js server. It is a minimal and quite unopinionated framework.
在本文中,让我们使用Node.js和HarperDB Cloud构建一个简单的REST API,以存储一些数据。 我们还将使用Express作为构建Node.js服务器的框架。 这是一个小且完全不受质疑的框架。
先决条件 (Prerequisites)
Before you begin this tutorial, you’re going to need the following:
在开始本教程之前,您需要满足以下条件:
Node.js version above
12.x.x
installed on your local machine您本地计算机上安装的Node.js版本高于
12.xx
- Access to a package manager such as npm or yarn访问软件包管理器,例如npm或yarn
- Basic JavaScript and ES6 knowledge基本JavaScript和ES6知识
Access to a REST API client such as Postman or Insomnia
访问诸如Postman或Insomnia之类的REST API客户端
Access to a HarperDB Cloud instance (free tier)
访问HarperDB Cloud实例( 免费 )
To continue with the rest of the tutorial, please make sure you have an account with HarperDB Cloud and are logged in.
要继续学习本教程的其余部分,请确保您已经拥有HarperDB Cloud帐户并已登录。
入门 (Getting started)
Start by creating the project directory on a local development environment. Give this directory a name and navigate into it. Then, initialize this project to manage npm dependencies by creating a package.json
file.
首先在本地开发环境上创建项目目录。 为该目录命名并导航至该目录。 然后,通过创建package.json
文件初始化此项目以管理npm依赖项。
The --yes
flag uses the default settings when initializing a package.json
from npm config you might have set up.
从可能已设置的npm config初始化package.json
时,-- --yes
标志使用默认设置。
After the initializing step, let us add an express package. From the terminal window, run the command:
初始化步骤之后,让我们添加一个快递包裹。 在终端窗口中,运行以下命令:
Next, create a new file called index.js
at the root of the project with the following code to trigger a minimal server:
接下来,使用以下代码在项目的根目录创建一个名为index.js
的新文件,以触发小服务器:
In the above code snippet, the app
is an object provided by Express API for the developer to communicate with the application and bootstrap a server.
在上面的代码片段中, app
是Express API提供的对象,供开发人员与应用程序通信并引导服务器。
Go back to the terminal and trigger the common node index.js
to start the server. This node
command is the simplest way to trigger a development server when building APIs with Node.js. Now, open up your favorite REST client to test APIs. For the demonstration purpose, I am going to use Insomnia.
返回终端并触发公共node index.js
以启动服务器。 使用Node.js构建API时,此node
命令是触发开发服务器的简单方法。 现在,打开您喜欢的REST客户端以测试API。 出于演示目的,我将使用Insomnia 。
You can test API endpoint by invoking a call to http://localhost:8000
and it is going to return the result as shown below.
您可以通过调用http://localhost:8000
来测试API终结点,它将返回结果