1
00:00:03,560 --> 00:00:07,745
In this exercise, we will learn more about loopback.

2
00:00:07,745 --> 00:00:13,635
Loopback itself started its journey with a small company called StrongLoop.

3
00:00:13,635 --> 00:00:20,775
This company was thereafter acquired by IBM and Loopback has become part of

4
00:00:20,775 --> 00:00:28,725
the IBM Bluemix platform but loopback is still available as an open source platform.

5
00:00:28,725 --> 00:00:36,205
So, you can install and make use of Loopback to build your backend as a service,

6
00:00:36,205 --> 00:00:43,850
as we will demonstrate in this exercise and the remaining two exercises of this lesson.

7
00:00:43,850 --> 00:00:51,230
As I mentioned, Loopback itself is available as an NPM module.

8
00:00:51,230 --> 00:00:56,445
So we're going to be installing Loopback command line interface first.

9
00:00:56,445 --> 00:01:00,810
So to install the Loopback command line interface,

10
00:01:00,810 --> 00:01:02,875
CLI, at the prompt,

11
00:01:02,875 --> 00:01:13,030
type npminstall loopback CLI minus minus save.

12
00:01:13,030 --> 00:01:18,170
Now, if you are doing it on a Mac or a Linux machine,

13
00:01:18,170 --> 00:01:22,025
make sure to use sudo in front of it and then

14
00:01:22,025 --> 00:01:26,855
let the Loopback command line interface get installed.

15
00:01:26,855 --> 00:01:29,024
Once that gets installed,

16
00:01:29,024 --> 00:01:31,195
you can get started with the exercise.

17
00:01:31,195 --> 00:01:32,510
As you can see, right now,

18
00:01:32,510 --> 00:01:37,380
I'm using the Loopback CLI version 4.2.0 in this course.

19
00:01:37,380 --> 00:01:45,380
Now, to scaffold out a loopback framework based application is very straightforward.

20
00:01:45,380 --> 00:01:47,725
Once you install the command line interface,

21
00:01:47,725 --> 00:01:50,690
you may need to restart your terminal in order for

22
00:01:50,690 --> 00:01:53,970
the command to be available at your prompt.

23
00:01:53,970 --> 00:02:00,080
So make sure to restart your terminal or your command window.

24
00:02:00,080 --> 00:02:01,810
Then, at the prompt,

25
00:02:01,810 --> 00:02:06,560
you should have this command called lb available for us.

26
00:02:06,560 --> 00:02:08,695
LB standing for Loopback.

27
00:02:08,695 --> 00:02:14,560
So. To scaffold out a Loopback application at the terminal prompt,

28
00:02:14,560 --> 00:02:18,850
type lb conFusionServer-Loopback,

29
00:02:21,490 --> 00:02:25,650
at a convenient location on your computer.

30
00:02:25,650 --> 00:02:27,845
When we do that,

31
00:02:27,845 --> 00:02:34,530
the Loopback CLI will end up creating the application for us,

32
00:02:34,530 --> 00:02:36,950
and for the application name,

33
00:02:36,950 --> 00:02:39,585
I'm just going to accept the same name,

34
00:02:39,585 --> 00:02:45,389
and then I'll ask it to create the project in that specific directory,

35
00:02:45,389 --> 00:02:53,090
and then we'll select the 3.X version of Loopback for us to scaffold out an application.

36
00:02:53,090 --> 00:02:55,880
Then, for the API server,

37
00:02:55,880 --> 00:03:02,070
we will select the API server as the option for this.

38
00:03:02,070 --> 00:03:07,170
Then wait for Loopback to scaffold out our application.

39
00:03:12,230 --> 00:03:14,355
Once that is done,

40
00:03:14,355 --> 00:03:16,110
the next step is, of course,

41
00:03:16,110 --> 00:03:24,605
to go to the conFusionServer-Loopback folder.

42
00:03:24,605 --> 00:03:30,415
Then, we will start configuring our server.

43
00:03:30,415 --> 00:03:35,870
Now, Loopback supports data through data modeling.

44
00:03:35,870 --> 00:03:38,060
So we can define data models.

45
00:03:38,060 --> 00:03:42,030
To do this, the Loopback command line interface provides us with

46
00:03:42,030 --> 00:03:46,975
the various commands for developing the data models.

47
00:03:46,975 --> 00:03:50,960
So, in this case, I'm going to develop one example

48
00:03:50,960 --> 00:03:55,130
of a data of a model for my dishes here.

