All language subtitles for 005 How to Use the Native Node Modules_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

Original subtitles

In the last lesson, we created a very basic node application and all our application did was to console

log a bit of text.

In this lesson I'm going to show you how to use native node modules that will allow you to leverage

existing code that's been built into Node that makes it way more powerful than just what you can do

with JavaScript.

What exactly are these native node modules?

Well, they're kind of like your starting tool set.

Now we mentioned before that Node had a whole bunch of features built into it just so that it was able

to make it easier for you to create applications, especially on the server side And the code that was

written in order to make this easier include things such as a file access, so reading and writing into

the local file system.

So that could be local on your computer or in the case of your web application, it will be onto the

server computer as well as things such as networking in order to reach across the internet and carry

out certain bits of functionality.

That's important for the back end of a web app and a lot more.

Essentially, you can think of these native modules as kind of like the games that come pre-bundled

with your operating system.

One of my favorite is Minesweeper on Windows.

It's sort of endlessly entertaining and don't really know why.

But also remember there was pinball and hearts and solitaire and you can of course install more games

onto your computer, but the native ones are the ones that came pre bundled because they thought maybe

they'd be useful to entertain you or to while away the hours like I did in my childhood.

Now in the course resources for this lesson so you can find that in the curriculum and there'll be a

dropdown for course resources.

You should find a link to this documentation page and this page documents all of the native modules.

Now there's a lot of them and we're not going to go through them each one by one because that would

take years.

But also that's what the documentation is for.

It's more like a dictionary and not a curriculum.

And some of these modules we're going to be using in the future, such as the Http, the Hypertext transfer

Protocol module and maybe the Path module.

But right now the one I want to show you is one of the most important and basic features of Node, and

this is the file system.

Remember with native JavaScript that is run in a browser, you can't access a user's files on their

computer.

Can you imagine if you go on to a website and somebody is able to read and write your local file system?

That is a recipe for disaster.

But in the case of NodeJS, because it liberates JavaScript out of the browser, we're able to use it

to access, to read and write to files on the server or in this case to our local computer.

Now you don't have to worry about it being malicious because you're going to be the one writing all

the code.

So what is the file system?

Well, the file system is the native node module that allows us to access the local storage.

And in order to start using it, we need to either import the code from the file system module or we

can require the bits of code that we need from this module.

In the next lesson, I'm going to talk to you a little bit more about ESM, but for now we're going

to use the Commonjs pattern in order to get hold of the methods that we need from this file system module.

I want you to download the starting files for this module, which is in a zipped folder called 2.2 Native

Modules, and you're going to need to extract that folder and open it inside vs code.

If you take a look inside the Index.js, you'll see it's yet another blank page.

But here is where we're going to get hold of that file system module.

So I'm going to create a const called FS and this is going to be set to equal require and the module

that we need has to be entered in as a string.

And you can see as soon as I put in those double quotes, it shows me all of the native node modules

that are available.

It knows exactly what I'm trying to do and the one we want is called FS, which is short for file system.

So now that we've gotten hold of this module, we're going to use one of the methods in that module.

And if we scroll up you can see that there are many, many methods available, but the one that we want

to use is called FS dot write file because.

This is going to allow us to take a message that say user inputs or from some bit of our website and

we're able to write it into a file to be saved onto the computer.

And in this case, it will be our local computer.

The method that we need in order to do this is called write file, and this is from the node file system

module.

And then in order to use this method, you can see the structure is like this.

The first is the file that we want to create.

Next is the data that we want to put in that file.

And finally we have the callback to handle any errors or any issues.

This is the example code for how this would work.

And again, notice how they've got the import format instead of the require format, which we're going

to go through very, very soon in the next lesson.

For now, don't worry too much about that.

We're going to use this write file method and we're going to pull it out of the FS module.

Heading back over here, we can type FS, dot write file and there we can access this method that we

need.

Now the file I'm going to create is called message dot text.

So it's going to be a text file.

And because I'm not putting in a full path here, it's going to create it in the same parent folder

as my index.js.

Now the next part I need to put in is the data.

So I'm going to just enter a string.

So let's use that previous sentence.

Hello from NodeJS.

And then finally we have our callback.

Our callback is pretty simple.

It can return an error and if there is an error, we can throw the error.

And if not, we can simply console log that the file has been saved.

You can either type this out or you can simply just copy everything from here to the end of that curly

brace and paste in that callback as our last parameter.

This is what it should look like.

And now if we hit save and we open up our terminal, let's CD over to this folder and remember that

your file path will look different depending on whether if you're on Windows or Mac and also depending

on how you organize your project structure.

So don't worry if this doesn't match, just make sure that you grab hold of the right folder in the

left hand pane here and now once you're inside that folder, we're going to use our node to run our

index.js and then let's hit enter and it tells us the file has been saved.

Success.

So there were no errors and we managed to get to our console log.

Now you can see that the message dot text file that we wanted to be created got created inside the same

folder and if we open it up, it says hello from NodeJS.

How cool is that?

So we've managed to use a native node module.

We didn't have to think about how to create these files, save it to the local system or any of that

business, because Node has created the code to enable us to do this functionality.

Now the next thing of course is a challenge for you.

Instead of just writing to file, you can also read from a file.

If you take a look at the FS dot read file documentation, I want you to use the example code along

with what we've written so far, and I want you to change the text inside here so maybe you can say

hello from Angela or whatever your name is and let's hit save on that message.

And coming back here, I want you to write the code that would read that file message dot text, take

the text inside it, and then console log it into the terminal.

That is your challenge.

Pause the video, take a look at the documentation and see if you can figure out how to do this.

Have a go now.

All right.

So the method that we need is called read file, and it takes a couple of options.

One is the path to the file that we want to read, and the next is the callback for what we want to

do once our file has been read.

Now, if you take a look at the example here in the callback, we can get either an error or the data

that was read from the file and we can use that data in a console log.

Pretty simple stuff.

Let's copy this example code and put it into our index.js in order to access that method, of course

we need to write f s dot read file and then we need to provide the path to this particular file now

because it's inside the same parent folder.

All we need to do is to write dot slash and then message dot text, and then the rest of the code is

pretty much already written for us.

Let's hit, save and make sure that you've modified the message dot text.

And now all we have to do is to comment out the right file.

We don't want this message replacing what we've got in here, so we don't want this to be run again.

And then we want to go into our terminal and use node to run our Index.js.

Now you can see we've got something outputted here, but it's in the format of a buffer.

Let's take a look at the documentation and see what's going on.

If we continue reading, it says if no encoding is specified.

So remember encoding is one of the options, then the raw buffer is returned.

But we can specify the type of encoding we want the output to be, and in our case we will want UTF

eight just to be able to output the text.

So let's modify our method and add in this extra option right in here.

So now we've got the location of our file, the output encoding of the contents of that file and finally

the console log and error handling.

Let's hit save and let's run our index.js again.

And you can see almost just like magic, it has read our file, including the text updates that we put

in there and it's console logged it right here into the terminal.

So hopefully you managed to get that right.

If not, take a look inside the solution dot JS to see if there are any differences between your code,

see if you've made any spelling errors or typos, that sort of stuff.

And once you're ready, head over to the next lesson where we're going to learn about the Node package

manager.

So for all of that and more, I'll see you there.

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