1
00:00:00,000 --> 00:00:04,528
[MUSIC]

2
00:00:04,528 --> 00:00:09,392
Now that we have understood REST API and
express support for

3
00:00:09,392 --> 00:00:13,293
REST API,
let's move on to the next exercise.

4
00:00:13,293 --> 00:00:17,168
Where we will look at how we
will develop the REST API

5
00:00:17,168 --> 00:00:19,972
using the support given by express.

6
00:00:19,972 --> 00:00:23,110
And also, the use of express router,

7
00:00:23,110 --> 00:00:28,110
enabling us to organize our code
in a way that is more suitable

8
00:00:28,110 --> 00:00:33,231
when you need to support a large
number of REST API endpoints.

9
00:00:35,080 --> 00:00:39,965
To get started,
continuing with the node-express folder

10
00:00:39,965 --> 00:00:44,565
we have been working on
the express-based web server.

11
00:00:44,565 --> 00:00:50,546
At the prompt, let's install body-parser,

12
00:00:50,546 --> 00:00:57,729
so to do that,
type npm install body-parser --save.

13
00:00:57,729 --> 00:01:02,674
And we are using the 1.18.3 version of

14
00:01:02,674 --> 00:01:06,689
body-parser in this course here.

15
00:01:06,689 --> 00:01:12,151
Now once we have installed body-parser,
then go into our code.

16
00:01:12,151 --> 00:01:16,476
In the index.js5,

17
00:01:16,476 --> 00:01:22,723
let me require bodyParser, so

18
00:01:22,723 --> 00:01:28,492
we'll say const bodyParser

19
00:01:28,492 --> 00:01:33,553
require body-parser.

20
00:01:33,553 --> 00:01:39,891
And then whenever you
need to use a middleware,

21
00:01:39,891 --> 00:01:46,558
you'll say, app.use(bodyParser.json).

22
00:01:46,558 --> 00:01:51,782
So this allows us to parse
the body of the request message,

23
00:01:51,782 --> 00:01:54,989
which is formatted in JSON format.

24
00:01:57,466 --> 00:02:03,374
Once we have completed that,
then let's start building

25
00:02:03,374 --> 00:02:08,678
up The REST API support for
the /dishes endpoint.

26
00:02:08,678 --> 00:02:12,797
Using the app.all, app.get, put, post, and

27
00:02:12,797 --> 00:02:16,339
delete methods are supported by express.

28
00:02:16,339 --> 00:02:21,692
So to do that, let me start out by saying

29
00:02:21,692 --> 00:02:26,725
app.all, and the first parameters

30
00:02:26,725 --> 00:02:31,937
that app.all takes is the endpoint.

31
00:02:31,937 --> 00:02:36,360
So in this case,
I am specifying the endpoint of /dishes.

32
00:02:36,360 --> 00:02:43,560
And then the second parameter
is the callback function,

33
00:02:43,560 --> 00:02:49,564
req, res, next, the three parameters here.

34
00:02:49,564 --> 00:02:56,518
And inside this callback function, we're
going to handle the incoming request.

35
00:02:56,518 --> 00:03:01,740
So we will say, when a request comes in,
for all the requests.

36
00:03:01,740 --> 00:03:06,993
So when we say app.all,
no matter which method is invoked,

37
00:03:06,993 --> 00:03:12,864
get, put, post, or delete, for
the /dishes REST API endpoint,

38
00:03:12,864 --> 00:03:17,190
this code will be executed
first by default here.

39
00:03:17,190 --> 00:03:26,376
So we'll say res StatusCode is 200 and

40
00:03:26,376 --> 00:03:31,889
then res.setHeader and

41
00:03:31,889 --> 00:03:39,248
let's say Content-Type text.

42
00:03:39,248 --> 00:03:44,290
We'll send plain text now,
instead of HTML text.

43
00:03:44,290 --> 00:03:47,575
This should be enough for
REST API endpoints.

44
00:03:47,575 --> 00:03:52,420
Later on, we will send the data
in the form of JSON once we

45
00:03:52,420 --> 00:03:54,640
are able to retrieve
the data from the database.

46
00:03:54,640 --> 00:03:57,340
So that will come in one
of the later exercises.

47
00:03:57,340 --> 00:04:04,389
For the moment, we'll simply send
plaintext replies back to the client.

48
00:04:04,389 --> 00:04:10,785
Now after these two,
the middleware here is not completed yet.

49
00:04:10,785 --> 00:04:17,590
So we will call the next function here, so
the next as you see refers to this next.

50
00:04:17,590 --> 00:04:24,954
So when you call next, what it means
is that it'll continue on to look for

51
00:04:24,954 --> 00:04:29,665
additional specifications down below here,

52
00:04:29,665 --> 00:04:33,790
which will match this dishes endpoint.

53
00:04:33,790 --> 00:04:38,282
So this would be done for
all the requests, get, put, post, and

54
00:04:38,282 --> 00:04:42,712
delete, on the dishes, and
it'll continue on to the next one.

55
00:04:42,712 --> 00:04:48,436
So here, suppose we get a get request

56
00:04:48,436 --> 00:04:53,792
on dishes, which means that now,

57
00:04:53,792 --> 00:04:58,961
if we get a get request on dishes,

58
00:04:58,961 --> 00:05:04,133
first this will be executed, and

59
00:05:04,133 --> 00:05:09,304
then the req and res will be passed

60
00:05:09,304 --> 00:05:13,760
on to this second call here.

61
00:05:13,760 --> 00:05:17,481
So in this case,
I will no longer need the next,

62
00:05:17,481 --> 00:05:21,335
because I'm not going
to call further down.

63
00:05:21,335 --> 00:05:26,444
I'll complete the handling right
within this method here itself.

