All language subtitles for 014 Using Modules 2_ Our Own Modules_Downloadly.ir_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,090 --> 00:00:02,770 So what I wanted to show you 2 00:00:02,770 --> 00:00:05,210 in this last video about the project 3 00:00:05,210 --> 00:00:08,360 is that we can actually create our own module 4 00:00:08,360 --> 00:00:12,137 and export something from them like for example a function. 5 00:00:12,137 --> 00:00:16,710 Then import this function into another module 6 00:00:16,710 --> 00:00:19,150 and then use that function there. 7 00:00:19,150 --> 00:00:22,653 Okay, and so that is what we're gonna do in this lecture. 8 00:00:24,490 --> 00:00:26,680 So let's say that we actually had 9 00:00:26,680 --> 00:00:28,816 a bunch of different JavaScript files 10 00:00:28,816 --> 00:00:32,613 in which we used this replace template function. 11 00:00:34,240 --> 00:00:37,710 So right now we're just using it here in index.js. 12 00:00:37,710 --> 00:00:41,240 We use it twice and that is why we have a function, 13 00:00:41,240 --> 00:00:43,062 but imagine if we wanted to use this function 14 00:00:43,062 --> 00:00:44,960 in multiple files. 15 00:00:44,960 --> 00:00:48,930 Okay, so what we can do is to create a new module 16 00:00:48,930 --> 00:00:50,790 and export that function from it 17 00:00:50,790 --> 00:00:53,210 and then import it back here. 18 00:00:53,210 --> 00:00:55,330 So, the first thing that you need to know 19 00:00:55,330 --> 00:00:56,900 is that in Nodejs 20 00:00:56,900 --> 00:01:01,420 actually every single file is treated as a module. 21 00:01:01,420 --> 00:01:06,210 And so this index.js here actually is also a module, 22 00:01:06,210 --> 00:01:08,780 which in this case imports other modules 23 00:01:08,780 --> 00:01:11,663 and particularly these three. 24 00:01:13,610 --> 00:01:16,090 Let's now create a new folder here, 25 00:01:16,090 --> 00:01:17,800 which I'm gonna call modules 26 00:01:20,120 --> 00:01:21,105 and then in here 27 00:01:21,105 --> 00:01:23,050 I'm gonna create a file 28 00:01:23,050 --> 00:01:26,707 which will be our module called replaceTemplate.js. 29 00:01:31,050 --> 00:01:34,793 Okay, so let's grab our function here. 30 00:01:37,220 --> 00:01:39,000 Actually, cut it out from here 31 00:01:40,730 --> 00:01:42,863 and simply paste it here. 32 00:01:44,420 --> 00:01:48,560 All right, now how do we actually export this function 33 00:01:48,560 --> 00:01:50,350 from this module? 34 00:01:50,350 --> 00:01:53,110 Well, there are different ways of exporting something 35 00:01:53,110 --> 00:01:54,166 from a module 36 00:01:54,166 --> 00:01:56,040 and we're gonna talk in depth 37 00:01:56,040 --> 00:01:59,013 about all of this in another section later on. 38 00:01:59,013 --> 00:01:59,904 But for now 39 00:01:59,904 --> 00:02:04,303 all we're gonna do is to use module.export. 40 00:02:08,020 --> 00:02:09,000 So in each module, 41 00:02:09,000 --> 00:02:12,950 we have access to a variable called module 42 00:02:12,950 --> 00:02:16,360 and on there we can set the export's property. 43 00:02:16,360 --> 00:02:20,230 And that we then set to whatever we wanna export. 44 00:02:20,230 --> 00:02:22,943 In this case, that is just this function. 45 00:02:24,130 --> 00:02:25,940 So we're gonna get rid of this name 46 00:02:27,660 --> 00:02:28,940 and so basically 47 00:02:28,940 --> 00:02:33,940 what we're gonna assign to this export property on module 48 00:02:34,151 --> 00:02:36,423 is simply this anonymous function. 49 00:02:37,700 --> 00:02:39,050 I said anonymous 50 00:02:39,050 --> 00:02:42,110 because right now this function does not have a name. 51 00:02:42,110 --> 00:02:44,184 And so it's just an anonymous function 52 00:02:44,184 --> 00:02:47,867 to be assigned to this export property 53 00:02:47,867 --> 00:02:50,750 on the module object. 54 00:02:50,750 --> 00:02:53,500 Again, that is an object that we have access to 55 00:02:53,500 --> 00:02:56,820 in each and every Nodejs module. 56 00:02:56,820 --> 00:02:58,250 And again, 57 00:02:58,250 --> 00:02:59,410 later on you will learn 58 00:02:59,410 --> 00:03:02,020 how that actually happens behind the scenes. 59 00:03:02,020 --> 00:03:02,853 But for now, 60 00:03:02,853 --> 00:03:04,050 let's just use it 61 00:03:04,050 --> 00:03:07,070 and don't worry about why it works this way. 62 00:03:07,070 --> 00:03:10,750 So this exports this function from the module. 63 00:03:10,750 --> 00:03:14,423 Let's just go to index.js and import it. 64 00:03:15,510 --> 00:03:19,410 And imports usually always happen at the top of the file 65 00:03:19,410 --> 00:03:21,400 and after the core modules. 66 00:03:21,400 --> 00:03:23,100 First, we have the core modules 67 00:03:23,100 --> 00:03:25,462 and then we have our own modules. 68 00:03:25,462 --> 00:03:30,462 We will do require and then our own module, 69 00:03:30,890 --> 00:03:32,640 which is dot... 70 00:03:32,640 --> 00:03:34,534 And remember in the require function, 71 00:03:34,534 --> 00:03:39,534 the dot actually means the current location of this module. 72 00:03:39,725 --> 00:03:43,660 So remember when we talked about the dir name variable here, 73 00:03:43,660 --> 00:03:48,330 which is usually the one that points to the module's folder. 74 00:03:48,330 --> 00:03:50,860 But in this case in the require function, 75 00:03:50,860 --> 00:03:52,678 remember there is an exception 76 00:03:52,678 --> 00:03:55,567 where this dot actually points to the location 77 00:03:55,567 --> 00:03:57,740 where this module is in. 78 00:03:57,740 --> 00:03:58,660 And so right now, 79 00:03:58,660 --> 00:04:00,960 that is this root folder here. 80 00:04:00,960 --> 00:04:04,410 And so from here we go to modules 81 00:04:05,660 --> 00:04:09,333 and then in there we have replaceTemplate. 82 00:04:12,480 --> 00:04:13,313 And again, 83 00:04:13,313 --> 00:04:15,220 just as with the other modules, 84 00:04:15,220 --> 00:04:17,192 we don't need the .js. 85 00:04:18,670 --> 00:04:22,700 Now that is gonna return what we exported here 86 00:04:22,700 --> 00:04:24,929 with module.exports. 87 00:04:24,929 --> 00:04:28,321 What we have here, this code, this function, 88 00:04:28,321 --> 00:04:33,321 is what will be exported from this require function call. 89 00:04:33,370 --> 00:04:35,620 And so we can now save that into any variable 90 00:04:35,620 --> 00:04:37,870 and give it a name that we want. 91 00:04:37,870 --> 00:04:40,460 So we could just call it X if we wanted 92 00:04:40,460 --> 00:04:42,850 or really whatever we wanted. 93 00:04:42,850 --> 00:04:45,960 But instead I am just giving it this name 94 00:04:45,960 --> 00:04:47,420 that we already had here 95 00:04:47,420 --> 00:04:50,670 so that I don't have to change my code here. 96 00:04:50,670 --> 00:04:53,770 So replaceTemplate is the name 97 00:04:53,770 --> 00:04:56,160 that I wanna give to this variable 98 00:04:56,160 --> 00:04:58,040 which is now this function. 99 00:04:58,040 --> 00:04:59,580 And just like this, 100 00:04:59,580 --> 00:05:01,900 we exported something from one module 101 00:05:01,900 --> 00:05:04,203 and imported it into another one. 102 00:05:05,689 --> 00:05:09,941 And so now if we restart our server, 103 00:05:09,941 --> 00:05:13,900 everything should still be working. 104 00:05:13,900 --> 00:05:15,000 But it actually does not. 105 00:05:15,000 --> 00:05:18,300 We have some kind of error here. 106 00:05:18,300 --> 00:05:20,920 And it says it cannot find the module. 107 00:05:20,920 --> 00:05:23,630 And that's because I was stupid enough 108 00:05:23,630 --> 00:05:27,150 to call this folder here modules.js. 109 00:05:27,150 --> 00:05:28,600 I am sorry for that. 110 00:05:28,600 --> 00:05:29,611 This one is of course 111 00:05:29,611 --> 00:05:32,363 only supposed to be called modules. 112 00:05:34,270 --> 00:05:37,350 And that's why it wasn't giving me the auto-complete here. 113 00:05:37,350 --> 00:05:39,194 I was wondering why that was. 114 00:05:39,194 --> 00:05:40,973 And now we know. 115 00:05:42,100 --> 00:05:43,540 If it doesn't work for you, 116 00:05:43,540 --> 00:05:45,119 just change the name here of this folder 117 00:05:45,119 --> 00:05:49,413 and then everything should match and work as it should. 118 00:05:50,730 --> 00:05:55,730 Let's reload and it still replaces everything as it should. 119 00:05:56,160 --> 00:05:57,716 The same here of course. 120 00:05:57,716 --> 00:06:02,716 And so yeah, we are now done with this project. 121 00:06:03,630 --> 00:06:06,380 Congratulations, well done. 122 00:06:06,380 --> 00:06:09,974 You are well on your way of learning Nodejs. 123 00:06:09,974 --> 00:06:11,500 Now in the rest of the section 124 00:06:11,500 --> 00:06:12,770 we're gonna talk a little bit 125 00:06:12,770 --> 00:06:16,510 about the node package manager or npm. 126 00:06:16,510 --> 00:06:18,380 And so we're gonna keep working a little bit 127 00:06:18,380 --> 00:06:19,770 with this project 128 00:06:19,770 --> 00:06:21,786 but not really adding any functionality. 129 00:06:21,786 --> 00:06:24,890 Anyway, see you in the next video 130 00:06:24,890 --> 00:06:27,973 where we're gonna start talking about npm. 9710

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