49
00:03:55,130 --> 00:03:56,620
So, at the prompt,

50
00:03:56,620 --> 00:03:59,560
let me type lb model.

51
00:03:59,560 --> 00:04:01,905
Then, as we go through the steps,

52
00:04:01,905 --> 00:04:04,600
I'll explain to you what

53
00:04:04,600 --> 00:04:12,860
the Loopback command line interface and how it creates the data model for the dishes.

54
00:04:12,860 --> 00:04:14,840
So, in this case,

55
00:04:14,840 --> 00:04:16,720
we'll just type lb model,

56
00:04:16,720 --> 00:04:21,460
and then we'll be prompted with the name for the model,

57
00:04:21,460 --> 00:04:24,165
and then I will type the name of the model as dishes.

58
00:04:24,165 --> 00:04:28,059
As you recall, we have been using dishes, promotions,

59
00:04:28,059 --> 00:04:34,339
and leaders as the three major data sources for our application.

60
00:04:34,339 --> 00:04:39,420
So, we'll create our first model named dishes, and this model,

61
00:04:39,420 --> 00:04:46,580
at this moment, will be stored in the in-memory database of our computer for the moment.

62
00:04:46,580 --> 00:04:47,840
In the next exercise,

63
00:04:47,840 --> 00:04:52,940
we will add in MongoDB as the backend persistence for our data.

64
00:04:52,940 --> 00:04:58,110
Now, this dishes model will be a persistent model.

65
00:04:58,110 --> 00:05:03,890
So, we'll select the persistent model as the option for the model,

66
00:05:03,890 --> 00:05:09,350
and then we'll say expose the dishes via a REST API,

67
00:05:09,350 --> 00:05:13,060
and then, by default,

68
00:05:13,060 --> 00:05:17,880
it'll expose it slash dishes endpoint and that'll be perfectly fine for us.

69
00:05:17,880 --> 00:05:19,680
So, we can just say yes.

70
00:05:19,680 --> 00:05:23,865
Then, the model will be created in a folder named common.

71
00:05:23,865 --> 00:05:27,950
Now, once the Loopback application is scaffolded out,

72
00:05:27,950 --> 00:05:34,020
we'll see where all this data is stored in the folder itself and in the subfolder,

73
00:05:34,020 --> 00:05:38,295
and this model will be created at the common sub folder.

74
00:05:38,295 --> 00:05:40,580
So, we'll select that.

75
00:05:40,580 --> 00:05:42,110
Then, at this point,

76
00:05:42,110 --> 00:05:45,860
it will prompt us to select all the properties that we want to

77
00:05:45,860 --> 00:05:50,750
specify for the dishes model.

78
00:05:50,750 --> 00:05:54,710
So, for this, we will specify

79
00:05:54,710 --> 00:06:00,880
the name as the first property and then we'll specify the property type as string.

80
00:06:00,880 --> 00:06:02,380
So, as you see,

81
00:06:02,380 --> 00:06:09,060
it is prompting us and obtaining information about the various parts of our model,

82
00:06:09,060 --> 00:06:12,425
and then helps us to configure the model for us.

83
00:06:12,425 --> 00:06:18,355
So, this is of the string type and this is required and the default value will be empty.

84
00:06:18,355 --> 00:06:22,230
So. That'll be name, then description,

85
00:06:22,370 --> 00:06:29,270
which will be of the string type required and the default value is empty.

86
00:06:29,270 --> 00:06:36,570
Then, category, which will be of the string type required and the default value is empty.

87
00:06:36,570 --> 00:06:40,930
Image which will be of the string type,

88
00:06:40,930 --> 00:06:43,934
and this is required,

89
00:06:43,934 --> 00:06:47,550
and the default value will be empty.

90
00:06:47,950 --> 00:06:52,885
Label, which will be also of the string type.

91
00:06:52,885 --> 00:06:58,310
This is required but the default value we'll specify as an empty string.

92
00:06:58,310 --> 00:07:01,360
Then, the last one would be price,

93
00:07:01,360 --> 00:07:05,870
which will be required and the default value is zero.

94
00:07:05,870 --> 00:07:10,115
Now, you see why I have configured all these various properties.

95
00:07:10,115 --> 00:07:19,070
All these are part of the dishes object that we use on our server side.

96
00:07:19,070 --> 00:07:22,580
Now, once you have completed entering all the properties,

97
00:07:22,580 --> 00:07:23,760
and which we have,

98
00:07:23,760 --> 00:07:25,565
then at that point the,