64
00:05:26,444 --> 00:05:31,337
So if we modify the req or
res inside this code,

65
00:05:31,337 --> 00:05:36,607
then when you call the next,
that modified object

66
00:05:36,607 --> 00:05:41,130
will be passed in as
the parameter to this.

67
00:05:41,130 --> 00:05:45,698
So if you have issued a get request
on dishes, so this is what will be

68
00:05:45,698 --> 00:05:50,202
executed right after this, so
this next will cause it to pass on.

69
00:05:50,202 --> 00:05:55,836
And then here, you notice that
we modified the res object here.

70
00:05:55,836 --> 00:06:02,770
And the modification will carry in as
the input parameter to this function here.

71
00:06:03,940 --> 00:06:09,380
So inside this get function,
we can say, for example, at the moment,

72
00:06:09,380 --> 00:06:15,197
I'm simply going to send back
a simple message, we'll say,

73
00:06:15,197 --> 00:06:20,170
will send all the dishes to you.

74
00:06:20,170 --> 00:06:25,650
Later on, when we retrieve the dishes
information from the database

75
00:06:25,650 --> 00:06:31,036
after we learn about MongoDB,
then this function will be returning

76
00:06:31,036 --> 00:06:36,534
that JSON data back to the client,
since it's in a get request here.

77
00:06:36,534 --> 00:06:40,566
Now in the previous course,
we had used the JSON server, and

78
00:06:40,566 --> 00:06:44,457
the JSON server was already
providing all of these for us.

79
00:06:44,457 --> 00:06:46,965
Now you are seeing how you would construct

80
00:06:46,965 --> 00:06:50,268
a real server that will
process the incoming request.

81
00:06:50,268 --> 00:06:53,139
And then generate
an appropriate response and

82
00:06:53,139 --> 00:06:57,857
send it back to you in response to
the request received from the client side.

83
00:06:57,857 --> 00:07:02,611
And this is where the user express,
as we see here, is useful.

84
00:07:02,611 --> 00:07:06,971
So we have handled get,

85
00:07:06,971 --> 00:07:14,387
let's handle post also on dishes here.

86
00:07:16,120 --> 00:07:21,064
And again,
the parameters would be (req, res,

87
00:07:21,064 --> 00:07:26,722
next), and that function that
is invoked will be here.

88
00:07:26,722 --> 00:07:31,606
Now, when the get request is received,
because here, you're calling rest.end.

89
00:07:31,606 --> 00:07:36,982
So that end is the handling of the get
request and doesn't trigger the reply

90
00:07:36,982 --> 00:07:42,130
to be sent back, or response to be
sent back to the client at this point.

91
00:07:42,130 --> 00:07:47,050
Now, if you get a post request on dishes,
again, this whole code will be executed.

92
00:07:47,050 --> 00:07:52,859
And then, because of this next, that
will drop into this function call here.

93
00:07:52,859 --> 00:07:57,000
And then the code here, will be executed,
so when you receive a post

94
00:07:59,610 --> 00:08:04,820
The post request coming in from
the server will carry some information

95
00:08:04,820 --> 00:08:08,710
in the body of the message
in the form of JSON data.

96
00:08:08,710 --> 00:08:13,650
They'll see what the form
of the JSON data is when we

97
00:08:13,650 --> 00:08:18,910
look later on when we test
end point using post net.

98
00:08:18,910 --> 00:08:24,700
I'll show you what the body of
the post request message will contain.

99
00:08:24,700 --> 00:08:27,710
But at this point,
what I'm going to do is,

100
00:08:27,710 --> 00:08:33,420
I will extract the information
from the body.

101
00:08:33,420 --> 00:08:38,545
And so here when we use the body parser,
what happens is that for

102
00:08:38,545 --> 00:08:44,541
the incoming request, the body of
the incoming request will be parsed and

103
00:08:44,541 --> 00:08:48,237
then added into the req
object as req.body.

104
00:08:48,237 --> 00:08:52,843
So the req.body will give you access
to whatever is inside that body of

105
00:08:52,843 --> 00:08:54,050
the message.

106
00:08:54,050 --> 00:08:58,642
So, when I send the request,

107
00:08:58,642 --> 00:09:04,339
I will add information to the request

108
00:09:04,339 --> 00:09:10,034
body in the form of a JSON string which

109
00:09:10,034 --> 00:09:15,570
contains a name and a description.

110
00:09:15,570 --> 00:09:20,081
So I'm going to extract these
two piece of information and

111
00:09:20,081 --> 00:09:26,080
then print them out and then send them
back to the client in the reply message.

112
00:09:26,080 --> 00:09:32,840
So I can say, req.body.and then name.

113
00:09:32,840 --> 00:09:38,298
And so the expectation is that when
the client sends the post message,

114
00:09:38,298 --> 00:09:42,183
the post message body will
contain a JSON string,

115
00:09:42,183 --> 00:09:47,108
which will also contains the name
property in the JSON string.

116
00:09:47,108 --> 00:09:52,646
So req.body.name plus and

117
00:09:52,646 --> 00:09:57,952
this is where I will include

118
00:09:57,952 --> 00:10:06,970
with details: + req.body.description.

119
00:10:06,970 --> 00:10:11,654
So whatever the JSON string
contains in the req.body, but

120
00:10:11,654 --> 00:10:15,963
JSON string will be parsed
into a JavaScript object and

121
00:10:15,963 --> 00:10:19,909
added in to the request
object as a property body.

122
00:10:19,909 --> 00:10:22,971
The JavaScript object points to whatever

123
00:10:22,971 --> 00:10:27,440
came in as the JSON string in
the body of the request message.

124
00:10:27,440 --> 00:10:31,750
So that is why I am able to parse the name
property, the description property,,

