Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,350 --> 00:00:03,520
Let's now implement a route handler
2
00:00:03,520 --> 00:00:07,750
for post requests so that we can actually add a Natour
3
00:00:07,750 --> 00:00:09,023
to our data set.
4
00:00:10,470 --> 00:00:12,870
So just like with the doGET() request handler,
5
00:00:12,870 --> 00:00:15,350
let's start by adding a new route.
6
00:00:15,350 --> 00:00:19,220
So app.post this time, and remember
7
00:00:19,220 --> 00:00:22,120
now we use post so the post method here,
8
00:00:22,120 --> 00:00:26,140
because that's the kind of request that we want to handle.
9
00:00:26,140 --> 00:00:30,263
So requests that are made with the http post verb right?
10
00:00:32,482 --> 00:00:33,316
V1
11
00:00:34,610 --> 00:00:35,960
and tours.
12
00:00:35,960 --> 00:00:39,690
Just as we talked about in the REST API lecture,
13
00:00:39,690 --> 00:00:42,363
the URL is actually exactly the same.
14
00:00:43,390 --> 00:00:46,290
No matter if we want to get all the tours,
15
00:00:46,290 --> 00:00:48,800
or if we want to create a new tour,
16
00:00:48,800 --> 00:00:50,650
the URL is exactly the same.
17
00:00:50,650 --> 00:00:53,780
Then only thing that changes is the http method
18
00:00:53,780 --> 00:00:56,363
that we used for doing these requests.
19
00:00:57,592 --> 00:01:01,420
To get all the tours and post to create a new tour,
20
00:01:01,420 --> 00:01:04,337
then in here, our typical call-back function
21
00:01:04,337 --> 00:01:06,906
where we get access to the request
22
00:01:06,906 --> 00:01:10,810
and to the respond object so that we can get some data
23
00:01:10,810 --> 00:01:14,560
from the request and send back an appropriate response.
24
00:01:14,560 --> 00:01:16,800
Now remember that with a post request,
25
00:01:16,800 --> 00:01:20,790
we can send data from the client to the server, right?
26
00:01:20,790 --> 00:01:24,543
This data is then ideally available on the request.
27
00:01:25,690 --> 00:01:28,970
The request object again is what holds all the data,
28
00:01:28,970 --> 00:01:32,240
all the information, about the request that was done.
29
00:01:32,240 --> 00:01:35,190
If that request contains some data that was sent,
30
00:01:35,190 --> 00:01:38,840
that data should be on the request, right?
31
00:01:38,840 --> 00:01:42,890
Now out of the box, Express does not put that body data
32
00:01:42,890 --> 00:01:46,670
on the request, and in order to have that data available,
33
00:01:46,670 --> 00:01:49,176
we have to use something called middleware.
34
00:01:49,176 --> 00:01:52,100
We are gonna talk in great detail about middleware
35
00:01:52,100 --> 00:01:54,220
in a couple of lectures, but for now,
36
00:01:54,220 --> 00:01:56,280
in order to make this work, we need to include
37
00:01:56,280 --> 00:01:59,943
a simple middleware here at the top of the file.
38
00:02:01,860 --> 00:02:04,000
So actually, right here.
39
00:02:04,000 --> 00:02:08,490
What we need to do, is to say app.use
40
00:02:08,490 --> 00:02:09,370
then express
41
00:02:12,687 --> 00:02:13,520
.json
42
00:02:14,490 --> 00:02:16,030
Okay, and that's it.
43
00:02:16,030 --> 00:02:20,480
So again, this [express.json] here is middleware.
44
00:02:20,480 --> 00:02:23,510
And middleware is basically a function that can modify
45
00:02:23,510 --> 00:02:25,560
the incoming request data.
46
00:02:25,560 --> 00:02:28,370
It's called middleware because it stands between,
47
00:02:28,370 --> 00:02:31,390
so in the middle of the request and the response.
48
00:02:31,390 --> 00:02:34,030
It's just a step that the request goes through
49
00:02:34,030 --> 00:02:35,830
while it's being processed.
50
00:02:35,830 --> 00:02:38,440
And the step the requests go through, in this example
51
00:02:38,440 --> 00:02:42,330
is simply that the data from the body is added to it.
52
00:02:42,330 --> 00:02:44,410
So it's added to the request object
53
00:02:44,410 --> 00:02:45,963
by using this middleware.
54
00:02:47,560 --> 00:02:51,763
We use app.use in order to use middleware like this.
55
00:02:52,870 --> 00:02:55,250
And again, we will talk a lot more about middleware
56
00:02:55,250 --> 00:02:57,540
a bit later in this section.
57
00:02:57,540 --> 00:03:00,703
For now, just know that this is how we use middleware.
58
00:03:01,750 --> 00:03:04,110
All the details will then follow later.
59
00:03:04,110 --> 00:03:07,307
So for now, just to start, I want to actually show you
60
00:03:07,307 --> 00:03:09,830
the data from the body in the console,
61
00:03:09,830 --> 00:03:14,830
just to verify that it actually works, so req.body.
62
00:03:14,920 --> 00:03:17,960
Again, body is the property that is gonna be available
63
00:03:17,960 --> 00:03:21,130
on the request, because we used that middleware
64
00:03:21,130 --> 00:03:22,393
a couple of moments ago.
65
00:03:23,560 --> 00:03:27,090
So we want to log[req.body] to the console
66
00:03:27,090 --> 00:03:29,600
and then we also need to send back a response.
67
00:03:29,600 --> 00:03:34,083
So res.send and for now just some place holder here.
68
00:03:35,870 --> 00:03:37,020
Just 'Done' okay.
69
00:03:37,020 --> 00:03:39,070
So we always need to send back something
70
00:03:39,070 --> 00:03:42,600
in order to finish the so-called request/response cycle,
71
00:03:42,600 --> 00:03:44,980
which again we're gonna learn a bit more about later
72
00:03:44,980 --> 00:03:46,530
in this section.
73
00:03:46,530 --> 00:03:49,363
So let's now test this out, and create a new request
74
00:03:49,363 --> 00:03:51,200
for the post.
75
00:03:51,200 --> 00:03:53,680
Now here in postman, we can do something really nice
76
00:03:53,680 --> 00:03:56,133
which is to save all of these requests.
77
00:03:58,180 --> 00:04:01,200
I'm gonna start by creating a new collection,
78
00:04:01,200 --> 00:04:02,500
which I will call Natours.
79
00:04:06,570 --> 00:04:07,600
New collection.
80
00:04:07,600 --> 00:04:10,793
Now I will save this request inside that collection.
81
00:04:12,650 --> 00:04:14,573
So this one is Get all the Tours.
82
00:04:17,380 --> 00:04:20,290
Put it into Natours, and save.
83
00:04:20,290 --> 00:04:23,563
So now we have our Get requests right here.
84
00:04:26,070 --> 00:04:27,410
Well let's now copy this one right here,
85
00:04:27,410 --> 00:04:28,583
create a new one.
86
00:04:30,120 --> 00:04:31,970
So this one is a post request.
87
00:04:31,970 --> 00:04:35,330
Before doing it, I will actually also save it right
88
00:04:35,330 --> 00:04:36,853
to this collection.
89
00:04:38,080 --> 00:04:41,083
So Create New Tour.
90
00:04:43,443 --> 00:04:47,733
Here on the left side, we now have both of our requests.
91
00:04:49,070 --> 00:04:52,620
Let's now actually specify our body here,
92
00:04:52,620 --> 00:04:56,550
so the data that we basically want to send to the server.
93
00:04:56,550 --> 00:04:59,370
We come here to Body, and there are different ways
94
00:04:59,370 --> 00:05:02,250
of doing it now, but the easiest one is to simply say,
95
00:05:02,250 --> 00:05:06,260
Raw, and then Application JSON.
96
00:05:06,260 --> 00:05:09,470
All we have to do here is to basically specify some JSON,
97
00:05:09,470 --> 00:05:12,180
and that will then get transmitted to the server.
98
00:05:12,180 --> 00:05:13,760
So the first thing we want to specify
99
00:05:13,760 --> 00:05:17,150
is the name of the Tour, and you remember that in JSON,
100
00:05:17,150 --> 00:05:20,550
everything has to be in quotes and actually double quotes,
101
00:05:20,550 --> 00:05:21,873
not just any quotes.
102
00:05:22,810 --> 00:05:26,040
Let me just call this one a Test Tour, and let's take a look
103
00:05:26,040 --> 00:05:29,343
at what else we have in the tour data.
104
00:05:30,550 --> 00:05:32,850
So we have the name, we have the duration,
105
00:05:32,850 --> 00:05:35,033
and let's just put also the difficulty.
106
00:05:37,260 --> 00:05:38,720
So I'm not going to specify all of these
107
00:05:38,720 --> 00:05:40,893
because that takes way too much time.
108
00:05:42,500 --> 00:05:45,870
Let's just say the duration is 10 days,
109
00:05:45,870 --> 00:05:46,890
and the difficulty
110
00:05:50,250 --> 00:05:51,483
is easy.
111
00:05:52,800 --> 00:05:57,290
That as our body will be transmitted to the server.
112
00:05:57,290 --> 00:06:00,550
So that's tested out now, hitting send
113
00:06:00,550 --> 00:06:03,500
and here we get back our response, which is done, remember.
114
00:06:07,104 --> 00:06:09,520
Here is the response that we sent, so done.
115
00:06:09,520 --> 00:06:11,700
And you see that down here in our console,
116
00:06:11,700 --> 00:06:15,153
we now have an object which is basically the JSON
117
00:06:15,153 --> 00:06:17,750
that we sent in our body, right?
118
00:06:17,750 --> 00:06:19,600
So perfect, it works!
119
00:06:19,600 --> 00:06:21,170
It is no longer JSON, as you can see.
120
00:06:21,170 --> 00:06:23,860
It is truly a JavaScript object.
121
00:06:23,860 --> 00:06:27,000
Just to show you that we really needed middleware,
122
00:06:27,000 --> 00:06:28,350
let's comment this one out.
123
00:06:30,190 --> 00:06:31,688
Send it again.
124
00:06:31,688 --> 00:06:36,160
Now no longer have the request.body, right?
125
00:06:36,160 --> 00:06:39,093
That's why it's undefined now, 'cause it no longer exists.
126
00:06:40,440 --> 00:06:44,030
Give it a save, send our request again,
127
00:06:44,030 --> 00:06:45,403
and so now it's back.
128
00:06:48,220 --> 00:06:51,510
We have our post route all set up, it's working now
129
00:06:51,510 --> 00:06:53,810
and we also get access to the body,
130
00:06:53,810 --> 00:06:56,830
and so what we want to do now is to actually persist
131
00:06:56,830 --> 00:07:00,680
that data into this tour simple JSON file.
132
00:07:00,680 --> 00:07:04,090
We're actually gonna modify this file, so that the data
133
00:07:04,090 --> 00:07:07,500
is saved to our fictional database here.
134
00:07:07,500 --> 00:07:09,820
This JSON file right now kind of works
135
00:07:09,820 --> 00:07:11,623
as our fictional database.
136
00:07:12,850 --> 00:07:15,810
So let's now take care of that.
137
00:07:15,810 --> 00:07:18,063
I'm gonna go ahead and comment this one out.
138
00:07:19,250 --> 00:07:20,650
So let's get started.
139
00:07:20,650 --> 00:07:23,440
The first thing we need to do is to figure out the id
140
00:07:23,440 --> 00:07:25,240
of the new object.
141
00:07:25,240 --> 00:07:28,720
Remember, again in the lecture about REST API's,
142
00:07:28,720 --> 00:07:30,570
is that when we create a new object,
143
00:07:30,570 --> 00:07:33,200
we never specify the id of the object.
144
00:07:33,200 --> 00:07:35,623
The database usually takes care of that.
145
00:07:36,980 --> 00:07:40,470
A new object usually automatically gets it's new id.
146
00:07:40,470 --> 00:07:42,840
Well in this case, we do not have any database,
147
00:07:42,840 --> 00:07:46,020
and so what we're gonna do is to simply take the id
148
00:07:46,020 --> 00:07:48,833
of the last object and then add +1 to that.
149
00:07:50,020 --> 00:07:51,010
Let's do that very quickly,
150
00:07:51,010 --> 00:07:53,440
and that's just basic JavaScript.
151
00:07:53,440 --> 00:07:57,740
So newId it is tours, right?
152
00:07:57,740 --> 00:08:01,160
Remember that tours, we already have the data
153
00:08:01,160 --> 00:08:02,673
in this tours variable.
154
00:08:03,800 --> 00:08:06,500
So that's an array of all the tour objects,
155
00:08:06,500 --> 00:08:08,313
and so we want to get the last one,
156
00:08:09,150 --> 00:08:13,410
which is tours.length - 1,
157
00:08:13,410 --> 00:08:17,220
then the id property of that and we add + 1.
158
00:08:17,220 --> 00:08:18,823
So that is the new id.
159
00:08:21,533 --> 00:08:23,033
Next up, we create a new tour,
160
00:08:23,950 --> 00:08:28,070
and that tour will basically be the body that we send
161
00:08:28,070 --> 00:08:30,863
plus the new id that we just created.
162
00:08:31,950 --> 00:08:36,730
So we can use object.assign, which basically allows us
163
00:08:36,730 --> 00:08:40,070
to create a new object by merging two existing objects
164
00:08:40,070 --> 00:08:41,130
together.
165
00:08:41,130 --> 00:08:43,299
So the first object is gonna be
166
00:08:43,299 --> 00:08:44,920
id=newId,
167
00:08:48,270 --> 00:08:49,200
and then we want
168
00:08:51,140 --> 00:08:52,353
request.body.
169
00:08:54,330 --> 00:08:58,730
We could also had done req.body.id=newId,
170
00:09:00,250 --> 00:09:03,503
but I did not want to mutate the original body object.
171
00:09:04,740 --> 00:09:06,323
I just left it like this.
172
00:09:08,564 --> 00:09:11,720
That is the new tour, and now what we want to do
173
00:09:11,720 --> 00:09:16,120
is to push this tour into the tour array, right?
174
00:09:16,120 --> 00:09:19,130
So tours, is the array of the nine tours that we have
175
00:09:19,130 --> 00:09:20,330
at this point.
176
00:09:20,330 --> 00:09:25,163
We say push and push the new tour in there.
177
00:09:27,880 --> 00:09:31,500
That is the array which now has the new tour in it,
178
00:09:31,500 --> 00:09:35,110
but of course now we have to persist that into the file.
179
00:09:35,110 --> 00:09:37,855
That again is quite easy for you at this point,
180
00:09:37,855 --> 00:09:41,840
so use fs.write file.
181
00:09:41,840 --> 00:09:43,390
So which one should we use?
182
00:09:43,390 --> 00:09:46,540
writeFile or writeFileSync?
183
00:09:47,570 --> 00:09:50,070
The answer for that is very simple.
184
00:09:50,070 --> 00:09:52,010
We are inside of a call-back function,
185
00:09:52,010 --> 00:09:54,430
that is gonna run in the event loop.
186
00:09:54,430 --> 00:09:57,180
We can never, ever block the event loop.
187
00:09:57,180 --> 00:10:00,030
What we're gonna do is to use writeFile
188
00:10:00,030 --> 00:10:01,720
and not to Sync in this one.
189
00:10:01,720 --> 00:10:03,820
We want to pass in a call-back function
190
00:10:03,820 --> 00:10:05,800
that is gonna be processed in the background
191
00:10:05,800 --> 00:10:07,160
and as soon as it's ready,
192
00:10:07,160 --> 00:10:10,810
it's gonna put its event in one of the event loop queue,
193
00:10:10,810 --> 00:10:12,180
which is then gonna be handled
194
00:10:12,180 --> 00:10:14,760
as soon as the event loop passes that phase.
195
00:10:14,760 --> 00:10:17,360
Remember that from the Event Loop lecture?
196
00:10:17,360 --> 00:10:21,080
Anyway, let's get the file name from up here,
197
00:10:21,080 --> 00:10:23,650
because we will really override this file
198
00:10:23,650 --> 00:10:25,270
so that when we restart this server,
199
00:10:25,270 --> 00:10:26,823
it's then gonna be there.
200
00:10:28,040 --> 00:10:30,101
So where-
201
00:10:30,101 --> 00:10:31,018
right here.
202
00:10:31,937 --> 00:10:35,246
So that is the file that where we want to write to.
203
00:10:35,246 --> 00:10:37,733
The data that we want to write, which is tours,
204
00:10:39,040 --> 00:10:40,760
and then our call-back function,
205
00:10:40,760 --> 00:10:43,203
which in this case only has the error.
206
00:10:46,040 --> 00:10:49,890
Here I just noticed that we need to also stringify
207
00:10:49,890 --> 00:10:51,580
this object, right?
208
00:10:51,580 --> 00:10:53,680
We want json in this JSON file,
209
00:10:53,680 --> 00:10:57,020
and this right now is just a plain, normal JavaScript object
210
00:10:57,020 --> 00:10:58,580
and so we need to convert that.
211
00:10:58,580 --> 00:11:01,160
But that's again simple, already did that.
212
00:11:01,160 --> 00:11:02,320
JSON.stringify
213
00:11:05,510 --> 00:11:06,343
of the object.
214
00:11:07,930 --> 00:11:11,760
What do we want to do as soon as the file is written?
215
00:11:11,760 --> 00:11:13,340
Well, what we usually do is
216
00:11:13,340 --> 00:11:16,683
to send the newly created object as the response.
217
00:11:17,670 --> 00:11:18,600
Let's do that.
218
00:11:18,600 --> 00:11:21,650
So response, and then as always, we start by
219
00:11:21,650 --> 00:11:23,340
specifying the status code.
220
00:11:23,340 --> 00:11:26,483
In this case, it's not gonna be a 200, but a 201.
221
00:11:27,400 --> 00:11:30,190
So 201 which means created.
222
00:11:30,190 --> 00:11:34,090
200 stands for okay, 201 stands for created,
223
00:11:34,090 --> 00:11:36,260
which is exactly what happened here.
224
00:11:36,260 --> 00:11:38,513
We created a new resource on a server.
225
00:11:41,260 --> 00:11:45,873
So JSON, and it keeps adding this message here.
226
00:11:48,240 --> 00:11:50,993
Status is again, success.
227
00:11:52,500 --> 00:11:56,770
I'm not using the results property here, that we had before,
228
00:11:56,770 --> 00:11:59,263
because now I'm only sending back one result.
229
00:12:00,651 --> 00:12:02,203
Data is our envelope here.
230
00:12:04,100 --> 00:12:09,100
Tour is new tour.
231
00:12:09,230 --> 00:12:10,063
That's it!
232
00:12:10,063 --> 00:12:12,660
Give it a save here, and so that format it,
233
00:12:12,660 --> 00:12:14,260
or code a little bit.
234
00:12:14,260 --> 00:12:15,933
Let's add some more space here.
235
00:12:18,480 --> 00:12:20,533
I think we're ready to now give it a try.
236
00:12:23,180 --> 00:12:26,273
We can actually create a new tour, using this data here.
237
00:12:28,600 --> 00:12:30,440
So adjust this Test Tour,
238
00:12:30,440 --> 00:12:32,540
and with this data, and let's wait for it.
239
00:12:33,670 --> 00:12:34,620
What happened here?
240
00:12:35,780 --> 00:12:37,000
Let's see, ah okay.
241
00:12:37,000 --> 00:12:40,733
We have a problem that we sent a response twice basically.
242
00:12:40,733 --> 00:12:43,320
So we sent a response with JSON,
243
00:12:43,320 --> 00:12:45,530
and then again with .send.
244
00:12:45,530 --> 00:12:46,620
That's not allowed.
245
00:12:46,620 --> 00:12:48,990
That's actually why we get this error here.
246
00:12:48,990 --> 00:12:50,622
So it's actually nice that it happened here,
247
00:12:50,622 --> 00:12:54,871
so that you know what this standard error here means.
248
00:12:54,871 --> 00:12:58,330
HTTP_HEADERS_SENT cannot set headers after they are sent
249
00:12:58,330 --> 00:12:59,163
to the client.
250
00:12:59,163 --> 00:13:02,483
That happens when you try to send two responses.
251
00:13:04,642 --> 00:13:06,540
We need to get rid of that.
252
00:13:10,030 --> 00:13:12,163
Yeah, that was the only error that we had.
253
00:13:14,350 --> 00:13:16,390
That should now fix it.
254
00:13:16,390 --> 00:13:17,313
Give it a try.
255
00:13:19,410 --> 00:13:21,190
And indeed, here we go.
256
00:13:21,190 --> 00:13:24,941
So we have our id of 10, which our code created for us,
257
00:13:24,941 --> 00:13:28,930
and then the entire tour which is exactly the data
258
00:13:28,930 --> 00:13:32,353
that we sent as a body in our request.
259
00:13:33,850 --> 00:13:36,900
Now the thing is, that when we do get all tours here,
260
00:13:36,900 --> 00:13:38,363
it will not yet show up.
261
00:13:39,480 --> 00:13:40,933
If we go down here,
262
00:13:42,520 --> 00:13:44,770
actually it is already here.
263
00:13:44,770 --> 00:13:46,984
We have the one from before, so when we had that error,
264
00:13:46,984 --> 00:13:50,830
it actually also created this tour here.
265
00:13:50,830 --> 00:13:54,000
Then of course, the tour that we created with the id of 10.
266
00:13:54,000 --> 00:13:56,180
So this tour here is exactly this one
267
00:13:56,180 --> 00:13:57,530
that was created right now.
268
00:13:58,370 --> 00:14:03,370
If we do now, Test Tour 2, with the duration of just 5.
269
00:14:03,890 --> 00:14:05,580
Let's give this one a try,
270
00:14:05,580 --> 00:14:10,550
and if I do get all tours, then that shows up here as well.
271
00:14:10,550 --> 00:14:12,246
I said in the beginning that it wouldn't work,
272
00:14:12,246 --> 00:14:14,760
so that it wouldn't show up right away,
273
00:14:14,760 --> 00:14:17,793
because this only works if we restart the server.
274
00:14:18,950 --> 00:14:19,913
This file,
275
00:14:21,480 --> 00:14:23,610
the tour-simple.json file
276
00:14:23,610 --> 00:14:25,140
where our tours are coming from,
277
00:14:25,140 --> 00:14:28,819
is only read at the beginning when we start the server.
278
00:14:28,819 --> 00:14:31,420
Now the thing is that each time we save something
279
00:14:31,420 --> 00:14:35,180
to this JSON file, it will also reload the server.
280
00:14:35,180 --> 00:14:38,160
Let me prove that to you, so if I hit now save,
281
00:14:38,160 --> 00:14:40,800
then look what happens down here.
282
00:14:40,800 --> 00:14:43,180
So I save it, and so you see that down here
283
00:14:43,180 --> 00:14:44,980
it restarted the server.
284
00:14:44,980 --> 00:14:47,920
It also formatted the code, because of prettier.
285
00:14:47,920 --> 00:14:50,970
But what matters here, is that this code updated.
286
00:14:50,970 --> 00:14:52,780
That's why each time we save something here
287
00:14:52,780 --> 00:14:56,160
with our API, it will then immediately reload the server
288
00:14:56,160 --> 00:15:00,230
and will reload the content from the file into the tours,
289
00:15:00,230 --> 00:15:02,080
and we can then read that right away.
290
00:15:03,020 --> 00:15:06,400
That's why we have access immediately
291
00:15:06,400 --> 00:15:08,563
to all the newly created tours.
292
00:15:09,660 --> 00:15:10,550
Make sense?
293
00:15:10,550 --> 00:15:13,970
Well if not, please feel free to just ask your questions,
294
00:15:13,970 --> 00:15:16,673
and of course Q&A section, right?
22905
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.