99
00:07:25,565 --> 00:07:27,080
last one, of course,

100
00:07:27,080 --> 00:07:31,270
is to add the featured property,

101
00:07:31,270 --> 00:07:34,265
which will be of the type Boolean,

102
00:07:34,265 --> 00:07:36,665
and this is required,

103
00:07:36,665 --> 00:07:39,440
and the default value is false.

104
00:07:39,440 --> 00:07:41,560
That's it. Once we have entered all the properties,

105
00:07:41,560 --> 00:07:47,280
you just hit when it asks for the property name if you type return,

106
00:07:47,280 --> 00:07:49,960
then the model will be scaffolded out.

107
00:07:49,960 --> 00:07:55,050
Now, similarly, we will create the models for promotions and leaders.

108
00:07:55,050 --> 00:08:04,625
So, for promotions, let me go ahead and say lb model and the model name is promotions.

109
00:08:04,625 --> 00:08:08,080
The survey in the DB this is the persister model and we'll

110
00:08:08,080 --> 00:08:12,625
expose this by the API, and the common.

111
00:08:12,625 --> 00:08:23,040
For the promotions, the properties are name of the type string require no default value,

112
00:08:23,040 --> 00:08:25,949
image of the type string,

113
00:08:25,949 --> 00:08:29,535
require no default value,

114
00:08:29,535 --> 00:08:34,030
label of the type string,

115
00:08:34,030 --> 00:08:39,085
required the default value is empty.

116
00:08:39,085 --> 00:08:43,220
Price, string type, required,

117
00:08:43,220 --> 00:08:45,170
the default value is zero,

118
00:08:45,170 --> 00:08:52,150
and then description required and the default value is empty.

119
00:08:52,150 --> 00:08:54,550
That's it for promotions.

120
00:08:54,550 --> 00:08:57,460
Let me now create the leaders model.

121
00:08:57,460 --> 00:09:07,090
So, we'll say LB model and we'll say leaders,

122
00:09:07,090 --> 00:09:10,750
and this would be in the database PersistedModel,

123
00:09:10,750 --> 00:09:14,290
Expose leaders via REST API, custom, common.

124
00:09:14,290 --> 00:09:16,610
So, as you can see creating a model is very,

125
00:09:16,610 --> 00:09:20,175
very straightforward using the LoopBack CLI,

126
00:09:20,175 --> 00:09:24,260
and for the leader the properties of name,

127
00:09:24,260 --> 00:09:27,830
of the type string, required,

128
00:09:27,830 --> 00:09:32,759
default is empty, image of the type string,

129
00:09:32,759 --> 00:09:36,634
required default value is empty,

130
00:09:36,634 --> 00:09:40,190
designation of the type string,

131
00:09:40,190 --> 00:09:44,130
required default value is empty,

132
00:09:44,130 --> 00:09:46,790
abbreviation of the type string,

133
00:09:46,790 --> 00:09:54,425
required an empty, and the last one is description of the type string, required an empty.

134
00:09:54,425 --> 00:10:00,965
That's it. So, we have configured three different models here: promotions,

135
00:10:00,965 --> 00:10:04,280
leaders, and the dishes.

136
00:10:04,280 --> 00:10:09,175
That's it. Now, our LoopBack server would have been configured.

137
00:10:09,175 --> 00:10:12,765
We'll take a look at this in our editor,

138
00:10:12,765 --> 00:10:19,715
and also we'll start up this LoopBack server and then look at what it serves up for us.

139
00:10:19,715 --> 00:10:21,940
To start the LoopBack server,

140
00:10:21,940 --> 00:10:29,205
at the prompt type NPM start or node space dot.

141
00:10:29,205 --> 00:10:32,280
In either case, the LoopBack server will start up,

142
00:10:32,280 --> 00:10:35,620
and the LoopBack server will be available to us.

143
00:10:35,620 --> 00:10:39,210
At this address, you can also browse

144
00:10:39,210 --> 00:10:43,065
the REST API that is supported by the LoopBack server at this end part.

145
00:10:43,065 --> 00:10:45,360
So, let me copy this first,

146
00:10:45,360 --> 00:10:49,670
and then we'll see what the LoopBack server serves up at this point.

147
00:10:49,670 --> 00:10:51,170
In a browser address bar,

148
00:10:51,170 --> 00:10:55,325
let's type that and then see that our LoopBack server,

149
00:10:55,325 --> 00:10:56,990
if you access it,

150
00:10:56,990 --> 00:11:03,010
will show when it was last started and how long it has been running.

