如何在5分钟内通过身份验证构建RESTful API —全部从命令行(第1部分)

by Niharika Singh

由Niharika Singh

如何在5分钟内通过身份验证构建RESTful API —全部从命令行(第1部分) (How to Build a RESTful API with Authentication in 5 minutes — all from your command line (Part 1))

If the title of this article excites you, then my friend, you’re about to achieve level 100 of satisfaction by the end. I’ll quickly go through the course of this article:

如果这篇文章的标题使您兴奋,那么我的朋友,您将在最后达到100级的满意度。 我将快速阅读本文:

  1. What we are about to create: RESTful API which handles logs of food items on a restaurant menu. The database used in the back-end will be MongoDB. (You can literally use any fricking database on this planet. There’s an exhaustive list of database connectors/non-database connectors supported by LoopBack below.)

    我们将要创建的是: RESTful API,该API处理餐厅菜单上食品的日志。 后端使用的数据库将是MongoDB。 (您实际上可以使用此星球上的任何变形数据库。下面有LoopBack支持的详尽的数据库连接器/非数据库连接器列表。)

  2. What is LoopBack: In extremely simple terms, it is highly extensible, open source Node.js framework used to create dynamic, end-to-end REST APIs very rapidly. APIs generated via LoopBack are Swagger APIs (world’s most popular API framework, and you’ll see why very soon). The front-end could be made in whichever framework you’re in love with; Angular or React.

    什么是LoopBack:简而言之,它是高度可扩展的开源Node.js框架,用于快速创建动态的端到端REST API。 通过LoopBack生成的API是Swagger API(世界上最流行的API框架,您很快就会看到原因)。 可以在您喜欢的任何框架中创建前端。 角度或React。

  3. Creating application via CLI: This is the WOW part which removes all the programming involved. LoopBack CLI is so beautiful that all the hours of development work are reduced down to seconds. Here, we’d be setting up our database using CLI.

    通过CLI创建应用程序:这是WOW部分,它删除了所有涉及的程序。 LoopBack CLI非常漂亮,以至于所有开发时间都减少到了几秒钟。 在这里,我们将使用CLI设置数据库。

  4. Creating data models via CLI: Again, no programming. All via the beautiful CLI.

    通过CLI创建数据模型:同样,无需编程。 全部通过漂亮的CLI。

  5. Setting up Authentication via CLI: If you have experience creating APIs, you know how tough it is to restrict parts of API using authentication. Setting up token-based authentication using Express+Node.js on the server side is a pain. All of that pain will be taken away by tasting the elixir of LoopBack! It is heaven’s own drink.

    通过CLI设置身份验证:如果您有创建API的经验,那么您会知道使用身份验证来限制API的部分是多么困难。 在服务器端使用Express + Node.js设置基于令牌的身份验证很麻烦。 品尝LoopBack的长生不老药,将消除所有这些痛苦! 这是天堂自己的饮料。

分步指南: (Step by Step Guide:)

Pre-requisites: Make sure you’ve got Node.js, Robomongo installed and MongoDB server running.

先决条件:确保已安装Node.js , Robomongo且MongoDB服务器正在运行。

步骤1:通过NPM安装LoopBack CLI (STEP 1: Install LoopBack CLI via NPM)

Open the terminal and write the following command to install LoopBack CLI so that ‘lb’ command can be accessed. Only through ‘lb’ command can we generate applications, models, data sources etc. For further reading: https://loopback.io/doc/en/lb2/Command-line-tools.html#using-yeoman

打开终端并输入以下命令以安装LoopBack CLI,以便可以访问“ lb”命令。 只有通过“ lb”命令,我们才能生成应用程序,模型,数据源等。进一步阅读: https : //loopback.io/doc/en/lb2/Command-line-tools.html#using-yeoman

$ npm install -g loopback-cli

Make sure you install this globally, or else ‘lb’ command might not work for you.

请确保您全局安装此软件,否则“ lb”命令可能对您不起作用。

步骤2:建立应用程式 (STEP 2: Creating Application)

Make a directory where you wish to store your project. I’ll name it ‘restaurant-menu’. Make sure you’ve opened this directory in your terminal so that all the files generated via LoopBack are stored in that folder.

