Placeholder Image

Subtitles section Play video

  • Hey, what's up, guys?

  • It's Pedro here from new cutter dot com.

  • And in this tutorial, we don't learn how to install know Js as well as learn how to actually run code using node Js.

  • So to get started, we're gonna actually have to download no jazz.

  • So if you go to Google and typed know Js download, you should get a webpage similar.

  • Tow this and I will be leaving a link in the description in case you can't find it.

  • So go ahead and download 8.11 point three.

  • Lt s now, once you download that, run it, install it, enjoy all that good stuff.

  • And within these tutorials, I'm gonna be using visual studio code.

  • And if you don't know what visual studio code is, it's an editor.

  • Now you can use whatever editor you want.

  • But the reason I'm using visual studio code is because it has an integrated command prompt.

  • So once you download these two things, I'm just gonna head over to mine desktop and let's create a new folder and I'm just going to say no J s tutorial, And now we're just gonna head over.

  • It's a visual studio code and you should have a welcome screen like this go to file open folder, and we're gonna open the folder that we just create it.

  • So I'm just gonna select this.

  • And now I'm just gonna hit control n Control and is gonna create a new file.

  • I'm gonna hit control s and we're gonna save this file.

  • And I'm just gonna say ap dot Js And this is gonna be a very, very simplistic ah Java script file, and we're just going to run it.

  • And this is just a test to see if you installed no jazz correctly.

  • So if I hit Consul, not log Hello world from No J s.

  • Now, if I save this and I'm actually gonna exit out of this But you saw that this just popped up on my screen.

  • If I go to terminal here is our command prompt.

  • So I'm just gonna exit this to show you how you could bring this up whenever you want to run code using the terminal.

  • So I'm just gonna close this.

  • I'm gonna hit control B.

  • I am going to go to view.

  • I'm going to go to integrate it terminal now by default visual studio code has it on power show.

  • So if I go up here, you can notice that mind says CMD.

  • So if I hit control shift P and type default, I could say select default show.

  • Now, I have three terminals installed on my computer.

  • You probably have power show, and you probably have command prompt.

  • And I also have get go ahead and click Command prompt.

  • Hit this plus button.

  • This is gonna bring up a new terminal.

  • So if I was to hit this Oh, bring up another cmd and this should say CMB so I'm just gonna remove for me.

  • I'm gonna remove one since I already have one open and now I'm gonna do is type node and then the name of the file.

  • So the name of the fire that we just create it was called app about J s.

  • And here we go.

  • We get output of hello world from Know Js Now let's say that you're not using visual studio code For whatever reason, you have your own editor of choice what you conduce issue hit the start menu type cmd and then go to command prompt and then you could see that we get something very similar to the integrated command prompt.

  • But we're in a different location.

  • So if I go to my desktop, open this folder, let's copy the path that this voters located.

  • So I'm just gonna hit control C to copy.

  • Head over to my command.

  • Prompt.

  • I'm just gonna type.

  • See the CD stands for change directory.

  • Oh, that means is hey, change folder.

  • So right now I am in a folder called User's Page from Mercado.

  • I need to be in this folder.

  • So now I'm just gonna click this go down to edit click paste and that's our path.

  • So now have I hit Enter.

  • You can see that our paths have changed And now if I type node ap not J Yes, you can see that we get a hello world from note So either way is fine If you want to use visual studio code and its integrated terminal you're more than welcome to If you're using a different editor and it doesn't have its own integrated terminal, you're more than welcome to use the command.

  • Prompt this tutorial.

  • We're gonna be talking about node modules all and no module is is basically a JavaScript file.

  • It's a way to separate concerns for application.

  • So, for example, let's say that I haven't app dot Js file.

  • And in this app that jazz file I have math, a bunch of a P I request and database calls instead of putting them all on one file.

  • What weakened do is separate each of those concerns into their own files.

  • So to get started, I'm just gonna hit control b mess a control n.

  • So we're gonna create a new file, and I'm just going to say control s and we're gonna save it as a tutorial, not J Yes, let's save that control B.

  • And now we could focus on this new file orders new module called vittorio dot Js.

  • So to get started, let's actually give our tutorial madre or something.

  • So I'm going to give it a function.

  • So I'm gonna say consummate and it's gonna have to parameters and always gonna do is add and return them so numb one plus number two Next.

  • What I want to end up doing is I want to expose this function to the outside world because right now, if I had control s and if I was to try to use dysfunction, so I'm just going to delete this.

  • So if I was just to say some one plus one and if we were to actually run this, you can see that we get an error or some is not defined.

  • So you might be saying, Why is some not defined?

  • I just to find it here.

  • Well, I need to expose this function for the outside world to use, So I need to let No, no, that this some function within my tutorial module is available to be used.

  • So in order to do that or I need to do is type module exports and pass it in some.

  • Like so now that this is available to be used, I need to tell my app dot Js fire where this some function is located.

  • So what I can do here is the clear variable.

  • So I'm just gonna say concerts, tutorial.

  • And now I'm just gonna use the word require, and then we're going to give it a path.

  • Now the path is dot Ford slashed.

  • So where did I get dot for its lash from dot Forward slash means that it's within the same exact path that this file is currently in.

  • Where at dot Js So if I was the hit control B, you can see that I that j s is on the same level as tutorial dot Js and that they're in the same folder called Know Js tutorial.

  • So if I hit control, be again, get rid of that and then I pass in the name of the file which is obviously tutorial dot Js.

  • Now you do not need to pass in the extension or you need to do is pass in the name of the file.

  • No, Js automatically assumes that it is a JavaScript file.

  • And now if I was the printout tutorial, So let's see what's inside it.

  • And now let's get rid of our some call.

  • We're gonna call some a little bit later.

  • I just want to show you what the variable to tour Gil has.

  • So if I was to save this and let's grow down, I'm gonna type c o s enter.

  • That's gonna clear the terminal.

  • And now I'm just gonna run node app and you can see that the tutorial now holds a function called some from our tutorial module.

  • So now if I want to actually use that function, I could say tutorial and pass in a one plus one and let's actually print out the results.

  • I'm just gonna say council about log it, Let's save it executed again and you can see that we get our desired output of two and that we are indeed importing from our tutorial module, the some function.

  • So now that we know how to do that, how will we import multiple stuff from modules?

  • So, for example, if we head back to our tutorial module, let's say I have multiple functions or multiple variables or even a class.

  • How do I export more than one thing?

  • Well, what you can do is, I can say, And just for the sake of example, that's add more stuff.

  • So I could say cons.

  • Hi.

  • And I'm just gonna set this the 3.14 and let's create a class and I'm gonna say some math object must give it a constructor, and I'm just gonna say object created.

  • Okay, so now I have some pie and some math object.

  • Now, I want all three of these to be exposed to the outside world.

  • So that at that J s, for example can require them and then use it.

  • So how do I do that?

  • Well, one way to do that is to add properties to the module exports object.

  • So, for example, I could say dot some and I'm just gonna copy this.

  • So now exports that sum is gonna have the some function exports, not.

  • Hi is gonna hold the value of 3.1 floor.

  • So I just said it equal to pi and some math object is gonna be modeled exports.

  • But some math project is gonna be equal to some math object.

  • Now, if I was to save this at overto app and let's just remove this for now, I want to print out what tutorial is holding once we require from our tutorial module.

  • So if I save this and now that's execute this, you can see that we're getting an object back.

  • Tutorial is now an object.

  • And now this object has the properties of some.

  • And it tells you that it's a function Hi.

  • The value of pi and some math object, and you can see that it's our class.

  • And now, if I want to execute this will be like accessing a normal object.

  • So, for example, we have tutorial and let's say we want to use the summer function.

  • And so just saying Tutorial and I'm passing the two numbers I could say tutorial some and passing the two numbers and I'm just gonna copy this.

  • Let's pace.

  • There's two more times.

  • And now if I want a print pi, I could say tutorial pie.

  • And with in here the same thing for the object I could say knew, Vittorio, that some math object and that's going to create a new instance of are some math object.

  • And now, if I was to save this and run it, you can see that executes as expected.

  • So tutorial some just us two pie has the value of 3.14 and you can see that our constructor gets executed.

  • Object created one more thing to show you before I wrap this up.

  • If this is an ugly Centex for you, for example, saying module that exports about some equals this module that exports that pie is a bit repetitive.

  • What we can do is instead of exporting each of these individually I am going to let's remove two of them.

  • And instead what we can do is export an object, a literal and tag along the properties.

  • So I could say some there's gonna be some pie is gonna be pie, and then some math object is going to be some math object.

  • And if I'm to save this and now execute it, we should get the same exact result.

  • Let's execute that.

  • You can see that it's working beautifully in this tutorial.

  • We're gonna be talking about the events module, particularly the events in middle class within the events module.

  • Now all the events module allows us to do is basically bring event driven programming to know Js So they get started.

  • We're gonna actually have to require the events module.

  • Now the events module is actually built in to know Js.

  • So I'm just going to say cons event and matter.

  • Now, this is going to be the class that we get from requiring from the events module.

  • I'm just gonna say equal require events.

  • Now from here, we're gonna have to create a new instance of the event a mentor, So I'm just going to say, const event emitter.

  • I was a new event.

  • A matter so from here, we're going to start getting into event driven programming.

  • Now that I have my event in metal object potentially attached a listener to it.

  • So I'm just going to say event in matter and I'm gonna call the method on now.

  • This meant that takes on two arguments.

  • One is the listener that you want to attach to this object.

  • So I'm just gonna call it tutorial for this example.

  • And the second argument is going to be the function that you want to execute when this tutorial event occurs.

  • So let's just pass in this function and all I'm going to say is confident Log editorial event has occurred.

  • So now if I save this and run it so I'm just gonna hit control as to save.

  • And I'm just going to say no to that.

  • You can see that nothing happens within our terminal Now.

  • Why is that?

  • Well, I have this object.

  • I've attached a listener called Vittorio, but I haven't emitted a tutorial event.

  • Remember, this cold here is on Lee going to be executed when a tutorial event occurs.

  • So now let's omit a tutorial event.

  • So I'm just gonna say event a matter.

  • And now I'm just gonna pass in tutorial as the first argument, and that's it.

  • So now if I was the hit control as to save and now execute this one more time