151
00:11:03,010 --> 00:11:11,575
There. Now, let's go to the Explorer of our LoopBack server,

152
00:11:11,575 --> 00:11:13,500
and then you will immediately see that

153
00:11:13,500 --> 00:11:17,785
this LoopBack API Explorer uses something called the Swagger

154
00:11:17,785 --> 00:11:25,185
IO format for showing us all the REST API endpoints.

155
00:11:25,185 --> 00:11:28,989
So, here you see that we have dishes, leaders, promotions,

156
00:11:28,989 --> 00:11:30,740
and LoopBack, by default,

157
00:11:30,740 --> 00:11:33,540
adds a User model in there.

158
00:11:33,540 --> 00:11:37,860
The User model is the standard user model for authentication purposes.

159
00:11:37,860 --> 00:11:41,690
We'll see how to make use of that in the next exercise.

160
00:11:41,690 --> 00:11:43,470
Now, let's click on dishes.

161
00:11:43,470 --> 00:11:48,250
When you click on dishes you'll see that the LoopBack server has automatically configured

162
00:11:48,250 --> 00:11:51,160
that slash dishes endpoint and then exposes

163
00:11:51,160 --> 00:11:55,435
various REST API endpoints automatically for us,

164
00:11:55,435 --> 00:12:01,310
and then the corresponding actions that you can perform on each one of them.

165
00:12:01,310 --> 00:12:06,775
Not only that, with the Swagger IO API,

166
00:12:06,775 --> 00:12:11,610
you automatically also get this documented information here,

167
00:12:11,610 --> 00:12:16,670
which gives you details about how to use that REST API endpoint.

168
00:12:16,670 --> 00:12:19,145
So, here when it says get slash dishes,

169
00:12:19,145 --> 00:12:22,185
it says what is the typical.

170
00:12:22,185 --> 00:12:26,490
This is the model structure used there,

171
00:12:26,490 --> 00:12:31,910
and then "let's try this out" and then you will see that it will

172
00:12:31,910 --> 00:12:34,440
return us with an empty body because at

173
00:12:34,440 --> 00:12:37,930
this moment there is nothing in my in-memory database.

174
00:12:37,930 --> 00:12:42,910
So, recall that we configured that with DB in-memory,

175
00:12:42,910 --> 00:12:43,910
which means that this is

176
00:12:43,910 --> 00:12:46,990
the in-memory database and right now it doesn't contain anything.

177
00:12:46,990 --> 00:12:50,400
But in the next exercise we will see how we will connect this to

178
00:12:50,400 --> 00:12:56,610
our MongoDB server and be able to retrieve all the information from our MongoDB server.

179
00:12:56,610 --> 00:13:00,180
Similarly, if you want to post a dish,

180
00:13:00,180 --> 00:13:04,165
then all that you need to do is use this endpoint 'POST dishes',

181
00:13:04,165 --> 00:13:08,280
and it also gives you exactly the structure

182
00:13:08,280 --> 00:13:12,435
of the data that you need to use for supplying information to the model.

183
00:13:12,435 --> 00:13:15,450
So, you can just simply copy that and then paste it into the data,

184
00:13:15,450 --> 00:13:19,120
and then fill in each one of these values here,

185
00:13:19,120 --> 00:13:21,760
and then once you do that you can simply, say,

186
00:13:21,760 --> 00:13:26,060
try it out and then that will be posted to the server.

187
00:13:26,060 --> 00:13:28,935
So, you can perform all the get, put, post,

188
00:13:28,935 --> 00:13:35,575
and delete operations directly by using this Explorer here

189
00:13:35,575 --> 00:13:38,970
to interact with your REST API endpoints

190
00:13:38,970 --> 00:13:42,570
that is automatically constructed for you by LoopBack.

191
00:13:42,570 --> 00:13:45,340
So with this, we complete this exercise.

192
00:13:45,340 --> 00:13:49,805
So, in this exercise we have seen how we can very quickly

193
00:13:49,805 --> 00:13:55,805
configure a REST API server using the LoopBack framework.

194
00:13:55,805 --> 00:13:57,480
In the next couple of exercises,

195
00:13:57,480 --> 00:14:02,030
we will explore more details about the LoopBack framework.

196
00:14:02,030 --> 00:14:06,755
Now, this may be a good time for you to do a Git initialization of

197
00:14:06,755 --> 00:14:10,230
your LoopBack folder and then do a Git

198
00:14:10,230 --> 00:14:15,570
commit with 'Getting Started' LoopBack as the message.