125
00:10:31,750 --> 00:10:38,000
you can have the price property,
the image property, and all of that.

126
00:10:38,000 --> 00:10:42,260
So if you've taken the previous course,
you've seen the structure of a dish

127
00:10:42,260 --> 00:10:48,850
object, in the form of a JSON string or
the JavaScript object there.

128
00:10:48,850 --> 00:10:52,920
So, the name and
description sounds familiar to you.

129
00:10:52,920 --> 00:10:55,400
So that exactly what I am using here.

130
00:10:55,400 --> 00:11:00,823
Right now I'm only extracting the name and
the description from the body and

131
00:11:00,823 --> 00:11:04,220
then sending it back to
the client in this body.

132
00:11:04,220 --> 00:11:09,460
So I'm just constructing this reply
message here using the information

133
00:11:09,460 --> 00:11:13,150
from the body of the request message,
and then sending it back to the client.

134
00:11:13,150 --> 00:11:17,830
So that way I can confirm that the server
is receiving whatever I am sending

135
00:11:17,830 --> 00:11:19,930
in the body of the message.

136
00:11:19,930 --> 00:11:21,820
So this is the post request.

137
00:11:23,390 --> 00:11:30,620
Now for put, let me just copy this,
and then paste it here.

138
00:11:30,620 --> 00:11:35,360
And then for put on that dishes endpoint,

139
00:11:35,360 --> 00:11:39,100
because it put on the dishes
endpoint doesn't make sense.

140
00:11:39,100 --> 00:11:44,440
A post means that you're posting
a new dish to the servers.

141
00:11:44,440 --> 00:11:50,992
A put on the dishes end
point doesn't make sense,

142
00:11:50,992 --> 00:11:55,516
so here what I'm going to do is I'm

143
00:11:55,516 --> 00:12:00,664
going to reply back with the message PUT

144
00:12:00,664 --> 00:12:06,660
operation not supported on /dishes.

145
00:12:06,660 --> 00:12:14,790
In addition, I will also,
Include a status code of 403.

146
00:12:14,790 --> 00:12:19,987
403 means their operation not supported.

147
00:12:19,987 --> 00:12:24,319
So if you look back at
the HTTP response codes table,

148
00:12:24,319 --> 00:12:30,600
you will see the corresponding
code 403 what it represents there.

149
00:12:30,600 --> 00:12:34,650
So that is what I am using for post.

150
00:12:34,650 --> 00:12:39,879
For delete, I'm going to copy

151
00:12:39,879 --> 00:12:44,777
this one and for delete I'm going to,

152
00:12:47,537 --> 00:12:55,960
Simply parse this, and then we'll say,
Deleting all the dishes.

153
00:12:55,960 --> 00:13:02,200
So when you send a delete request
on that /dishes endpoint.

154
00:13:02,200 --> 00:13:04,900
It is interpreted to mean that

155
00:13:04,900 --> 00:13:08,960
the client wants to delete all the dishes
information on the server side.

156
00:13:08,960 --> 00:13:11,300
Now, this is a dangerous operation, so

157
00:13:11,300 --> 00:13:15,540
you make sure that you don't
allow partner users to do.

158
00:13:15,540 --> 00:13:17,750
So later on, when we study authentication,

159
00:13:17,750 --> 00:13:23,220
we'll see how we can res check this
operation to only privileged users.

160
00:13:23,220 --> 00:13:28,620
But again, as you see, this is a dangerous
operation, so keep in mind about that.

161
00:13:28,620 --> 00:13:33,340
So now you see that in the dishes
endpoint, we have get, put, post,

162
00:13:33,340 --> 00:13:35,130
and delete support.

163
00:13:35,130 --> 00:13:41,520
Let's also support that on
the dishes/:dishId endpoint.

164
00:13:41,520 --> 00:13:46,280
So I'm going to copy the get, put,
post, and delete methods from here,

165
00:13:48,530 --> 00:13:54,175
and then calls all of them
also to be supported on

166
00:13:54,175 --> 00:14:00,360
dishes/:dishId.

167
00:14:00,360 --> 00:14:06,913
So this is for the get,
and we copy that and

168
00:14:06,913 --> 00:14:11,480
post, put, and delete.

169
00:14:11,480 --> 00:14:13,330
How do we handle each of these?

170
00:14:13,330 --> 00:14:19,460
So for get,
if I get a request on dishes/dishId,

171
00:14:19,460 --> 00:14:22,750
what I would like to do is to
extract this parameter and

172
00:14:22,750 --> 00:14:27,000
then send it back in the request,
in the reply message.

173
00:14:27,000 --> 00:14:29,725
So we'll say, we'll send,

174
00:14:39,018 --> 00:14:41,625
Details of the dish.

175
00:14:44,584 --> 00:14:46,910
Now which dish?

176
00:14:46,910 --> 00:14:51,570
This information is in the parameter.

177
00:14:51,570 --> 00:14:55,335
So this parameter value can retrieve from

178
00:14:55,335 --> 00:15:02,300
req.params.dishId.

179
00:15:02,300 --> 00:15:07,510
So this dishId, as you see, the name that
you use here should match this value here.

180
00:15:07,510 --> 00:15:09,370
So if you just simply see ID here,

181
00:15:09,370 --> 00:15:13,220
this should also be corresponding
the given sId here.

182
00:15:13,220 --> 00:15:17,920
So the name itself doesn't mean anything,
except that this parameter name and

183
00:15:17,920 --> 00:15:23,400
this should match each other, so that you
can retrieve the information correctly.

184
00:15:25,090 --> 00:15:30,470
So we'll say,
send the dish parameters dishId and

185
00:15:30,470 --> 00:15:37,360
then we'll say to you.

186
00:15:37,360 --> 00:15:43,160
So that way we know that the server
is getting the dish parameter..

