All language subtitles for 002 Creating Our First Server with Express_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,430 --> 00:00:05,120 All right. After all of this time talking about how amazing the back end is and how cool it is to have 2 00:00:05,190 --> 00:00:08,820 to build your own server, we’re finally actually going to do it this time. 3 00:00:09,000 --> 00:00:14,250 So, in this lesson, we're going to learn how to create a real server locally on our computer using 4 00:00:14,250 --> 00:00:16,219 Node.js and Express. 5 00:00:16,230 --> 00:00:17,510 Now first things first. 6 00:00:17,550 --> 00:00:21,780 I've got my Hyper Terminal open and I've got Atom open next to it as well. 7 00:00:21,810 --> 00:00:28,000 So the first thing I want to do is we're going to remove the intro-to-node project directory 8 00:00:28,020 --> 00:00:29,650 if you still have it there, 9 00:00:29,670 --> 00:00:32,220 so we start off with a clean slate. 10 00:00:32,460 --> 00:00:38,330 Now, the second thing is, I want you to notice that you can see the location I'm currently at is the tilde, 11 00:00:38,330 --> 00:00:41,900 so that means I'm in the home directory of my Mac book. 12 00:00:42,030 --> 00:00:48,600 So I want you to open up a new Hyper Terminal, or just navigate to the home directory, so that we're starting 13 00:00:48,600 --> 00:00:50,410 at the same position. 14 00:00:50,610 --> 00:00:53,070 And here comes a challenge. 15 00:00:53,130 --> 00:00:58,170 So, using what you learned in the last module, all about how to use the command line, 16 00:00:58,380 --> 00:01:04,190 I want you to complete this challenge by yourself and to create a new directory, 17 00:01:04,410 --> 00:01:10,760 and we're going to call it my-express-server, and then I want you to cd into that directory. 18 00:01:10,790 --> 00:01:18,180 Then inside that project folder, I want you to create a new file called server.js, and I want you 19 00:01:18,180 --> 00:01:19,910 to initialize npm. 20 00:01:20,010 --> 00:01:25,130 And when you are initializing npm, I want you to set the starting point as server.js. 21 00:01:25,290 --> 00:01:29,010 So pause the video now and see if you can complete that challenge. 22 00:01:31,510 --> 00:01:31,770 All right. 23 00:01:31,780 --> 00:01:37,370 So the first thing we had to do was to create a new directory, and I'm going to create that on my 24 00:01:37,370 --> 00:01:37,860 desktop. 25 00:01:38,050 --> 00:01:46,900 So let's just shrink that down, and I'm going to cd into desktop. Now that I'm on my desktop as indicated 26 00:01:46,900 --> 00:01:47,500 here, 27 00:01:47,530 --> 00:01:53,590 I'm going to create a new directory, and I do that of course using the mkdir keyword, 28 00:01:53,740 --> 00:01:57,600 and then I'm going to call it my-express-server. 29 00:01:58,330 --> 00:01:59,280 So bam. 30 00:01:59,290 --> 00:02:01,380 There it is. Created. 31 00:02:01,420 --> 00:02:09,740 Now I'm going to cd into my-express-server, and I'm going to create a new file here called 32 00:02:09,750 --> 00:02:10,340 server.js. 33 00:02:10,370 --> 00:02:13,260 So, to create a new file the keyword is, of course, touch. 34 00:02:13,330 --> 00:02:17,770 And then I create my server.js, which is the name of the file. 35 00:02:17,950 --> 00:02:22,420 So this is a brand new Javascript file that I've just created out of thin air. 36 00:02:22,420 --> 00:02:29,460 Now, while we’re inside my-express-server, this directory, we're going to initialize npm. 37 00:02:29,530 --> 00:02:32,960 So the keyword for that is npm init. 38 00:02:33,010 --> 00:02:34,930 And we're going to hit enter. 39 00:02:34,930 --> 00:02:40,310 And now we can go through the process of setting up our NPM. 40 00:02:40,360 --> 00:02:46,230 So the package name is going to be called my-express-server. That’s fine. Version is 1.0.0. 41 00:02:46,390 --> 00:02:54,270 Description, let's call it, “My first express server.” Entry point is server.js. 42 00:02:54,420 --> 00:02:56,790 That’s exactly what we want. 43 00:02:57,070 --> 00:03:03,640 And test command we’ll ignore. Git repository we’ll ignore. Keywords ignore. Author, we'll just add our own 44 00:03:03,640 --> 00:03:11,050 name. License. And now they've compiled our package.json, and let's just take a look at it. Looks 45 00:03:11,050 --> 00:03:11,790 fine to me. 46 00:03:11,800 --> 00:03:14,110 So let's hit enter for yes. 47 00:03:14,320 --> 00:03:19,230 And now we have our package.json and our server.js ready set up. 48 00:03:19,240 --> 00:03:24,940 So this is basically the process that you will go through every single time you create a new web development 49 00:03:24,940 --> 00:03:25,850 project. 50 00:03:25,870 --> 00:03:30,490 So it's good to run through it a few times just so that you get used to all the things that you have 51 00:03:30,490 --> 00:03:31,480 to do. 52 00:03:31,960 --> 00:03:32,250 All right. 53 00:03:32,260 --> 00:03:38,290 So now that I've done that, the next thing I'm going to do is I'm going to open up that project inside 54 00:03:38,320 --> 00:03:44,920 Atom. And the short cut to do that in the command line is simply, make sure that you're inside the directory 55 00:03:45,030 --> 00:03:48,500 that you want to add to Atom as the project folder, 56 00:03:48,760 --> 00:03:52,240 and then you're going to write atom . 57 00:03:52,480 --> 00:03:59,590 and now if you hit enter, it will open up and add your project folder to Atom on the left hand side, and you 58 00:03:59,590 --> 00:04:01,930 can get started coding up over here. 59 00:04:02,080 --> 00:04:07,810 So that's just a quick tip when you're using Atom and the command line. As you'll come to see, the command 60 00:04:07,810 --> 00:04:13,460 line is super duper powerful, and it is literally going to become your best friend. 61 00:04:13,540 --> 00:04:16,740 So the next step is to install 62 00:04:16,760 --> 00:04:22,250 Express. And what do we always do when we use something new? We check out its documentation. 63 00:04:22,360 --> 00:04:26,870 So the documentation for Express is on a web site called expressjs.com. 64 00:04:27,010 --> 00:04:32,560 And if you have a look at ‘Getting started’ and ‘Installing’, then you can see they've got a whole bunch of 65 00:04:32,560 --> 00:04:33,910 instructions for you. 66 00:04:33,970 --> 00:04:36,540 So we've already completed the first few steps. 67 00:04:36,580 --> 00:04:38,440 We've created a new directory. 68 00:04:38,440 --> 00:04:40,170 We've initialized NPM. 69 00:04:40,270 --> 00:04:44,770 We've set our entry point and we're now going to install 70 00:04:44,770 --> 00:04:45,640 Express. 71 00:04:45,880 --> 00:04:52,120 Now you can see that they've got this command ‘npm install express - - save’. Now, as they explain 72 00:04:52,120 --> 00:05:00,790 down here, what ‘ - - save’ basically does is it adds Express into your packages.json under the 73 00:05:00,790 --> 00:05:01,930 dependencies. 74 00:05:01,990 --> 00:05:08,950 Now it used to be that you had to do this manually by adding the ‘ - - save’, but as of Node 5 and 75 00:05:08,950 --> 00:05:14,950 above, which we're definitely using, we’re using node 10 in this course, way into the future, 76 00:05:15,100 --> 00:05:17,400 you don't actually need this flag, 77 00:05:17,440 --> 00:05:19,260 it’s done by default. 78 00:05:19,270 --> 00:05:27,610 So all we need to do is to say ‘npm install express’, and we can do that over here, making sure that we're 79 00:05:27,610 --> 00:05:35,260 inside, again, our directory that we're working with, and then we're going to say ‘npm install express’. 80 00:05:35,300 --> 00:05:41,350 Hit enter. It’s going to take a little while to download. It’s a relatively large package. And once it's done, it 81 00:05:41,350 --> 00:05:48,620 will add the Node modules that Express requires, as well as Express itself. 82 00:05:48,670 --> 00:05:55,330 And now, when you have a look inside your packages.json, you can see that we've got a new dependency 83 00:05:55,330 --> 00:05:57,510 added, which is Express. 84 00:05:57,520 --> 00:06:00,680 Now this is the current latest version of Express, 85 00:06:00,790 --> 00:06:06,300 but in the future when you’re taking this course, it might be 4.17 or 5.0, 86 00:06:06,430 --> 00:06:07,420 that’s completely fine. 87 00:06:07,420 --> 00:06:12,580 We're going to be working with the latest version, and this course will be up to date with the latest 88 00:06:12,640 --> 00:06:13,420 version. 89 00:06:13,690 --> 00:06:18,840 So I will be time traveling into the future and changing the course in case there are any issues. 90 00:06:18,910 --> 00:06:24,690 So rest assured, everything that you're watching, everything I'm telling you to do, is going to work. 91 00:06:24,700 --> 00:06:32,260 So now that we've installed Express, the next step is of course to require Express. So let's head over 92 00:06:32,290 --> 00:06:37,640 to ‘Getting started’ and going to the ‘Hello world example’. 93 00:06:37,870 --> 00:06:44,110 So we're going to create a new constant called express and we're going to require express. Now in this 94 00:06:44,110 --> 00:06:49,630 case they’re actually specifying a specific version, but we won’t. We’re just going to use the current latest 95 00:06:49,630 --> 00:06:50,220 version. 96 00:06:50,380 --> 00:06:53,020 So let's head over to our server.js. 97 00:06:53,020 --> 00:07:01,320 We're going to create a new const that's called express, and we're going to require that package “express”. 98 00:07:01,420 --> 00:07:07,330 Now, as always, if we want to use the keyword const without JSHint shouting at us, we're going to add 99 00:07:07,360 --> 00:07:12,760 a comment called ‘jshint esversion:6’ 100 00:07:13,000 --> 00:07:18,220 to make that warning go away and tell our linter that we are using 101 00:07:18,310 --> 00:07:19,640 ES6. 102 00:07:19,660 --> 00:07:27,430 All right. So now that we've required and incorporated Express into our file, the next step is to create 103 00:07:27,490 --> 00:07:34,810 a new constant called app, and this is simply a function that represents the Express module, and we bind 104 00:07:34,810 --> 00:07:36,650 that to the word app. 105 00:07:36,880 --> 00:07:44,980 So we now have a constant called app, which is set equal express, and you'll notice that in the wild, when 106 00:07:44,980 --> 00:07:52,390 you come across web sites built using Express, the word app is usually always used when you're referring 107 00:07:52,390 --> 00:07:55,310 to the Express module, like so, 108 00:07:55,570 --> 00:08:00,910 although you don't have to. You can call it anything you like. But this is just best practice, and it makes it 109 00:08:00,940 --> 00:08:04,360 easier for other people to understand what's going on in your code. 110 00:08:04,360 --> 00:08:10,930 So now that we've created our constant app, the next step is to use this app, and we're going to use one 111 00:08:10,930 --> 00:08:13,240 of its methods called listen. 112 00:08:13,570 --> 00:08:22,570 And this tells it to listen on a specific port for any HTTP requests that get sent to our server. 113 00:08:22,870 --> 00:08:29,090 So I'm going to choose the port 3000, and if we hit save, 114 00:08:29,140 --> 00:08:33,900 now we have literally just built our very first server. 115 00:08:34,120 --> 00:08:37,030 This is the bare bones of an Express server. 116 00:08:37,030 --> 00:08:44,030 Now let's go into Hyper and let's run this server by saying node server.js, 117 00:08:44,080 --> 00:08:50,320 hit enter, and you can see that not much is happening, other than the fact that it seems like our 118 00:08:50,320 --> 00:08:51,700 command line is hanging. 119 00:08:51,700 --> 00:08:58,270 We don't get our normal sort of prompt, right? Because normally, when we execute a command, we'll get something 120 00:08:58,270 --> 00:09:00,170 like this, ‘Angelas-Macbook-Pro: 121 00:09:00,190 --> 00:09:05,590 my-express-server angelayu’ and the prompt, which is of course that dollar sign. 122 00:09:05,620 --> 00:09:07,690 Now we don't get the prompt here. 123 00:09:07,690 --> 00:09:12,820 The reason is because it's listening on this port that we specified. 124 00:09:12,820 --> 00:09:17,270 Now a port is basically just a channel that we've tuned our server to. 125 00:09:17,500 --> 00:09:22,960 So, for example, sometimes I listen to the radio, and I tune my radio to listen to 104.9FM, 126 00:09:22,960 --> 00:09:28,780 and our server is just tuned into the channel 3000. 127 00:09:28,990 --> 00:09:35,050 Now not much is happening here, so let's stop our server, and we do that by going into the command line 128 00:09:35,410 --> 00:09:37,360 and hitting Control C. 129 00:09:37,660 --> 00:09:42,340 Now, you're going to get yourself out of a lot of hairy situations by using Control C. 130 00:09:42,370 --> 00:09:50,070 So now we've stopped our server and we're back on the prompt, so we're no longer running our server anymore. 131 00:09:50,380 --> 00:09:58,030 So if we wanted to be able to see when our port is set up and when our server is running, we can add 132 00:09:58,060 --> 00:10:00,030 a callback function to this method 133 00:10:00,030 --> 00:10:08,770 listen. So we can add an anonymous function, and we're going to callback using a console.log saying, “Server 134 00:10:08,770 --> 00:10:15,430 started on port 3000”, which is the one that we specified here. 135 00:10:15,430 --> 00:10:24,310 So now, if we hit save and we try to run our code again, node server, you can see now, instead of just hanging, 136 00:10:24,370 --> 00:10:29,830 it's telling us that server has started on port 3000. 137 00:10:29,830 --> 00:10:36,940 Now if we head over to that ports location, which is localhost:3000, because we're hosting our server 138 00:10:36,940 --> 00:10:44,530 locally, and after the colon we specify the port that our server is set up on, which is 3000, and you can 139 00:10:44,530 --> 00:10:49,210 see that we get this error, where it says, “Cannot GET /“. 140 00:10:49,210 --> 00:10:50,970 Now what does that mean? 141 00:10:51,160 --> 00:10:58,300 Well, it means that when our browser is trying to get in touch with our server on the port 3000, it's 142 00:10:58,300 --> 00:11:01,660 not able to get anything back. 143 00:11:01,660 --> 00:11:09,280 Now we have to figure out how can we write some code so that our server responds when a browser is making 144 00:11:09,280 --> 00:11:11,370 a request to our server. 145 00:11:11,560 --> 00:11:15,550 We have to send the browser some information, right, to display. 146 00:11:15,760 --> 00:11:17,350 So, in the next lesson, 147 00:11:17,350 --> 00:11:18,630 that’s what we're going to be learning about. 148 00:11:18,640 --> 00:11:25,950 We're going to learn about the request and response that we can provide when a browser makes a get request. 149 00:11:26,110 --> 00:11:29,110 So for all of that and more, I’ll see you on the next lesson. 15648

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