Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,300 --> 00:00:02,640
Welcome back.
2
00:00:02,640 --> 00:00:05,120
Let's now finally set up Express,
3
00:00:05,120 --> 00:00:08,390
create a simple server, and do some basic routing
4
00:00:08,390 --> 00:00:10,300
just to get an initial feeling
5
00:00:10,300 --> 00:00:13,590
of how we actually work in Express.
6
00:00:13,590 --> 00:00:16,230
And so in this video, we're actually gonna start working
7
00:00:16,230 --> 00:00:19,883
on the natours project, so the big main project
8
00:00:19,883 --> 00:00:21,690
that is part of this course.
9
00:00:21,690 --> 00:00:23,750
So just like before, please go ahead
10
00:00:23,750 --> 00:00:27,180
and get the starter project from the course files.
11
00:00:27,180 --> 00:00:29,190
I already have it here.
12
00:00:29,190 --> 00:00:32,470
I have it opened up in my VS code.
13
00:00:32,470 --> 00:00:36,180
Let's take just a very quick look at what we have here.
14
00:00:36,180 --> 00:00:38,270
We have the prettier configuration file
15
00:00:38,270 --> 00:00:40,800
just like we set up like before.
16
00:00:40,800 --> 00:00:44,210
I also have a config file for ESLint,
17
00:00:44,210 --> 00:00:46,894
and ESLint we haven't set up at this point,
18
00:00:46,894 --> 00:00:48,250
but we're going to do that
19
00:00:48,250 --> 00:00:50,230
also later in this section.
20
00:00:50,230 --> 00:00:51,810
For now, I just want to start coding
21
00:00:51,810 --> 00:00:54,710
and not worry about linting our code.
22
00:00:54,710 --> 00:00:57,410
Linting is just to fix some errors.
23
00:00:57,410 --> 00:00:58,930
But again, we're going to do that later.
24
00:00:58,930 --> 00:01:02,170
Here we just have some files for later,
25
00:01:02,170 --> 00:01:07,170
like css, images, okay, and now also for example,
26
00:01:07,700 --> 00:01:11,287
some data that we need to build our application.
27
00:01:11,287 --> 00:01:14,790
Again, we're going to start using this data here
28
00:01:14,790 --> 00:01:16,330
throughout this section.
29
00:01:16,330 --> 00:01:19,440
Anyway, we now have a brand new project.
30
00:01:19,440 --> 00:01:21,950
The first thing that we usually do with a new project
31
00:01:21,950 --> 00:01:24,410
is to create the package.json file,
32
00:01:24,410 --> 00:01:26,590
just like I mentioned before.
33
00:01:26,590 --> 00:01:28,453
We did that in the nodefarm project,
34
00:01:28,453 --> 00:01:31,090
and now we're going to do that here as well.
35
00:01:31,090 --> 00:01:32,863
I open up the integrated terminal.
36
00:01:34,480 --> 00:01:38,290
You can do that by hitting this shortcut here
37
00:01:38,290 --> 00:01:40,490
or just come here to View and then Terminal.
38
00:01:41,710 --> 00:01:44,750
Let me put it up a little bit so you can see it.
39
00:01:44,750 --> 00:01:47,510
Remember the command is npm init.
40
00:01:48,927 --> 00:01:51,083
We gave a name to the project.
41
00:01:53,010 --> 00:01:54,340
It's called natours.
42
00:01:54,340 --> 00:01:56,540
Oh okay, it cannot have any capital letters.
43
00:01:57,560 --> 00:01:59,000
Let's do that again.
44
00:01:59,000 --> 00:02:02,190
The version is one, description, let's just say
45
00:02:02,190 --> 00:02:07,190
learning node, express, and mongoDB, mongoDB.
46
00:02:11,930 --> 00:02:13,930
Entry point, index.js, well actually,
47
00:02:13,930 --> 00:02:16,400
we're going to use app.js now.
48
00:02:16,400 --> 00:02:17,890
Let's call it app.js.
49
00:02:17,890 --> 00:02:20,820
We have no test command, no git, no keywords,
50
00:02:20,820 --> 00:02:24,633
author is me, and that's it, so okay.
51
00:02:28,090 --> 00:02:29,533
Here we have package.json.
52
00:02:30,560 --> 00:02:32,393
Just like before, nothing new here.
53
00:02:33,363 --> 00:02:35,720
Now it's time to install Express.
54
00:02:35,720 --> 00:02:38,820
Npm I express.
55
00:02:38,820 --> 00:02:40,630
I'm going to use Express for here,
56
00:02:40,630 --> 00:02:42,100
which is at the point of recording,
57
00:02:42,100 --> 00:02:44,610
the latest version, but for some time,
58
00:02:44,610 --> 00:02:47,860
the Express team has been working on version 5.
59
00:02:47,860 --> 00:02:50,170
That is a major version which could introduce
60
00:02:50,170 --> 00:02:53,240
breaking changes if you installed that one.
61
00:02:53,240 --> 00:02:54,750
Now, there's actually not a lot of stuff
62
00:02:54,750 --> 00:02:57,450
changing in version 5, but to make sure,
63
00:02:57,450 --> 00:03:01,170
please install Express at version 4 just like I'm doing.
64
00:03:01,170 --> 00:03:05,310
In order to do that, just hit at 4.
65
00:03:05,310 --> 00:03:08,770
That will then install the latest version inside of 4
66
00:03:08,770 --> 00:03:13,140
with the latest minor and patch versions, so 4.16.4.
67
00:03:14,260 --> 00:03:16,300
That's the version that I'm using now
68
00:03:16,300 --> 00:03:18,630
in this course, and yours will probably be
69
00:03:18,630 --> 00:03:21,480
a later version, but as long as you have the 4 here
70
00:03:21,480 --> 00:03:23,330
in the beginning, you should be fine.
71
00:03:24,563 --> 00:03:26,060
You see that it actually created
72
00:03:26,060 --> 00:03:28,307
our node modules folder over here.
73
00:03:28,307 --> 00:03:30,505
Now, we're ready to start.
74
00:03:30,505 --> 00:03:35,030
I'm creating a new file and it's called app.js.
75
00:03:35,030 --> 00:03:37,470
It's kind of a convention to have
76
00:03:37,470 --> 00:03:39,763
all the Express configuration in app.js.
77
00:03:40,949 --> 00:03:42,743
That's what I'm using here now.
78
00:03:43,860 --> 00:03:45,410
We have now the warning
79
00:03:45,410 --> 00:03:46,900
that it can't load ESLint.
80
00:03:46,900 --> 00:03:48,650
Again, we're going to configure that later
81
00:03:48,650 --> 00:03:50,260
throughout this section.
82
00:03:50,260 --> 00:03:51,903
So, let's now use Express.
83
00:03:53,661 --> 00:03:58,661
I'm going to require of course, the Express package.
84
00:03:58,670 --> 00:04:00,370
Nothing new at this point.
85
00:04:00,370 --> 00:04:01,490
Give it a safe.
86
00:04:01,490 --> 00:04:03,850
You see that preview already did its magic here
87
00:04:03,850 --> 00:04:06,170
by adding this semi-colon and also adding
88
00:04:06,170 --> 00:04:07,403
this new line down here.
89
00:04:08,500 --> 00:04:10,840
We have Express imported, and now what we do
90
00:04:10,840 --> 00:04:13,100
is to create a variable called app.
91
00:04:13,100 --> 00:04:14,793
Again, that's kind of a standard.
92
00:04:16,540 --> 00:04:20,683
So app and assigned result of calling express.
93
00:04:21,577 --> 00:04:23,063
That's actually it.
94
00:04:24,290 --> 00:04:26,960
This here is a function which upon calling
95
00:04:26,960 --> 00:04:30,373
will add a bunch of methods to our app variable here.
96
00:04:31,700 --> 00:04:32,870
The first one that we're going to use
97
00:04:32,870 --> 00:04:34,930
is actually app.listen to
98
00:04:34,930 --> 00:04:36,543
basically start up a server.
99
00:04:38,769 --> 00:04:40,990
That is a bit similar to what we did before
100
00:04:40,990 --> 00:04:44,730
with the http package in the previous sections, right.
101
00:04:44,730 --> 00:04:46,500
So again, keep in mind that Express
102
00:04:46,500 --> 00:04:49,430
is 100% no js under the hood,
103
00:04:49,430 --> 00:04:50,710
and some of the things work
104
00:04:50,710 --> 00:04:53,070
in a very similar way here in Express.
105
00:04:53,070 --> 00:04:54,980
All right, again, it simply makes our lives
106
00:04:54,980 --> 00:04:56,790
a bit easier by taking some of
107
00:04:56,790 --> 00:04:58,920
the complexity away from us.
108
00:04:58,920 --> 00:05:00,670
Just like before, into app.listen,
109
00:05:00,670 --> 00:05:01,770
we paste in the port.
110
00:05:01,770 --> 00:05:05,053
Let's actually create a variable for that here before.
111
00:05:07,170 --> 00:05:09,870
Port and let's say 3000 for now.
112
00:05:09,870 --> 00:05:11,820
We're going to change that a bit later.
113
00:05:13,930 --> 00:05:16,443
We paste in the port and a callback function.
114
00:05:17,730 --> 00:05:19,108
Again, this is the callback function
115
00:05:19,108 --> 00:05:21,280
that will be called as soon as
116
00:05:21,280 --> 00:05:22,743
the server starts listening.
117
00:05:24,490 --> 00:05:27,340
Let's simply do a console.log here,
118
00:05:27,340 --> 00:05:29,963
app running on port.
119
00:05:36,770 --> 00:05:38,690
All right, so that is our server
120
00:05:38,690 --> 00:05:40,550
now actually already listening.
121
00:05:40,550 --> 00:05:43,660
Now what we need to do next is to define route.
122
00:05:43,660 --> 00:05:45,320
And once more, we actually already
123
00:05:45,320 --> 00:05:47,200
kind of defined routes before
124
00:05:47,200 --> 00:05:49,840
in the nodefarm project, remember that,
125
00:05:49,840 --> 00:05:52,770
but it works very differently with Express.
126
00:05:52,770 --> 00:05:54,780
Remember that routing means basically
127
00:05:54,780 --> 00:05:57,560
to determine how an application responds
128
00:05:57,560 --> 00:06:01,053
to a certain client request, so to a certain URL.
129
00:06:02,250 --> 00:06:04,030
And actually, it's not just a URL
130
00:06:04,030 --> 00:06:05,732
but also the http method
131
00:06:05,732 --> 00:06:08,280
which is used for that request.
132
00:06:08,280 --> 00:06:11,283
Remember that from the http lecture that we had before.
133
00:06:12,370 --> 00:06:14,060
How do we do that?
134
00:06:14,060 --> 00:06:16,570
Well, it's very simple in Express.
135
00:06:16,570 --> 00:06:19,377
All we do is app, then the http method
136
00:06:19,377 --> 00:06:21,960
that we want to respond to,
137
00:06:21,960 --> 00:06:23,300
and let's start with the simplest one
138
00:06:23,300 --> 00:06:25,643
which is get, and then the URL.
139
00:06:26,630 --> 00:06:30,053
We're just specifying the kind of root URL here.
140
00:06:31,880 --> 00:06:34,380
Again, the route is basically the URL,
141
00:06:34,380 --> 00:06:36,980
which in this case, is just this root URL
142
00:06:36,980 --> 00:06:40,513
and also the http method, which is get in this case.
143
00:06:42,210 --> 00:06:44,100
Now, what do we actually want to happen
144
00:06:44,100 --> 00:06:47,570
when someone hits that URL with a get request?
145
00:06:47,570 --> 00:06:49,280
Well, whatever we want to do,
146
00:06:49,280 --> 00:06:51,710
we need to specify it in a callback function,
147
00:06:51,710 --> 00:06:54,758
which we specify as the second argument.
148
00:06:54,758 --> 00:06:56,108
We have a callback function
149
00:06:58,280 --> 00:07:00,720
just like this, and this callback function
150
00:07:00,720 --> 00:07:03,575
can accept a couple of arguments.
151
00:07:03,575 --> 00:07:05,870
The most basic one, and the ones that we usually
152
00:07:05,870 --> 00:07:10,590
always need are get request, entity response.
153
00:07:10,590 --> 00:07:13,740
In that regard, it is again very similar
154
00:07:13,740 --> 00:07:16,503
to what we did before in the nodefarm project.
155
00:07:17,380 --> 00:07:19,280
Back then, when we started our server,
156
00:07:19,280 --> 00:07:21,380
we also had access to the request
157
00:07:21,380 --> 00:07:23,210
and to response object.
158
00:07:23,210 --> 00:07:26,020
Now, they are a little bit different here in Express.
159
00:07:26,020 --> 00:07:29,364
They have a lot more data and methods on them,
160
00:07:29,364 --> 00:07:32,560
but the idea is exactly the same.
161
00:07:32,560 --> 00:07:34,740
So you see that Express apps
162
00:07:34,740 --> 00:07:37,150
and so also node apps for that matter,
163
00:07:37,150 --> 00:07:39,890
are all about requests and responses,
164
00:07:39,890 --> 00:07:42,660
simply because that is how the web actually works
165
00:07:42,660 --> 00:07:45,053
just as you learned in the previous sections.
166
00:07:47,280 --> 00:07:48,360
What do we want to do now?
167
00:07:48,360 --> 00:07:50,150
Well, all we want to do is to send
168
00:07:50,150 --> 00:07:51,613
some data back very quickly.
169
00:07:52,620 --> 00:07:55,122
We can use the response object
170
00:07:55,122 --> 00:07:58,560
and from there, we can use the send method
171
00:07:58,560 --> 00:08:01,113
and specify something that we want to send back.
172
00:08:03,180 --> 00:08:04,823
Hello from the server side.
173
00:08:06,476 --> 00:08:09,700
Just some string again that we want to send back.
174
00:08:09,700 --> 00:08:11,490
We can also specify the status code
175
00:08:11,490 --> 00:08:12,580
and that's very easy.
176
00:08:12,580 --> 00:08:15,380
All we have to do is to before we actually send
177
00:08:15,380 --> 00:08:16,850
the string to the client,
178
00:08:16,850 --> 00:08:18,693
we just add status before that.
179
00:08:20,200 --> 00:08:23,680
The status method, and in here,
180
00:08:23,680 --> 00:08:25,420
we specify our code.
181
00:08:25,420 --> 00:08:27,943
And again, we're going to use 200 for okay.
182
00:08:29,990 --> 00:08:32,570
That's actually all we have to do.
183
00:08:32,570 --> 00:08:34,360
Let's very quickly test this out
184
00:08:34,360 --> 00:08:36,222
and then add some more stuff to it.
185
00:08:38,610 --> 00:08:43,610
Let's use nodemon app.js.
186
00:08:43,640 --> 00:08:44,850
Let's see what happens
187
00:08:44,850 --> 00:08:47,420
and our app is running on port 3000.
188
00:08:47,420 --> 00:08:48,390
Great.
189
00:08:48,390 --> 00:08:50,690
And how do we now test this API?
190
00:08:50,690 --> 00:08:52,893
Well, that's why we installed Postman.
191
00:08:54,280 --> 00:08:55,623
Let's open up Postman.
192
00:08:56,560 --> 00:08:59,233
Actually it's still here from the last lecture.
193
00:09:00,450 --> 00:09:02,920
Let's go ahead and delete this
194
00:09:02,920 --> 00:09:06,730
and write the URL, or actually, the IP
195
00:09:06,730 --> 00:09:10,710
for local host and then our port.
196
00:09:10,710 --> 00:09:12,710
That is exactly the same as we did
197
00:09:12,710 --> 00:09:14,333
in the nodefarm project.
198
00:09:15,680 --> 00:09:17,103
We're hitting just the root.
199
00:09:18,060 --> 00:09:19,890
We can write the slash or it works
200
00:09:19,890 --> 00:09:21,440
just the same without anything.
201
00:09:22,300 --> 00:09:24,650
Let's send this and indeed, here we go.
202
00:09:24,650 --> 00:09:27,070
Hello from the server side.
203
00:09:27,070 --> 00:09:30,030
That's amazing, it already works.
204
00:09:30,030 --> 00:09:31,500
Just to make sure, we would of course
205
00:09:31,500 --> 00:09:34,128
get the exact same result in a browser.
206
00:09:34,128 --> 00:09:38,501
If I copy this here, you will see the same result.
207
00:09:38,501 --> 00:09:40,560
But again, we're going to use Postman
208
00:09:40,560 --> 00:09:45,130
because it makes all this API testing a lot easier for us.
209
00:09:45,130 --> 00:09:47,330
You will see, once we really start using
210
00:09:47,330 --> 00:09:49,783
more and more features of this great application.
211
00:09:51,330 --> 00:09:53,800
Now, going back here, let's add some more stuff
212
00:09:53,800 --> 00:09:56,270
because send simply sends this string here
213
00:09:56,270 --> 00:09:58,860
back to the client, but it's also very easy
214
00:09:58,860 --> 00:10:01,280
to send json to the client.
215
00:10:01,280 --> 00:10:04,853
Instead of using send, let's actually use json.
216
00:10:06,523 --> 00:10:08,370
The json method, and then in here,
217
00:10:08,370 --> 00:10:10,203
we can paste in an object.
218
00:10:11,440 --> 00:10:12,540
Let's do it like this.
219
00:10:18,480 --> 00:10:19,980
Let's say this is the message.
220
00:10:22,550 --> 00:10:25,133
And then just some other field here, natours.
221
00:10:29,060 --> 00:10:31,050
Okay, and now you see, that preview here
222
00:10:31,050 --> 00:10:33,230
auto-formatted this for us
223
00:10:33,230 --> 00:10:35,230
and they also want this in another line.
224
00:10:36,260 --> 00:10:38,560
Ah okay, we're missing a comma here of course.
225
00:10:39,410 --> 00:10:41,380
But the preview extension still put it
226
00:10:41,380 --> 00:10:43,250
up here for some reason.
227
00:10:43,250 --> 00:10:44,350
But of course, that doesn't really matter.
228
00:10:44,350 --> 00:10:46,023
It's just a matter of formatting.
229
00:10:47,955 --> 00:10:50,413
Let's take a look at what happens now.
230
00:10:52,840 --> 00:10:55,960
Now we have our nicely formatted json here.
231
00:10:55,960 --> 00:10:57,477
Gives us all these different colors here
232
00:10:57,477 --> 00:10:59,077
for the keys and for the values.
233
00:10:59,930 --> 00:11:02,010
Here we have the status codes.
234
00:11:02,010 --> 00:11:04,890
Here we have the time it took to do this request.
235
00:11:04,890 --> 00:11:07,683
We also have the size of the response.
236
00:11:08,620 --> 00:11:11,190
We could, for example, change the code here very easily.
237
00:11:11,190 --> 00:11:13,973
404 is not found, remember.
238
00:11:14,830 --> 00:11:19,120
Do it again, and then here you see, 404, not found.
239
00:11:19,120 --> 00:11:20,110
That's the kind of stuff that
240
00:11:20,110 --> 00:11:22,020
Postman is really great for.
241
00:11:22,020 --> 00:11:24,893
Let's actually increase the size of all this.
242
00:11:27,930 --> 00:11:31,730
Let's now continue, just add something else here.
243
00:11:31,730 --> 00:11:32,760
Just something very easy.
244
00:11:32,760 --> 00:11:34,870
Remember, all of this is just to test it out,
245
00:11:34,870 --> 00:11:37,070
to get our feet wet, and start getting
246
00:11:37,070 --> 00:11:39,060
a little bit of experience.
247
00:11:39,060 --> 00:11:43,233
Remember that this here is the http method for the request.
248
00:11:44,328 --> 00:11:46,900
This response that we're sending here,
249
00:11:46,900 --> 00:11:49,040
so this one here, is only sent
250
00:11:49,040 --> 00:11:54,040
when this get method is sent to our server on this URL.
251
00:11:57,180 --> 00:11:58,520
Let me actually show it to you.
252
00:11:58,520 --> 00:12:00,753
If we do, for example, a post.
253
00:12:02,220 --> 00:12:04,420
That's very easy again with Postman.
254
00:12:04,420 --> 00:12:07,200
If we do a post, then we will not get anything.
255
00:12:07,200 --> 00:12:10,490
Instead, what Express automatically sends back is
256
00:12:10,490 --> 00:12:14,270
this html here which says cannot post
257
00:12:14,270 --> 00:12:16,260
and then with a 404, not found.
258
00:12:16,260 --> 00:12:18,480
That's because we don't have any route defined
259
00:12:18,480 --> 00:12:22,163
for this URL and for this http method.
260
00:12:23,450 --> 00:12:25,493
Let's now very quickly do that.
261
00:12:28,370 --> 00:12:31,423
So post, we want the same URL,
262
00:12:34,920 --> 00:12:36,743
the request and the response object.
263
00:12:38,810 --> 00:12:41,823
Let's simply say res.send,
264
00:12:46,090 --> 00:12:48,723
you can post to this endpoint.
265
00:12:52,400 --> 00:12:54,200
Just so you see that this is how
266
00:12:54,200 --> 00:12:56,240
we send different responses for
267
00:12:56,240 --> 00:12:59,023
different http methods in the request.
268
00:13:00,800 --> 00:13:03,380
Let's run that again, and indeed,
269
00:13:03,380 --> 00:13:07,033
you can post to this endpoint with a 200 status code.
270
00:13:07,033 --> 00:13:09,110
This is actually the default one
271
00:13:09,110 --> 00:13:12,395
when we do not specify any in our codes.
272
00:13:12,395 --> 00:13:15,610
Here we have 404 which is not correct.
273
00:13:15,610 --> 00:13:18,610
But again, 200 is actually the default.
274
00:13:18,610 --> 00:13:20,360
Another quick thing that I want to show you
275
00:13:20,360 --> 00:13:23,400
is that by using this json method here for example,
276
00:13:23,400 --> 00:13:26,000
this will automatically set our content type
277
00:13:26,000 --> 00:13:28,680
to application json, so remember that
278
00:13:28,680 --> 00:13:31,580
we did that manually in the nodefarm app
279
00:13:31,580 --> 00:13:34,800
when we created our very simple API.
280
00:13:34,800 --> 00:13:37,360
Back then, we also sent back some json,
281
00:13:37,360 --> 00:13:39,650
but we then had to manually define that
282
00:13:39,650 --> 00:13:42,360
the content was json so that the browser knew
283
00:13:42,360 --> 00:13:43,523
what it was expecting.
284
00:13:44,550 --> 00:13:47,440
But Express takes that work away from us.
285
00:13:47,440 --> 00:13:48,763
Let's actually check that.
286
00:13:50,020 --> 00:13:51,563
Going back to get.
287
00:13:53,120 --> 00:13:55,760
Here we have our result headers.
288
00:13:55,760 --> 00:13:57,250
Here you see that the content type
289
00:13:57,250 --> 00:14:00,040
is indeed set to application json.
290
00:14:00,040 --> 00:14:02,100
Express then also automatically sends
291
00:14:02,100 --> 00:14:04,020
a bunch of other headers, for example,
292
00:14:04,020 --> 00:14:08,623
this powered by Express or the date or the connection.
293
00:14:09,930 --> 00:14:11,880
But again, we're going to take more care
294
00:14:11,880 --> 00:14:15,040
of headers a bit later in the course.
295
00:14:15,040 --> 00:14:18,410
This was just a very small and simple example here,
296
00:14:18,410 --> 00:14:20,320
but you can already start to appreciate
297
00:14:20,320 --> 00:14:24,240
how much easier it is to do this kind of responses.
298
00:14:24,240 --> 00:14:27,660
To create this route in order to respond to client request,
299
00:14:27,660 --> 00:14:31,373
in a much easier way than in just plain node.js.
300
00:14:32,650 --> 00:14:34,390
With that being said, in the next video,
301
00:14:34,390 --> 00:14:36,990
we're going to learn all about building APIs
302
00:14:36,990 --> 00:14:39,930
using the rest architecture, so that after that,
303
00:14:39,930 --> 00:14:42,150
we can finally start building our API.
23204
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.