187
00:15:43,160 --> 00:15:47,350
So if I say /dishes/23
there several replies

188
00:15:47,350 --> 00:15:52,350
will send the details of
the dish 23 to you and so on.

189
00:15:52,350 --> 00:15:57,632
So we'll see how it works when
we use post to test this Server.

190
00:15:57,632 --> 00:15:59,584
Now for post.

191
00:15:59,584 --> 00:16:03,530
For post,
this method is not going to be supported,

192
00:16:03,530 --> 00:16:07,060
so I'm just going to copy this part.

193
00:16:07,060 --> 00:16:12,205
it doesn't make sense to do
a post on a specific dish ID,

194
00:16:12,205 --> 00:16:16,375
because you're not trying
to add a new dish.

195
00:16:16,375 --> 00:16:21,360
But you want to modify, and modification
is done by using the put operation.

196
00:16:21,360 --> 00:16:28,111
So we'll say,
POST operation not supported on /dishes.

197
00:16:33,331 --> 00:16:36,360
And then I'm going to add in the.

198
00:16:39,750 --> 00:16:43,197
Req.params.dishId.

199
00:16:43,197 --> 00:16:47,535
So this will send back
saying post not supported on

200
00:16:47,535 --> 00:16:50,752
dishes/23 in the reply message.

201
00:16:50,752 --> 00:16:55,926
Now, for PUT, For PUT, we'll say,

202
00:16:59,902 --> 00:17:08,799
res.end and say Will, update the dish.

203
00:17:13,902 --> 00:17:20,978
Req.params.dishId.

204
00:17:28,915 --> 00:17:31,675
Or rather, we'll do it this way.

205
00:17:33,785 --> 00:17:39,141
I'll first write, So

206
00:17:39,141 --> 00:17:45,345
res.write can be used to add
a line to the reply message.

207
00:17:45,345 --> 00:17:50,077
So we'll say updating the dish.

208
00:17:50,077 --> 00:17:58,655
And we'll say req.params.dishId.

209
00:17:58,655 --> 00:18:06,245
And since this is a PUT operation, and
if the body contains the JSON string,

210
00:18:06,245 --> 00:18:10,277
which contains the details of the dish,

211
00:18:10,277 --> 00:18:17,310
I can extract the JSON string because
we are using the body parser.

212
00:18:17,310 --> 00:18:21,649
So we'll say Will

213
00:18:21,649 --> 00:18:26,569
update the dish:.

214
00:18:30,234 --> 00:18:31,924
Which dish?

215
00:18:31,924 --> 00:18:34,822
req.body.name.

216
00:18:38,122 --> 00:18:38,657
Plus.

217
00:18:40,531 --> 00:18:45,701
With details,

218
00:18:45,701 --> 00:18:55,250
req.body.description.

219
00:18:59,320 --> 00:19:08,800
Now, when we update the dish,
I want to add a, New line there.

220
00:19:08,800 --> 00:19:11,411
So I'll say '/n' there.

221
00:19:14,500 --> 00:19:19,257
So when you do a PUT, you are sending back
the information about which dish ID you

222
00:19:19,257 --> 00:19:22,674
are updating, and also,
the details you are updating.

223
00:19:22,674 --> 00:19:26,997
The name and the description,

224
00:19:26,997 --> 00:19:34,318
which should be in the body
of the PUT message there.

225
00:19:34,318 --> 00:19:41,868
For delete, It'll say Deleting dish,
and req.params.dishId.

226
00:19:41,868 --> 00:19:47,970
And so for delete, this is
the operation that will be performed.

227
00:19:47,970 --> 00:19:51,607
So you see that now we have the get,
put, post, and

228
00:19:51,607 --> 00:19:55,838
delete operations on the dishes/dishId,
endpoint, and

229
00:19:55,838 --> 00:20:00,590
also the get, put,
post operations on the /dishes, endpoint.

230
00:20:00,590 --> 00:20:03,765
So let's now save the changes.

231
00:20:03,765 --> 00:20:05,387
And start our server again.

232
00:20:08,245 --> 00:20:10,643
Start the server by saying npm start.

233
00:20:10,643 --> 00:20:15,611
Let's now go to postman and
send a few requests to this server and

234
00:20:15,611 --> 00:20:17,270
see what it returns.

235
00:20:17,270 --> 00:20:22,287
Let me first issue a get to local
host 3,000 and see what it returns.

236
00:20:22,287 --> 00:20:26,832
So when you send a request
to get local host 3,000,

237
00:20:26,832 --> 00:20:30,798
it is still returning
the index start HTML page,

238
00:20:30,798 --> 00:20:35,069
because the static file
handling is still in place.

239
00:20:35,069 --> 00:20:41,564
Now let me send a get request to
localhost:3000/dishes, and this,

240
00:20:41,564 --> 00:20:48,383
as you expect, will send back a reply
saying we'll send all the dishes to you.

241
00:20:48,383 --> 00:20:56,350
Now, let's send a POST request
to localhost:3000/dishes.

242
00:20:56,350 --> 00:21:01,200
So this is where you can change the
various methods that you want to execute.

243
00:21:01,200 --> 00:21:02,880
But when you send a post,

244
00:21:02,880 --> 00:21:08,570
you want to include some details
in the body of the message because

245
00:21:08,570 --> 00:21:12,920
there's somebody's expecting you to send
information in the body of the message.

246
00:21:12,920 --> 00:21:17,967
So click on body here and
click on raw here.

247
00:21:17,967 --> 00:21:23,395
And then, for the text,
you select this to JSON so

248
00:21:23,395 --> 00:21:26,377
use the application JSON.

249
00:21:26,377 --> 00:21:31,081
So the content type would
be application/JSON in