创建一个目录,您要在其中存储项目。 我将其命名为“餐厅菜单”。 确保已在终端中打开此目录,以便将通过LoopBack生成的所有文件存储在该文件夹中。

Then enter the following command:

然后输入以下命令:

$ lb

A lot of questions will be asked, like those displayed in the image below.

就像下面图片中显示的那样,将会提出很多问题。

(To navigate among options, use arrow keys on your keyboard)

(要在选项之间导航,请使用键盘上的箭头键)

API已创建! (THE API IS CREATED!)

I’m not kidding. Don’t believe me? Run the application using the following command:

我不是在开玩笑。 不相信我吗 使用以下命令运行应用程序:

$ node .

If you point to localhost:3000, you’ll see something like this:

如果您指向localhost:3000,则会看到以下内容:

However, if you go to localhost:3000/explorer, you’ll see the gorgeous SwaggerAPI.

但是,如果您访问localhost:3000 / explorer,则会看到漂亮的SwaggerAPI。

LoopBack has set up all the routes for you:

LoopBack为您设置了所有路由:

GET users, POST users, PUT users, DELETE users, Login, Log out, Change Password. Literally everything! It would otherwise take hours of work to code this out.

GET用户,POST用户,PUT用户,DELETE用户,登录,注销,更改密码。 从字面上看一切! 否则,要花费大量时间进行编码。

Open this folder in any text editor. I’d be using Atom.

在任何文本编辑器中打开此文件夹。 我会使用Atom。

步骤3:连接MongoDB (STEP 3: Connecting MongoDB)

If you open datasources.json in the Server folder, you should see something like:

如果在Server文件夹中打开datasources.json ,应该会看到类似以下内容的内容:

{  "db": {    "name": "db",    "connector": "memory"  }}

This means that presently, the data source being used is the memory of our computer. We’ve got to change this to Mongo. So let’s install mongo connector:

这意味着当前正在使用的数据源是我们计算机的内存。 我们必须将其更改为Mongo。 因此,让我们安装mongo连接器:

$ npm install --save loopback-connector-mongodb

Alongside, I hope mongod is running. This is how you’d know it is running:

同时,我希望mongod正在运行。 这是您如何知道它正在运行的方式:

2018-01-27T15:01:13.278+0530 I NETWORK  [thread1] waiting for connections on port 27017

Now, let’s connect the connector!

现在,让我们连接连接器!

$ lb datasource mongoDS --connector mongoDB

This will ask a lot of questions as follows:

这将提出很多问题,如下所示:

Now modify datasources.json because we don’t wish to use memory. We wish to use Mongo.

现在修改datasources.json因为我们不希望使用内存。 我们希望使用Mongo。

{  "db": {    "host": "localhost",    "port": 27017,    "url": "",    "database": "food",    "password": "",    "name": "mongoDS",    "user": "",    "connector": "mongodb"  }}

So our database named: food is created.

因此,我们创建了名为: food的数据库。

步骤4:创建数据模型 (STEP 4: Creating Data Models)

Run following command to create data models:

运行以下命令以创建数据模型:

$ lb model

You may add however many properties to a particular model. To stop entering more properties, just hit Enter to get out of the CLI.

但是,您可以向特定模型添加许多属性。 要停止输入更多属性,只需按Enter键即可退出CLI。

Check out dishes.json in the Common/Models folder.

查看Common / Models文件夹中的dishes.json

{  "name": "dishes",  "base": "PersistedModel",  "idInjection": true,  "options": {    "validateUpsert": true  },  "properties": {    "name": {      "type": "string",      "required": true    },    "price": {      "type": "number",      "required": true    }  },  "validations": [],  "relations": {},  "acls": [],  "methods": {}}

You may edit the properties from this json file as well. It is not necessary to use CLI.

您也可以从此json文件编辑属性。 不需要使用CLI。

Now let’s rerun the server using the following command and head over to localhost:3000/explorer

现在,让我们使用以下命令重新运行服务器,然后转到localhost:3000 / explorer

$ node .

Now you’ll see 2 models: dishes, and user

现在您将看到2个模型: dishesuser

Now let’s POST some dish.

现在让我们发布一些dish

