All language subtitles for 003 Handling Requests and Responses the GET Request_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,810 --> 00:00:07,980 Now in the last lesson, we set up and we started our server, and our server was listening on the port 2 00:00:08,039 --> 00:00:12,270 3000 for any browsers who are trying to get in touch with it. 3 00:00:12,420 --> 00:00:19,020 Unfortunately we got an error, where our browser said, “I got in touch with your server at this location 4 00:00:19,080 --> 00:00:21,930 3000, but I couldn't get anything back. 5 00:00:21,990 --> 00:00:26,690 I can't display you anything because your server isn't giving me anything to display.” 6 00:00:26,940 --> 00:00:30,250 So that's what we're going to address in this lesson. 7 00:00:30,270 --> 00:00:38,040 Now, what you have to imagine is that this localhost:3000 is basically the same as the route of any web 8 00:00:38,040 --> 00:00:38,700 site. 9 00:00:38,700 --> 00:00:40,000 So it's the home page. 10 00:00:40,050 --> 00:00:43,140 It's the equivalent of going to google.com, 11 00:00:43,140 --> 00:00:43,570 right? 12 00:00:43,740 --> 00:00:49,050 But in this case our domain is simply called localhost:3000. 13 00:00:49,230 --> 00:00:57,450 So this / just represents localhost:3000/, which is the route, or the 14 00:00:57,450 --> 00:00:59,720 home page, of our web site. 15 00:00:59,910 --> 00:01:07,500 And, again, if I transform it to www.google.com/, then this will go to the home page 16 00:01:07,590 --> 00:01:10,700 of Google, and that is what we call the route. 17 00:01:10,710 --> 00:01:20,700 So when we load up a web site, say google.com, then our browser will send a request to Google's servers 18 00:01:21,060 --> 00:01:29,430 to get some data for this location, and Google servers, when it sees that request, it will send our browser 19 00:01:29,500 --> 00:01:37,140 a response, and that response includes the HTML, the CSS and the Javascript that’s needed to render 20 00:01:37,230 --> 00:01:38,700 this web site. 21 00:01:38,750 --> 00:01:44,430 Now, our server is currently not sending anything back when our browser makes the get requests. 22 00:01:44,490 --> 00:01:51,810 So let's change that. Just above the app.listen, we're going to say app.get. 23 00:01:52,080 --> 00:01:59,100 And this is a method that's provided by Express that allows us to specify what should happen when a 24 00:01:59,100 --> 00:02:04,940 browser gets in touch with our server and makes a get request. 25 00:02:04,950 --> 00:02:10,770 Now the first parameter it takes is the location of the get request. 26 00:02:10,770 --> 00:02:18,360 So when we type localhost:3000, the get request is being sent to the route of our web site, which 27 00:02:18,360 --> 00:02:21,380 is represented by a forward slash. 28 00:02:21,630 --> 00:02:24,660 So this is basically our home page. 29 00:02:24,660 --> 00:02:31,710 Now when that get request happens, we can trigger a callback function, and this callback function can 30 00:02:31,710 --> 00:02:36,610 have two parameters: request and response. 31 00:02:36,900 --> 00:02:45,540 So this method, app.get, defines what should happen when someone makes a get request to the home 32 00:02:45,690 --> 00:02:46,350 route. 33 00:02:46,470 --> 00:02:48,650 So that's the first parameter. 34 00:02:48,780 --> 00:02:57,030 And then there's a callback function that tells the server what to do when that request happens. 35 00:02:57,060 --> 00:03:04,530 So let's printout this request object that we get when the callback gets triggered and see what it looks 36 00:03:04,530 --> 00:03:04,880 like. 37 00:03:04,980 --> 00:03:11,970 So I'm just going to console.log the request, and I'm going to hit save l, and I'm going to terminate my 38 00:03:11,970 --> 00:03:17,310 server by hitting Control C, and then I'm going to restart it by saying node 39 00:03:17,360 --> 00:03:17,870 server.js, 40 00:03:18,060 --> 00:03:21,450 with the changes that we've implemented in here. 41 00:03:22,860 --> 00:03:29,810 So now let's head over to our browser and we're going to hit enter on localhost:3000 again, and you'll 42 00:03:29,820 --> 00:03:35,880 see that we've got the same errors, but this time when we look inside our console, you can see that there's 43 00:03:35,880 --> 00:03:42,040 a whole bunch of information that's packaged into this request which is being logged. 44 00:03:42,180 --> 00:03:48,780 And this is all of the information that's associated with the request that was made to our server. 45 00:03:48,870 --> 00:03:54,850 So, for example, you can see that my browser accepts the following languages, and these are the languages that 46 00:03:54,900 --> 00:04:01,180 I'm happy to read, for example simplified Chinese or English. 47 00:04:01,470 --> 00:04:08,400 You can also see other things such as what was the URL that I accessed when I triggered this request, 48 00:04:08,700 --> 00:04:14,800 and it's got so much more information all about that request that was made to the server. 49 00:04:15,000 --> 00:04:18,800 Now the second object here is the response. 50 00:04:18,839 --> 00:04:26,760 This is the response that outcome server can make when the request gets triggered at this home location. 51 00:04:26,760 --> 00:04:30,090 So currently our server isn't responding with anything, 52 00:04:30,330 --> 00:04:32,320 and that's why we're getting this error over here. 53 00:04:32,330 --> 00:04:36,220 In fact, Chrome actually tells you that localhost didn't send any data, 54 00:04:36,240 --> 00:04:40,390 it’s got an empty response. So let's change that. 55 00:04:40,500 --> 00:04:47,700 Let's actually give it a response. So we can tap into the response object, and we can use the send method 56 00:04:47,790 --> 00:04:49,520 to send a response. 57 00:04:49,520 --> 00:04:56,730 And we’re simply going to send back “Hello”. And now if I go onto the command line, I quit the previous server 58 00:04:56,730 --> 00:05:06,440 session, I've saved the new code, and I run my server again, this time when we head over to localhost:3000, 59 00:05:06,480 --> 00:05:10,050 you can see that we see the word ‘Hello’ in our browser. 60 00:05:10,350 --> 00:05:19,140 And that's because when we typed in localhost:3000, we specified a location of a server. 61 00:05:19,380 --> 00:05:27,400 So when we hit enter, the browser will go to that location and make a request to get some data back. 62 00:05:27,540 --> 00:05:35,940 And when that request gets made at that home location, then this callback gets triggered, and we send 63 00:05:36,210 --> 00:05:40,510 the browser a response, which is just the plain text of ‘Hello’. 64 00:05:40,860 --> 00:05:45,990 Now that gets sent back to our browser and it renders it on screen. 65 00:05:46,200 --> 00:05:48,770 So you don't have to just send plain text. 66 00:05:48,780 --> 00:05:50,450 You can actually send HTML. 67 00:05:50,460 --> 00:05:52,880 So let's try sending an h1 68 00:05:52,920 --> 00:05:56,330 in this case. Let’s say ‘Hello, world!’ 69 00:05:58,750 --> 00:06:04,450 And this is just h1 that's wrapped inside some quotation marks that we're going to send as our 70 00:06:04,450 --> 00:06:05,770 response. 71 00:06:05,770 --> 00:06:12,270 So now if I hit save, I quit my server again, and run my server with the new code. 72 00:06:12,370 --> 00:06:19,640 And now we go to localhost:3000, we get the h1 being sent back with ‘Hello, world!’ 73 00:06:19,690 --> 00:06:27,550 Now, in most cases, when people are working with Express and Node.js, you'll see these two parameters 74 00:06:27,550 --> 00:06:28,300 shortened. 75 00:06:28,450 --> 00:06:36,160 So, as we talked about before, instead of saying event you can say evt, or e, because you can specify 76 00:06:36,160 --> 00:06:38,290 whatever you want for these names. 77 00:06:38,380 --> 00:06:44,550 But usually what you'll see is req for request and res for response. 78 00:06:44,680 --> 00:06:47,880 And that just makes it short but understandable. 79 00:06:47,920 --> 00:06:54,040 And this is basically best practice for working with Express, and this is what you'll see out there when 80 00:06:54,040 --> 00:06:58,780 you find other people's code and they are creating their Express servers. 81 00:06:58,780 --> 00:07:05,170 Now there is quite a lot of new information in this lesson but a lot of it depends on the understanding 82 00:07:05,200 --> 00:07:07,650 of how callbacks work. 83 00:07:07,820 --> 00:07:15,670 And so if this is at all confusing, then I recommend you to take a look back at our previous module where 84 00:07:15,670 --> 00:07:19,880 we talked about Javascript callbacks and higher order functions. 85 00:07:19,930 --> 00:07:25,690 So I'll post a link to that in the resources of this lesson just so that you can remind yourself of 86 00:07:25,690 --> 00:07:34,000 how we're able to provide an input into this callback function and how this input can get sent back 87 00:07:34,000 --> 00:07:37,650 to us when this callback is triggered. 88 00:07:37,900 --> 00:07:44,590 And it's a really good idea to create this yourself, mess around with it and send different responses, 89 00:07:44,620 --> 00:07:53,190 or log the requests, and just make sure that you're comfortable with this syntax and how it works. 90 00:07:53,320 --> 00:07:59,350 Now in the next lesson, I'm going to show you how to create other routes other than just targeting the 91 00:07:59,350 --> 00:08:07,150 home page, so we can start creating a multi page web site really easily by adding more lines of code 92 00:08:07,480 --> 00:08:09,050 into our server. 93 00:08:09,310 --> 00:08:12,580 So for all of that and more, I’ll see you on the next lesson. 10062

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.