250
00:21:31,081 --> 00:21:34,220
the request that you send.

251
00:21:34,220 --> 00:21:38,658
So since this is a JSON object, so

252
00:21:38,658 --> 00:21:43,103
within braces, I will say name.

253
00:21:45,320 --> 00:21:50,727
I'll just simply type in test,

254
00:21:50,727 --> 00:21:53,939
and description.

255
00:22:01,212 --> 00:22:05,760
Some standard information here, so
you see that this is a JSON string which

256
00:22:05,760 --> 00:22:10,900
contains two properties, name and
description and the corresponding values.

257
00:22:10,900 --> 00:22:13,858
So let's send a post
request to the server.

258
00:22:13,858 --> 00:22:18,294
When you send a post request
to the server, as you see,

259
00:22:18,294 --> 00:22:24,290
in the reply it says will add the dish:
test week details: description.

260
00:22:24,290 --> 00:22:30,368
So as you can see, the two piece of
information are extracted from the body

261
00:22:30,368 --> 00:22:35,772
of that JSON requested [INAUDIBLE] and
then included in the reply.

262
00:22:35,772 --> 00:22:39,459
Let's send a put request to dishes.

263
00:22:39,459 --> 00:22:43,029
When you send a put request to dishes,
as you see,

264
00:22:43,029 --> 00:22:48,654
it says PUT operation not supported on
/dishes, and look at the status here.

265
00:22:48,654 --> 00:22:52,580
It says Status: 403 Forbidden.

266
00:22:52,580 --> 00:22:57,570
So this operation is not
allowed on this endpoint.

267
00:22:57,570 --> 00:23:01,430
Let's do a DELETE operation on the dishes.

268
00:23:01,430 --> 00:23:04,240
When you DELETE operation on the dishes,

269
00:23:04,240 --> 00:23:09,270
then you will notice that it
says deleting all the dishes.

270
00:23:09,270 --> 00:23:11,560
So that's the reply that you get back.

271
00:23:12,920 --> 00:23:15,410
So you see that the get post, put and

272
00:23:15,410 --> 00:23:20,610
delete on the /dishes
endpoint works as you expect.

273
00:23:20,610 --> 00:23:24,507
Now let's do a get on dishes /23.

274
00:23:24,507 --> 00:23:29,017
So 23 here is the parameter,

275
00:23:29,017 --> 00:23:34,710
the ID of the dish that you want to get.

276
00:23:34,710 --> 00:23:38,716
So when you send a request to
that the server will reply,

277
00:23:38,716 --> 00:23:41,878
will send details of the dish: 23 to you.

278
00:23:41,878 --> 00:23:47,421
So you see that the parameter has been
extracted from the request message and

279
00:23:47,421 --> 00:23:50,645
then included in the reply message.

280
00:23:50,645 --> 00:23:54,850
Let's do a post on this.

281
00:23:54,850 --> 00:23:59,580
And as you know, POST is not
allowed on this endpoint, and so

282
00:23:59,580 --> 00:24:03,450
that's why you get a status
403 Forbidden on this.

283
00:24:03,450 --> 00:24:05,728
Let's do a PUT operation on that.

284
00:24:05,728 --> 00:24:10,040
So for the PUT operation, you notice
that the body still contains that.

285
00:24:10,040 --> 00:24:13,950
So in POST, when you type in a value here,

286
00:24:13,950 --> 00:24:18,720
that will be retained and can be included
in other requests that you're sending.

287
00:24:18,720 --> 00:24:25,087
So when you send a PUT request
on the endpoint, dishes/23.

288
00:24:25,087 --> 00:24:28,650
So you notice that it says
updating the dish, 23.

289
00:24:28,650 --> 00:24:31,780
Will update the dish,
the name of the dish,

290
00:24:31,780 --> 00:24:36,200
with details,
the description of the dish here.

291
00:24:36,200 --> 00:24:41,130
And then, let's do a delete
operation on that endpoint and

292
00:24:41,130 --> 00:24:45,140
then it says, d deleting dish: 23.

293
00:24:45,140 --> 00:24:52,210
So those endpoints are all supported
as you see in this example here.

294
00:24:52,210 --> 00:24:58,430
So we have done get PUT, POSRT, and delete
operations on both the /dishes endpoint

295
00:24:58,430 --> 00:25:03,830
and also the /dishes/
the dish id endpoint.

296
00:25:03,830 --> 00:25:08,010
So, you see how we have implemented
the REST API endpoint support for

297
00:25:08,010 --> 00:25:12,730
one set of REST API endpoints.

298
00:25:12,730 --> 00:25:17,405
Now, with this, we complete
the first part of this exercise.

299
00:25:17,405 --> 00:25:22,378
So, here we have seen how we can
use express to construct and

300
00:25:22,378 --> 00:25:26,755
implement the REST API endpoint for
the dishes.

301
00:25:26,755 --> 00:25:27,885
Now, this is a good time for

302
00:25:27,885 --> 00:25:32,895
you to do a git commit with
the message Express Simple REST.

303
00:25:33,895 --> 00:25:42,361
According to the terminal
I will start the, Server.

304
00:25:42,361 --> 00:25:45,845
Check that three of
the items have been changed.

305
00:25:45,845 --> 00:25:52,527
So, git add, git commit -m,

306
00:25:52,527 --> 00:26:01,750
Express Simple REST, And check the set.

307
00:26:01,750 --> 00:26:08,670
So as you can see, using Express you can
easily implement support for REST API.

308
00:26:08,670 --> 00:26:15,300
And as you can see from this list,
you construct the app get, PUT,

309
00:26:15,300 --> 00:26:21,590
POST, and delete methods for
all the REST API endpoints like this.