Now let’s GET the same dish.

现在让我们来做dish

You may play around with other HTTP requests too!

您也可以处理其他HTTP请求!

These APIs can be accessed outside the explorer as well:

这些API也可以在资源管理器之外访问:

http://localhost:3000/api/dishes

http:// localhost:3000 / api / dishes

步骤5:认证:蛋糕上的樱桃! (STEP 5: AUTHENTICATION: Cherry on the cake!)

To set up authentication, run the following command:

要设置身份验证,请运行以下命令:

$ lb acl

Now, let’s try to GET the dishes. Before that, please rerun the server.

现在,让我们尝试获取dishes 。 在此之前,请重新运行服务器。

Let’s get authenticated! For that, we need to get registered first. So we POST in users.

让我们通过身份验证! 为此,我们需要先注册。 因此我们在usersusers

Now, let’s log in.

现在,让我们登录。

Now, copy the ID in the response body and paste it in the Access Token field on top of the page.

现在,将ID复制到响应正文中,并将其粘贴到页面顶部的“访问令牌”字段中。

Now we are authenticated. YAY.

现在我们已通过身份验证。 好极了。

Now, let’s GET the dishes again.

现在,让我们再次取dishes

HOORAY!

哇!

Congratulations if you’ve successfully reached this step. So proud of you.

祝贺您成功完成此步骤。 为你骄傲。

Next steps would be to create a front end around this API which would be done later.

下一步将围绕该API创建一个前端,稍后再做。

可以在这里找到本文的前端教程。 在该教程中,我使用ReactJS围绕该API编织了一个前端。 (The frontend tutorial of this article can be found here. In that tutorial, I have used ReactJS to weave a frontend around this API.)

Bye folks! Happy coding.

再见! 快乐的编码。

翻译自: https://www.freecodecamp.org/news/build-restful-api-with-authentication-under-5-minutes-using-loopback-by-expressjs-no-programming-31231b8472ca/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395171.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MD5与SHA1

一、MD5 MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信…

java基本类型的默认值及其取值范围

转载于:https://www.cnblogs.com/billyu/p/5843845.html

tair client java_TFS(Taobao File System)Java Client 使用经验

TFS Java客户端的说明tfs-client-java版本选择:---------------------------------------tfs-client-java的版本有:release-2.0.0/tfs-with-large-2.1.1/tfs-client-java-2.1.3/release-2.2.2/release-2.2.3/release-2.2.4/release-2.2.6/最终我选用了tf…

Nutanix公布财报数据 准备IPO前表现抢眼

Nutanix已经向美国证券交易委员会提交了修订的S-1 IPO文件,其中的数据显示,Nutanix凭借着强力增长的业务为IPO做好了准备。 Nutanix公布财报数据 准备IPO前表现抢眼 Nutanix CEO Dheeraj Pandey Nutanix最新的财报数据无论从环比和同比收入增长方面看起来…

DevExpress WinForms使用教程:图表控件 - 内置深入查询

【DevExpress WinForms v18.2下载】在最新发布的DevExpress WinForms v18.2中,DevExpress WinForms和ASP.NET图表控件引入嵌套系列模板的概念,是您能在不编写自定义代码的情况下深入查询图表。 Chart控件的嵌入式痕迹导航元素显示导航层次结构中各个数据…

serverless 构建_使用Serverless,StepFunctions和StackStorm Exchange构建社区注册应用程序-Episode…...

serverless 构建by Dmitri Zimine由Dmitri Zimine 使用Serverless,StepFunctions和StackStorm Exchange构建社区注册应用程序-第3集 (Building a community sign-up app with Serverless, StepFunctions, and StackStorm Exchange — Episode 3) Build a real-worl…

AlfaLaval公司采用低速通风技术冷却数据中心

日前,瑞典热交换专家AlfaLaval公司推出了遵循低速通风原则的一系列数据中心冷却解决方案,其方案需要大量的风扇,而使空气以相当慢的速度流动,取得了与计算机机房空调(CRAC)一样有效的制冷效果。 该公司表示…

java pdf 首页 缩略图_Java中将上传的文件首页生成缩略图(先将上传的文件转成pdf,然后将pdf转成jpg)...