310
00:26:21,590 --> 00:26:24,601
Now imagine you have
a thousand REST API endpoints,

311
00:26:24,601 --> 00:26:27,347
and you need to construct
something like this.

312
00:26:27,347 --> 00:26:33,344
Your index.js file will explode with so
many different REST API endpoints.

313
00:26:33,344 --> 00:26:37,744
And each one being handled
using its own app.get,

314
00:26:37,744 --> 00:26:41,536
app.put, app.delete, and app.post.

315
00:26:41,536 --> 00:26:46,336
Now Wxpress supports a way of
subdividing this work into multiple,

316
00:26:46,336 --> 00:26:48,442
many Express applications,

317
00:26:48,442 --> 00:26:54,580
which then can be combined together to
form the overall, Express application.

318
00:26:54,580 --> 00:26:58,260
This is where we will make
use of the Express Router

319
00:26:58,260 --> 00:27:01,960
to be able to construct
a mini Express application.

320
00:27:01,960 --> 00:27:06,070
And then, inside a Express Router file,

321
00:27:06,070 --> 00:27:10,040
we will support the REST API endpoint for
one group of REST API parts.

322
00:27:10,040 --> 00:27:13,190
So for example, for
dishes, and dishes dishId,

323
00:27:13,190 --> 00:27:15,598
they can all be supported in one file.

324
00:27:15,598 --> 00:27:20,694
Similarly, in the assignment,
you'll be supporting a REST API

325
00:27:20,694 --> 00:27:25,339
endpoint called promotions and
promotions/:promoId.

326
00:27:25,339 --> 00:27:29,570
And then,
you'll support another REST API and

327
00:27:29,570 --> 00:27:34,241
for leaders/leaders, and /leader:leaderId.

328
00:27:34,241 --> 00:27:39,088
So each of these groups can be
implemented separately as many

329
00:27:39,088 --> 00:27:43,120
Express applications using Express Router.

330
00:27:43,120 --> 00:27:46,230
So that is what I will
illustrate to you for

331
00:27:46,230 --> 00:27:52,150
the dishes endpoint in the next
part of this exercise.

332
00:27:52,150 --> 00:27:57,668
So to do that, we realize that if we
put all the files in one single folder,

333
00:27:57,668 --> 00:28:01,498
then again your folder
structure will look messy.

334
00:28:01,498 --> 00:28:07,731
So my preference is to create
a folder here named routes.

335
00:28:07,731 --> 00:28:12,591
And this routes folder will contain
all the routers that I'm going to

336
00:28:12,591 --> 00:28:15,450
design using the Express Router.

337
00:28:15,450 --> 00:28:19,465
So in the routes folder,
I'm going to create a new file called

338
00:28:19,465 --> 00:28:27,270
dishRouter.js.

339
00:28:27,270 --> 00:28:31,332
And this dishRouter.js file will contain

340
00:28:31,332 --> 00:28:36,972
the implementation of the handling
of REST API endpoint for

341
00:28:36,972 --> 00:28:41,281
/dishes and /dishes:dishId endpoints.

342
00:28:41,281 --> 00:28:44,626
Now how do we make use of Express Router?

343
00:28:44,626 --> 00:28:46,910
Let's see how we can use it.

344
00:28:46,910 --> 00:28:51,050
Now Express Router comes together with
Express, so we don't need to install yet

345
00:28:51,050 --> 00:28:53,130
another Node module.

346
00:28:53,130 --> 00:28:56,970
Instead, we can work with Express
that we have already installed.

347
00:28:56,970 --> 00:29:02,308
So to do that, at that prompt,
type, const express

348
00:29:02,308 --> 00:29:07,200
= require('express');.

349
00:29:07,200 --> 00:29:09,950
So notice that,
since this is a mini-application,

350
00:29:09,950 --> 00:29:15,000
we still require express even
in this dishRouter.js file.

351
00:29:15,000 --> 00:29:18,495
And from your knowledge of Node modules,
once you define a new file,

352
00:29:18,495 --> 00:29:20,251
that becomes its own Node module.

353
00:29:20,251 --> 00:29:24,708
And this Node module can then
be imported in index.js.

354
00:29:24,708 --> 00:29:29,520
So you see the connection already
between how we can restructure our

355
00:29:29,520 --> 00:29:33,415
application into multiple
files using Node modules.

356
00:29:33,415 --> 00:29:39,525
So we'll set require Express,
then we'll say const

357
00:29:39,525 --> 00:29:44,830
bodyParser require ('body-parser').

358
00:29:44,830 --> 00:29:50,310
So we have already installed bodyParser
in the previous steps of the exercise,

359
00:29:50,310 --> 00:29:51,634
so we can use that.

360
00:29:51,634 --> 00:29:55,040
Now to use an Express router,

361
00:29:55,040 --> 00:30:00,636
let me declare const
dishRouter = express.R.

362
00:30:00,636 --> 00:30:05,769
And on express, it supports this
router interface, so we'll simply

363
00:30:05,769 --> 00:30:11,430
say express.Router and this will declare
dishRouter as an Express router.

364
00:30:11,430 --> 00:30:13,951
So in many Express application,

365
00:30:13,951 --> 00:30:18,820
an insight here I can handle that
dishRouter related code here.

366
00:30:19,830 --> 00:30:26,560
So once I declare of this an in Express
router, then I can say dishRouter.

367
00:30:28,050 --> 00:30:33,337
And then on the dishRouter,
it supports a method called route method,

368
00:30:33,337 --> 00:30:36,694
which can take in
an endpoint as a parameter.

369
00:30:36,694 --> 00:30:41,330
So I would simply declare
this endpoint a /.

370
00:30:41,330 --> 00:30:44,620
Now, you're wondering,
shouldn't that be dishes?

371
00:30:44,620 --> 00:30:49,630
You will pretty soon see
that I need to mount this

372
00:30:49,630 --> 00:30:53,140
Express router in my index.js file.

373
00:30:53,140 --> 00:31:01,270
So in my index.js file, I will mount this
express router at the /dishes endpoint.

374
00:31:01,270 --> 00:31:03,300
Mounting of an express router, again,

375
00:31:03,300 --> 00:31:05,900
one more concept that I
want you to understand.

376
00:31:05,900 --> 00:31:08,408
Again, I will illustrate that
to you in a short while.

377
00:31:08,408 --> 00:31:13,368
Now, the dishRouter.route means
that by using this approach,

378
00:31:13,368 --> 00:31:17,526
we are declaring the endpoint
at one single location.

379
00:31:17,526 --> 00:31:21,316
Whereby you can chain all get, PUT, POST,

380
00:31:21,316 --> 00:31:25,219
delete methods already
do this dish router.

381
00:31:25,219 --> 00:31:30,139
Now, when you look at index.js,,
look at the way we implimented this.

382
00:31:30,139 --> 00:31:36,029
So we have app.all and then /dishes,
app.get/dishes and /dishes.

383
00:31:36,029 --> 00:31:40,795
Now, if you had made a mistake,
and their instructing app.post

384
00:31:40,795 --> 00:31:45,580
/dishes instead if you just type /dish,
then what happens?

385
00:31:45,580 --> 00:31:49,050
The POST operation will not
be supported on dishes but

386
00:31:49,050 --> 00:31:51,730
will be supported on /dish endpoint.

387
00:31:53,170 --> 00:31:59,220
To avoid this problem, the express
router supports this route end point.

388
00:31:59,220 --> 00:32:00,090
On the route end point,

389
00:32:00,090 --> 00:32:05,860
you simply specify the end point on
which this router is going to work.

390
00:32:05,860 --> 00:32:10,890
And then, the get put portion delete
method, this simply chained into that.

391
00:32:10,890 --> 00:32:16,300
So it'll be one group of method
implementations all together.

392
00:32:16,300 --> 00:32:20,345
So that is the reason why
they use a Express router.

393
00:32:20,345 --> 00:32:24,185
So it comes with a couple of very useful

394
00:32:24,185 --> 00:32:28,525
support to make sure that your
implementation is correct.

395
00:32:28,525 --> 00:32:32,190
So now that we are going to
do that as the dish router,

396
00:32:32,190 --> 00:32:37,920
what I'm going to do is I'm going
to remove this thing from here.

397
00:32:37,920 --> 00:32:42,990
Now, that dishes IDN point,
I'm going to leave that

398
00:32:42,990 --> 00:32:47,565
to you as an exercise in
your first assignment but

399
00:32:47,565 --> 00:32:54,180
the dishes endpoint, I'm going to
cut this out, all the way up to all.

400
00:32:54,180 --> 00:32:59,350
I'm going to cut this
out from index.js5 and

401
00:32:59,350 --> 00:33:04,580
to move that into the dishRouter here.

402
00:33:04,580 --> 00:33:11,887
Now when I move that into the dishRouter,
I don't need this app.all anymore.

403
00:33:11,887 --> 00:33:18,267
I simply chain that into the route,
so I will simply say .all and

404
00:33:18,267 --> 00:33:24,210
then I no longer need this
end point definition there.

405
00:33:24,210 --> 00:33:24,900
That's it.
So

406
00:33:24,900 --> 00:33:28,390
it'll say .all and
then we'll say req, res, next.

407
00:33:28,390 --> 00:33:31,470
And this ,all is operating
on this particular endpoint

408
00:33:31,470 --> 00:33:33,690
already specified here.

409
00:33:33,690 --> 00:33:37,920
Now, not only that,
we can chain the remaining methods.

410
00:33:37,920 --> 00:33:41,360
So that's why you see that I have
removed the semicolon from here.

411
00:33:41,360 --> 00:33:46,270
I'm going to delete this app and
then attach it on to that.

412
00:33:46,270 --> 00:33:51,600
So does get also gets changed into
the route, and then I can remove

413
00:33:53,140 --> 00:33:57,740
this part, the handling will
remain exactly the same as before.

414
00:33:57,740 --> 00:34:05,520
So similarly,
I will remove the, App there.

415
00:34:05,520 --> 00:34:10,850
And then again, delete that from post.

416
00:34:10,850 --> 00:34:11,865
And the same thing.

417
00:34:17,447 --> 00:34:22,248
For put, And

418
00:34:22,248 --> 00:34:27,360
for delete, Same thing.

419
00:34:27,360 --> 00:34:30,800
So notice that there is no
semicolons here, here or

420
00:34:30,800 --> 00:34:34,670
here, but the last one, the delete,
will have the semicolon in place.

421
00:34:34,670 --> 00:34:40,810
So this group is one single
unit implemented by using

422
00:34:40,810 --> 00:34:45,930
the dish router on this particular router,
and all these are chained together.

423
00:34:47,640 --> 00:34:52,610
And also, of course, with delete,
I need to remove this end point.

424
00:34:52,610 --> 00:34:56,626
That's it, see,
clean structure of the code here.

425
00:34:56,626 --> 00:35:02,100
So that essentially, ends up
implementing the dishRouter, right,

426
00:35:02,100 --> 00:35:07,700
remember, this dishRouter is defined
inside the dishRouter.js file.

427
00:35:07,700 --> 00:35:12,870
Now, I need to export this
from this node module.

428
00:35:12,870 --> 00:35:17,500
So to export this,
I'll go to the bottom here and

429
00:35:17,500 --> 00:35:22,877
I'll say module.exports and

430
00:35:22,877 --> 00:35:26,170
say, dishRouter.