1、首先将上传的非jpg,pdf格式的文件转成pdf,这个是采用OpenOffice进行转的,具体代码如下:private void officeToPdf(){OpenOfficeConnection connection new SocketOpenOfficeConnection(8100);try {connection.connect();} cat…

1.2 如何在visual studio 中建立C#程序

这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大牛请自动跳过。 首先双击visual studio 2015的…

javascript在html中的延迟与异步

1.相同点:延迟与异步都会同时加载script 2.不同点:延迟是script加载完成后,待HTML执行完毕后,才会接着执行script; 异步是script加载完成后,接着就执行该程序,HTML等到script完全执行完毕后&…

三星全速进军物联网 所有产品都将内置互联功能

韩媒报道,近日消息传出,三星电子的家电部门,当前生产的产品都将内建Wi-Fi。相关高层表示,此种做法可替更先进的功能预作准备,因为家电寿命较长,至少可用五年。目前为止,三星家电只有部分具备Wi-…

2d手机游戏开发_我的手机游戏如何在2周内获得365K应用商店下载(以及为什么我退出独立游戏开发公司…...

2d手机游戏开发by William Kwan关冠伟 我的手机游戏如何在2周内获得365K应用商店下载(以及为什么以后我退出独立游戏开发者) (How My Mobile Game Got 365K App Store Downloads in 2 Weeks (And Why I Quit Indie Game Dev Afterwards)) I’m not a successful game develope…

Python ValueError: IO operation on closed file

ValueError IO operation on closed file表示处理了已经被关闭的数据,在python 中 with语句的上下文会帮助处理,也就是说,当python的处理代码不对齐的时候会出现这种情况。例子如下: header那一行,突出,也就…

java面向字符的输入流_详细解读Java编程中面向字符的输入流

字符流是针对字符数据的特点进行过优化的,因而提供一些面向字符的有用特性,字符流的源或目标通常是文本文件。 Reader和Writer是java.io包中所有字符流的父类。由于它们都是抽象类,所以应使用它们的子类来创建实体对象,利用对象来…

任务信号量

在实际任务间的通信中,一个或多个任务发送一个信号量或者消息给另一个任务是比常见的,而一个任务给多个任务发送信号量和消息相对比较少。前面所讲的信号量和消息队列均是单独的内核对象,是独立于任务存在的。这两章要讲述的任务信号量和任务…

域名服务商GoDaddy第四季度扭亏为盈

2月18日消息,据财经网站MarketWatch报道,域名服务提供商GoDaddy周三公布了第四季度财报。公司期内利润与营收均好于预期,给出的营收指导亦符合预测水平。 财报显示,第四季度中GoDaddy营收同比增长14%,为4.254亿美元&am…

易于使用的人工智能_需求分析:如何使用这种易于启动的方法+一个案例研究...

易于使用的人工智能by Turgay elik由Turgayelik 需求分析:如何使用这种易于启动的方法一个案例研究 (Requirement Analysis: how to use this startup-friendly approach a case study) In our previous blog posts, we explained why we decided to develop the …

java writeboolean_Java DataOutputStream writeBoolean()方法(带示例)

DataOutputStream类writeBoolean()方法writeBoolean()方法在java.io包中可用。writeBoolean()方法用于将给定的布尔字节写入基本输出流,因此成功执行后写入的变量计数器为1。writeBoolean()方法是一种非静态方法,只能通过类对象访问,如果尝试…

【BZOJ4300】—绝世好题(二进制dp)

传送门 考虑到只需要bi&bi−1̸0b_i\&b_{i-1} \not0bi​&bi−1​̸​0 由于&\&&,我们考虑二进制下只需要一位不为0就可以了f[i]f[i]f[i]表示当前数下,第iii位不为0的最优长度 那就是需要枚举当前这个数所有位就…

爱立信与中国联通成功完成国内首个LTE三载波聚合大规模部署测试

近日,爱立信与中国联通网络技术研究院、联通四川省公司、联通成都市分公司、Qualcomm Incorporated子公司Qualcomm Technologies, Inc.合作成功实现了国内首个三载波聚合大规模部署和运行测试,下行单用户峰值速率达到375Mbps。该项目充分验证了载波聚合大…