431
00:35:26,170 --> 00:35:27,030
That is it.

432
00:35:27,030 --> 00:35:34,920
So now, my dishRouter is
exporting everything that I need.

433
00:35:34,920 --> 00:35:41,170
Now, you are looking at this and
saying, what about Colon dish.

434
00:35:41,170 --> 00:35:44,302
That is going to be part
of your first assignment.

435
00:35:44,302 --> 00:35:47,010
Not only that, you'll be implementing for

436
00:35:47,010 --> 00:35:53,000
two additional REST API endpoints also,
the promotions and the leaders.

437
00:35:53,000 --> 00:35:57,960
But this already shows you
the structure of what a Express

438
00:35:57,960 --> 00:36:03,080
routers implementation of
res API support looks like.

439
00:36:03,080 --> 00:36:05,070
So this is for dishRouter.route.

440
00:36:05,070 --> 00:36:09,848
And one last thing before I forget,

441
00:36:09,848 --> 00:36:18,140
we should say
dishRouter.use(bodyParser.json()).

442
00:36:20,326 --> 00:36:23,852
Now once you have completed
implementing the dishRouter,

443
00:36:23,852 --> 00:36:26,130
we can now go in to the index.js file.

444
00:36:26,130 --> 00:36:34,020
Since this dishRouter is another node
module, a fine node module nevertheless.

445
00:36:34,020 --> 00:36:38,120
So we need to import this
into our application.

446
00:36:38,120 --> 00:36:44,250
So right here, I'm going to import const

447
00:36:46,000 --> 00:36:52,100
dishRouter is equal to require.

448
00:36:52,100 --> 00:36:56,626
Now since this is a file
based node module,

449
00:36:56,626 --> 00:37:03,000
I'll say ./routes/dishRouter.

450
00:37:03,000 --> 00:37:09,850
And once I have declared them there,
then I come down into the code here.

451
00:37:09,850 --> 00:37:14,230
And right there, I say app.use.

452
00:37:14,230 --> 00:37:19,130
And I mount the router at an endpoint.

453
00:37:19,130 --> 00:37:20,930
So how do I mount the router?

454
00:37:20,930 --> 00:37:25,790
The first parameter here,
I will specify slash dishes.

455
00:37:25,790 --> 00:37:29,480
And the second parameter,
specify dishRouter.

456
00:37:30,810 --> 00:37:31,830
And that's it.

457
00:37:31,830 --> 00:37:35,080
So what this means is that
in my express application,

458
00:37:36,205 --> 00:37:41,435
any request coming to that slash dishes
endpoint will be handled by dishRouter,

459
00:37:41,435 --> 00:37:45,615
and that'll be done by
the code that is present here,

460
00:37:45,615 --> 00:37:50,755
because we have said dishRouter route,
and so notice that this says slash,

461
00:37:50,755 --> 00:37:54,575
so which means that this is mounted
at the slash dishes endpoint.

462
00:37:54,575 --> 00:37:59,378
So that's why anything coming through
slash dishes will be sent over to this and

463
00:37:59,378 --> 00:38:00,890
will be handled by this.

464
00:38:02,730 --> 00:38:07,000
A big hint for
you to think about how you would implement

465
00:38:07,000 --> 00:38:11,020
that colon dish id end point.

466
00:38:11,020 --> 00:38:16,440
You will still use the same dishRouter.js
file to also implement the support for

467
00:38:16,440 --> 00:38:21,670
that, /dishes/:dishID end point.

468
00:38:21,670 --> 00:38:24,580
That's another big hint for you, okay.

469
00:38:24,580 --> 00:38:29,260
With these changes,
let's save the changes that we have done

470
00:38:29,260 --> 00:38:33,870
to our application and
then restart our server and

471
00:38:33,870 --> 00:38:37,480
then take a look at how our
server is going to do its work.

472
00:38:38,850 --> 00:38:44,190
Going to the terminal, let me restart
that server by typing npm start.

473
00:38:44,190 --> 00:38:47,340
And once the server is up and running,
I'm going to go to postman and

474
00:38:47,340 --> 00:38:50,220
send requests from postman to this server.

475
00:38:51,220 --> 00:38:57,110
Going to postman, now I know that
my server is only supporting

476
00:38:57,110 --> 00:39:01,690
the dishes end point,
I have implemented the dish ID part of it.

477
00:39:01,690 --> 00:39:05,570
So let me send a get request
to local host dishes and

478
00:39:05,570 --> 00:39:08,590
you'll see that it works
exactly like before.

479
00:39:08,590 --> 00:39:12,910
Now if you have done a previous request in
post then you can simply click on that and

480
00:39:12,910 --> 00:39:15,150
then resend that request.

481
00:39:16,940 --> 00:39:26,680
Put operation doesn't work,
post operation, Works as you see there.

482
00:39:26,680 --> 00:39:32,370
And then let's cause the delete
operation on the dishes.

483
00:39:32,370 --> 00:39:36,210
And it says deleting
all dishes as expected.

484
00:39:36,210 --> 00:39:45,230
So this is the implementation of the rest
API support using express router.

485
00:39:45,230 --> 00:39:48,950
With this, we complete the second
half of this exercise.

486
00:39:48,950 --> 00:39:54,490
This is a good time for you to do a get
comment with the message express router.

487
00:39:55,830 --> 00:40:00,896
Now that we have completed this exercise
where we have seen how Express can

488
00:40:00,896 --> 00:40:06,450
be used to support implementation of the
Res API end point support on our server,

489
00:40:06,450 --> 00:40:08,900
and also the use of Express router,

490
00:40:08,900 --> 00:40:13,906
it's time to move onto the first
assignment which follows this lesson.

491
00:40:13,906 --> 00:40:20,030
[MUSIC]