Placeholder Image

字幕表 動画を再生する

  • 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, you can see that our tutorial event gets triggered whenever we call the emit method.

  • So now what happens when we actually want this function to have parameters?

  • Well, I could give it parameters, so I could say num one number two and just for argument's sake.

  • So just a show on the consul we're gonna print out of some of numb one in them too.

  • So now how would I call it down here with the emit method?

  • Well, I'll just pass it in as an argument, like a pass in one and two.

  • So if I save this now and now, go to note app, you can see that we get the value of three.

  • So you just pass in the arguments now, this is all fine and dandy, but how would I create a custom object which takes advantage of using events?

  • Well, I could just inherit from the event a middle class so for example, let's say that I want to create a person class.

  • I said Class person and I could just extend from the event a middle class.

  • And now from here, let's give it a constructor.

  • Now, let's call our super function.

  • So I'm just gonna say super and this is going to allow us to use the this property within our class.

  • And now I'm just going to say this that on the score name is gonna be a signed value of name.

  • And now let's actually pass in name to our constructor.

  • And now let's give this person class a getter.

  • So I'm just going to say, get name and all of this is gonna do is just return this dot underscore name.

  • So now that we have our person class defined, let's actually create a new instance of it.

  • So I'm just going to go down here and I'm just going to say, Let Pedro equal new person unless pass in Pedro.

  • And now here.

  • Remember, when I say new person person extends the event a middle class, which means that Pedro is also an instance of the event a middle class.

  • So now I can add a listener to the pager object, so I could just say Pedro dot on.

  • And I'm just going to call it name and we're gonna pass in our function, and I'm just gonna print out.

  • My name is so and so So I'm just gonna say, Consul at Log, my name is and we're just going to say paid girl dot name.

  • So now that we've attached our listener, obviously we're gonna have to call the in MIT method.

  • So we're gonna admit the event has occurred.

  • So I'm just gonna say, Pedro, that emit, and we're gonna pass in name.

  • And now, if I was to save this and now execute this, you could see that we get the desired output of my name is pager.

  • So that's how you would extend using the event emitters class.

  • And for our final example, let's say that we have more than one person object, so I'm just gonna create a new person.

  • I'm just gonna call, Let's make it a girl.

  • I'm just gonna call her Christina.

  • I'm gonna say new person and we're gonna pass in Kristina.

  • Now let's add a listener to the Kristina object, so I'm just gonna say, Christina, that on name and you could use a loop for this.

  • But since I'm only creating to ah, person objects, it doesn't make sense for this example.

  • But you can use a loop to do this on one shot, and we're just gonna copy this based this in here.

  • I'm just gonna say Kristina, that name.

  • All right.

  • So why did I go through the bother of showing you that we created to person objects and attach listeners to it?

  • Well, what I want to show you is that when you e mitt and event to occur, the event gets executed synchronously.

  • So, for example, if I say Pedro and Asset Kristina And now, if I was to save this and now execute it, you can see that that these get executed synchronously.

  • So because Cato emitted, First Pedro gets executed first, and then our Christina object.

  • So they get executed in the order that they admit it in.

  • So they execute synchronously.

  • And this tutorial, we're gonna be talking about the reed line module.

  • Now, what the re line mantra allows us to do is prompt the user as well as get user input.

  • So to get started.

  • We're gonna have to require the module.

  • So I'm just going to say Constance Re Line is equal to require read line, and this is going to bring in our re line module.

  • Next, we're gonna have to create an instance of the reed line interface by using the create interface method.

  • So I'm just going to say, read line, not create interface, and within here is going to take an object.

  • Now we're going to give this object to properties, and this is basically our configuration file.

  • So the first property I'm gonna give it is input, and then we're going to give it an object.

  • Now, this object is called process, not process is a global object, so you don't have to require it.

  • It's already given to us by node.

  • And then here we're going to say STD in now, input is gonna be the STD in input stream.

  • And then here we obviously need an output.

  • So I'm just gonna say output and we're gonna give it process.

  • That s t D out.

  • And that's gonna stand for output stream.

  • Now when this mother is executed, is going to return our read line interface object So let's save it inside a variable so we can use it.

  • I'm just going to say Const Roo is equal to this.

  • Now let's say I want to ask the user what two numbers added together are equal to.

  • So for this example, I'm just gonna generate two random numbers between one and 10.

  • So I'm just going to say, let numb one equal math, the floor math dot random.

  • And we're gonna ties this by 10 and we're gonna shift it over by one.

  • So this is going to give us a random number between one.

  • And 10.

  • This copy this twice.

  • Pace that two.

  • And we need the answer.

  • So I'm just gonna say, Let answer equal number one plus none, too.

  • All right, so now that we have this, we're ready.

  • Tow?

  • Ask the user what thes two numbers are equal to.

  • Now, in order to do that, I'm gonna use our read line interface and are relying.

  • Interface has a method called question, so I'm going to say aro question.

  • Now, the first argument is gonna be a string.

  • The second argument is gonna be a function, and basically the first argument is going to be the question we would like to ask the user.

  • So I'm just gonna say, back Tick, what is numb?

  • One close number two question mark.

  • And I'm just gonna hit enter here because I want you guys to see this on one line, and we're gonna include our callback.

  • Now, this function is going to get the user input as a parameter.

  • So I'm just gonna say user input.

  • And for now, I'm just gonna print out user input onto the consul just so you guys can see that it's actually working.

  • So now if I was to save this head over to my consul, I've knowed app.

  • It's going to say What is four plus two?

  • I'm going to say banana.

  • And there you go.

  • It prints out the uses inputs.

  • It's important to note that if you take a look at our consul, the application is still running.

  • So I'm just gonna hit control C to break out of it.

  • And what we need to do is actually close the reed line interface when we're done using it.

  • So in order to do that, that's actually add a couple things.

  • First thing I want a new line right after this question mark here.

  • So I'm just gonna say for it slash And so when I prompt the user the uses input instead of lining up right next to it like so is gonna be underneath.

  • All right, so I fixed that problem.

  • Next thing I want to do is actually test the user input to see if he actually got the answer.

  • Correct.

  • So in order to do that, I'm just going to say if user input and it's always important to trim the user's input if you ever accept anything from a user just to get rid of the trailing white space and we're just going to say if the user's input is equal toe answer, So if they answer the math question correctly, I wanna close the application So I could say our Oh, that close.

  • And here and now if I was to save this and now execute the program.

  • So I'm just gonna write node app is going to say what is 10 plus four and I'm going to answer with 14 and you could see that we answered it correctly and therefore our application closed.

  • Now, when I want to do is actually add a listener to the clothes event.

  • Now, the reed line interface is actually an instance of the event.

  • A meter class.

  • So what?

  • We conduce this actually scroll down here and I'll add it towards the end.

  • And I want to say our own dot on, and basically, I'm going to say, Hey, I want you to listen for an event toe occur.

  • I'm gonna say close and we're gonna give it a call back.

  • And this function is on Lee goingto execute when we close the re line interface.

  • So now I'm just gonna say, Consul about law and basically I just want to let the user know that Hey, you got the correct answer.

  • So I'm just gonna say correct.

  • So now if I was to save this and now executed node app, what is six plus three?

  • I'm just gonna write nine.

  • You can see that correct gets printed out onto the council.

  • Now correct gets printed out onto the council because when we use the method close, it emits the clothes event.

  • And since we're listening for a closed event, this callback function gets executed.

  • And on that note, I'm better at a semicolon on there and let's see that.

  • So now that we handled what happens when the user inputs correctly?

  • What happens if the user gets the wrong answer?

  • So, in order to do that, let's create an L statement within our question method here.

  • So I'm just going to say else and what I want to do basically is re prompt the user.

  • So I'm just gonna say, are all that prompt that set prompt and Sepp prompt is basically going to taken argument of string and is goingto ask the user basically what you want to know?

  • So I'm going to say to the user Incorrect response.

  • Please try again.

  • Um, let's add a new line here and now, in order to use the prom, actually have to call it.

  • So the first method is to set the prom.

  • The second method is gonna be to call it I'm just gonna say are, uh, front.

  • So now I'm just gonna hit control as to save.

  • And now I'm just goingto execute this mode app, and he's gonna say, What is one plus five and I'm going to say banana and it's got, say, incorrect response.

  • Please try again.

  • Now, I actually haven't told it what to do once the user inputs.

  • So let's actually write the code for that now.

  • And what I'm gonna do is actually add a listener, too are read line interface.

  • So I'm going to listen for to use his input.

  • And the event for that is our O on line, and then we're gonna pass it in a call back.

  • Now, this callback is goingto have the user's input.

  • And now we're gonna decide what to do once we get the uses input.

  • So obviously, what we want to do is check if he uses input is correct.

  • And if it is correct, we are going to close the application.

  • And now what happens if it isn't correct?

  • We're going to say else, and then I'm gonna reset the problem.

  • So I'm just gonna say or, uh that set prom and I'm gonna say, let's actually make this more informative.

  • So I'm just gonna say your answer of and then we're just gonna print out what they wrote.

  • User input is incorrect, and we're gonna boot a ford slash.

  • And before that, I'm just going to say, try again.

  • So now let's end that and now, remember, once we set the prompt, may actually have to call it.

  • So now I'm just going to say or l dot prom.

  • And now if we just take a look at what we have so far, this is basically going to give us a loop.

  • So we set a line event listener.

  • Now, when the user enters input, that's going to trigger this line input we're gonna test the sea of what he gave us is correct.

  • And if it is, we're just gonna close the application.

  • And if we close the application, this on clothes method is going to execute is going to say correct.

  • So now let's see what happens if we give it the wrong value is going to execute the L statement is gonna set the prompt, your answer of so and so is wrong.

  • And then he's gonna prompt the user again.

  • And basically, this line event is going to keep executing until the user enters the correct input.

  • So let's actually take a look at this in action.

  • So I'm just going to clear the terminal and before execute, let me just say control as to see intro as safe and now I'm just gonna hit node app, and it's gonna say, What is one plus four?

  • I'm going to say banana.

  • Incorrect response.

  • Please try again.

  • I'm gonna take banana again.

  • Your answer.

  • Banana is not correct.

  • And you can see that this will go on indefinitely.

  • Now, if I go back up and let's actually see what it was what is one plus four?

  • I'm just gonna type in five, and you can see we get the output of correct and then exits the application and this tutorial.

  • We're gonna be talking about the file system module.

  • Now, what the file system module allows us to do is work with the file system.

  • And what I mean by that is that it allows us to create files, read files, the lead files, create folders, stuff like that.

  • So to get started, we're gonna have to require the five module into our project.

  • So I'm just gonna sit Concept fess is equal to require f s.

  • So now that we have the file system Madre within our note application, let's actually use it now.

  • If I had control B, you can see that all I have is my app that J s file within my know Js Tutorials folder.

  • So let's actually create a file.

  • So this one's gonna create a file, and we're going to say f s thought right file.

  • Now, the first argument is gonna be the name of the file.

  • So I'm just going to say example, that t x t The second argument is what you want to write to the file.

  • So I'm just going to say this is an example and the third argument is gonna be a callback, and this callback is gonna have a parameter of error in case anything goes wrong.

  • So I'm just gonna say air and their ego.

  • So that's actually finish writing this.

  • So I could say, if error, we're just gonna print out the error, Otherwise, we can say files successfully created.

  • So I'm just gonna write, file successfully create.

  • So now if I was to save this and let's run this node app, you can see that we get files successfully created, and now if I hit control B good not to be in the conflict.

  • When I do that control being now you can see that we have a newly creative file called example dot t X t And if we look in it, we see the data that rewrote to the file.

  • Now that we created this file, that's actually read it, let's open it up and read it.

  • So if I was to say control be again and I'm just gonna go toe at dot Js And within this else statement, let's put all this code within here and now I'm gonna say F s about read file and within here, the first argument, much like the right file method, is gonna be the name of the file that you want to read.

  • So I'm just going to say example that t x t The second argument is gonna be the encoding type.

  • Now I'm gonna leave this blank because I want to show you guys what happens when you don't leave and encoding type and in the third argument is gonna take a callback function.

  • And this callback function has two parameters, has error, and it has the file that you actually want to read Now, in here, actually want to show you guys what the file looks like?

  • So we're gonna do the same thing that we did in the right file method.

  • I'm gonna check if there's any errors.

  • And if there are no errors, I just want to print the file itself.

  • So now if I was to save this Gordon node app Now you see that the fire was successfully created, and then you see that when we print out the file, we get this buffer stream, and then we get this bunch of numbers following it.

  • Now, the buffer stream stores data in binary.

  • Okay, so what we need to do is specify the encoding type that we want it to be displayed as so up here, we're gonna go up here and I'm just gonna pass in u T f A.

  • So that's the encoding that I want.

  • And now, if I was to save this, go to note app, you can see that fire was successfully created and we're successfully reading the contents within the file.

  • All right, so now that we know how to create and read files, that's actually cover a few more methods.

  • So when I want to do is start with a fresh examples.

  • I'm just gonna comment this all out.

  • I'm just gonna highlight everything.

  • Control backslash, and that's gonna comment everything out for me.

  • Now what I want to do is let's say that when I created this example dot t x t follow that.

  • I really didn't mean to call it example dot t x t Let's say I wanted to call it ah example to, for instance Well, I could use the rename that thing so I could do something like this.

  • I can say f s dot rename and rename is gonna take in three arguments.

  • The first argument is gonna be example that t x t and that's gonna be the name of the file that we want to rename.

  • The second argument is gonna be what we want to rename the file, too.

  • So I'm just gonna say example, too t x t.

  • And the third argument is gonna be the callback function, which has a parameter called error, and we're gonna do the same thing as we've been doing for all these, uh, a synchronous cause we're just going to say, if error, we're just gonna print that out to the Consul If there's nowhere and everything went fine, we're gonna say successfully renamed the file, So I'm just gonna say, successfully renamed the file.

  • So now if I hit control as to save and now let's run this can see successfully renamed the file And it says the leader from this and that's probably because it hasn't updated yet.

  • So if I close this and I'm just gonna hit control B and you can see that within our package Explorer that our file has been renamed to example to not t x t.

  • And if I opened this is the same exact file that recreated couple more methods that I wanna go over.

  • So I'm just gonna hit control B.

  • And let's say that once I comment this out, let's say that when I was creating the file that I forgot to add something towards the end of the file.

  • So what do you want to do?

  • I want to append data to the file.

  • So I'm just gonna say F s that pen file is gonna take in three arguments.

  • It's gonna take in the name of the files.

  • So I'm just gonna say example to dot t x t.

  • The second argument is gonna be the data that you want to append to the file.

  • So I'm just gonna say some data being upended, and the third argument is gonna be the callback function.

  • And this is going to have an error as a parameter past this and And we're going to say if error rented out to the consul Otherwise, we're gonna print out that the successfully a pendant data to the fire so successfully upended data to file.

  • I'm just gonna move this over so you can see.

  • And now, if I save this and execute this so no dap and see successfully a pendant data to file.

  • So let's actually take a look at our file and you can see that our file has been changed.

  • So before we had, this is an example.

  • And then we have penned It's on data to the end of the file.

  • And the last method that I want to cover within this tutorial is how do I delete a fire?

  • So, for example, we did all of this and let's say that I've had enough with this while I just want it gone.

  • So let's just comment this out and this is gonna be the final method that I go over.

  • So we're just going to say f s not un ling.

  • And the first argument is obviously you guys should get this by now.

  • Gonna be example too.

  • Example to the T X T.

  • And the second argument is gonna be a callback function and had the parameter of error.

  • And with in here, if error printed out to the council, if there's no where we're just going to say successfully delete it, the fire successfully.

  • So now if I was to save this and now execute it, you can see successfully deleted the file.

  • So it's appeared deleted from this.

  • So if I close this and if I was the hit control be to bring up the package Explorer, you can see that the file no longer exist.

  • And this editorial is gonna be part two of my file system modules tutorial.

  • Now, in the previous tutorial, we went over using the file system module to work with files and this I want to go over how to work with folders.

  • So to get started, we're gonna have to require the file system module, so I'm just gonna say const f s equal require and then f s here.

  • All right.

  • Now what I want to do is I'm going to say f s dot em que dir.

  • What m Kader stands for is make directory, and our director is is a folder.

  • So now here is gonna take in a two arguments.

  • One is gonna be the folder name that you want, so I'm going to give it the value of tutorial, and then we're gonna pass in the callback with an error, and we're just going to say if error cancel dot log error.

  • Oh, wth we successfully created the folder sewn associate counsel log voter and successfully created.

  • So now if I was to save this and now before actually run this, I'm just gonna hit control B So you don't see that there's any magic happening.

  • So all I have is my know, Js tutorial folder and ap dot Js file.

  • So now if I go here type node app, you can see that we get the output of photos successfully created and that our tutorials voter has been created.

  • Now, let's say that actually want to delete this folder, so I'm just gonna hit control, be again, and this is actually going to give us an error.

  • But I'm just gonna show you what the air is is basically gonna complain that this voter really exists.

  • So I'm just going to say f s that are m and R M stands for it.

  • Remove and endure, and that stands for it.

  • Remove voter.

  • Now I'm going to give the folder name.

  • So we called it tutorial, and this is also going to take a call back, which gets an error.

  • So now if I say if error consul, about long at the aRer else we're gonna say, successfully deleted the folder successfully deleted the folder.

  • So now if I was the saved if and now execute it, you see that we get an error file already exists, and that's because the tutorials folder already exists, and we're trying to create another folder named the same thing.

  • So I'm just gonna head control B.

  • We're gonna delete the tutorial forwarder.

  • So I'm just gonna hit the league, confirm that control be again.

  • Let's execute this, and you can see that we successfully deleted the photo.

  • So if I go up here, hit control be you see that it doesn't exist.

  • So first thing we do is we create a folder called Tutorial and if this is successful.

  • Then we're gonna delete the folder called Tutorial and you could see about output that we successfully deleted the photo.

  • Now, this is too fast.

  • And that's why you wouldn't be able to see it if we had the package manager open.

  • So now that we know how to create and delete a folder, what I want to do now is create a folder and then create a file within that folder.

  • I'm just gonna hit control B, and we could leave this make directory.

  • So we're gonna make a photo call tutorial, and if it's successful, is gonna execute this house block.

  • So let's remove this.

  • And then within here, we're gonna create our new file.

  • So to do that, I'm just going to say f s that right file now, right file is gonna take in three arguments.

  • It's gonna take in the name of the fire.

  • But in this case, we've got to give it the full path.

  • So I'm gonna say dot for a slash And what 0.4 slash means is I want you to start relative toe at about J s.

  • And then I want you to go inside the folder called Vittorio.

  • So I'm just gonna hit control, be just to show you.

  • So at that Js starts here, there's gonna be a folder called Tutorials.

  • I'm gonna go inside that folder tutorial.

  • And now I want to you to create the file card example that t x t have a hit control be again.

  • And then the second argument is gonna be what?

  • The data that you want.

  • I'm just gonna write 123 Doesn't matter for this example.

  • And then the third argument is gonna be a callback function, which gets an error.

  • And then within here, I'm gonna write if error consulate out, log the air onto the consul.

  • Oh, we're gonna write successfully, create it, file successfully created file.

  • So now let's save this.

  • So I'm just gonna head control ass go down here node app, and you can see that says, successfully created file down here.

  • So now if I hit Control B and I'm gonna go up here and then hit control B, you can see that we have our newly created tutorial folder and within here we have our example that t x t So if we go in here, you could see, it has the data of 12 and three.

  • Now, what I want to show you is how would I delete a folder that has a fire within it?

  • So you might be saying, if I just go back here, hit control, be that Can I just use F s dot remove directory in order to do this?

  • Well, I am actually just gonna comment on other felt.

  • And what will end up happening if I use the remove directory function is that is that I'll get an error.

  • And the reason for that is because if I use the remove directory function, it has to be used on a directory or folder that is empty.

  • So just to show illustrate this that's going on right?

  • This quick remove directory function.

  • And if there's an area of print, it else, we're just gonna print the lead it folder.

  • So I'm just gonna hit control us to save, and I'm just gonna type c o s.

  • That clear to terminal No app.

  • And you can't see that we get an error directory not empty that we're trying to remove a directory that has a file within it.

  • So in order to fix that.

  • Let's delete the file first.

  • So I'm just going to say f s that unlike, and the first argument is gonna be the file, so remember dot forward slash and then we have to go into the tutorials border.

  • And then I named the fire example that t X t next argument is going to be a callback function, which has an error.

  • I'm just gonna say, if error printed out, if there's no error, then what?

  • I don't want to D'oh.

  • Well, if there's no error, that means that I successfully did Elliot the file, which means I can now successfully remove the file.

  • So I'm just gonna copy this, and I'm gonna put this within the else block here at some semi colon.

  • And now, if I was to save this and now execute it, you could see that we have deleted folder and you could see appear deleted from disk.

  • So if I go hit control B, you could see it at the folders been successfully deleted.

  • So what did we do?

  • First we deleted the file that was within the folder.

  • And then if that was successful, then I wanted to delete the photo itself.

  • Last example that I want to do is what happens when there are multiple files within a folder and you want delete all those files.

  • So I'm gonna cheat a little bit, and we're just gonna create it within our package Explorer.

  • So I'm just gonna say new folder and we're gonna call it example, and I'm just gonna give it to file.

  • So I'm just gonna say, a dot t x t.

  • And the second file is going to be be that t X t.

  • All right, so now I have my folder and I have my two files.

  • So how would I go about doing this?

  • Well, there's a method called Read directory.

  • And what Reed directory does is it gives you back the list of files that you have within that folder.

  • So let's actually call it.

  • So I'm just gonna comment on this out.

  • Comet this out and then I'm just gonna tight f s not read directory.

  • Now, the first argument is going to be obviously the name of their directory.

  • And in this case, we called it example.

  • And then the second argument is gonna be a callback function.

  • First parameter is gonna be error and then second parameter is gonna have files.

  • Now, files is gonna be Honore with the name of the file.

  • So now you could test if there's a mirror.

  • Well, we're gonna do is print out the error.

  • There's nowhere we're gonna print out files.

  • I'm just gonna print out files just to show you what files contains.

  • So if I say this, run this and the consul, you can see that we have our two files stored within honore a dot t x t and b dot t x t.

  • So now let's actually loot through this array and delete everything.

  • So now I'm just gonna say, four let file of files and then within here, we can start deleting each file individually, so I could say f s the unlinked, and then we're going to give it the file name.

  • So it's gonna be dot Ford slash example.

  • Then it's gonna be the name of the file after that is gonna take in an argument call back often era.

  • And if there was a problem the leading to have file, it's gonna print it out to the consul.

  • If there wasn't, we're just going to say successfully deleted file So and so, so, so successfully Delete it.

  • Five.

  • So now if I was to save this and now execute this, you can see successfully deleted file one's choice.

  • And if I hit control B, you can see that we have an empty folder called example, and this tutorial we're gonna be talking about the readable and rideable streams.

  • Now, what the streams allow us to do is basically be able to read and write data more efficiently.

  • And the way that they accomplish this is by reading and writing data and chunks, as opposed to reading and writing the data all at once.

  • So to get started, we're gonna have to require the F S Madre or to file system Madre o.

  • I'm just gonna say const f s is equal to require f s.

  • Now that we have our file system module included, we're gonna create a readable stream and there's a method called F s that create, reach, scream!

  • And now I need to give it the path.

  • So if I just hit control B, you can see that I have an example dot t x t file here and has a bunch of texts that we're gonna be reading in.

  • So if I had back to app that J s hit control be again, we're just gonna give it the path of dot for its lash Example that t x t and then that's it.

  • Now, this is going to return to us a readable stream.

  • So I'm just gonna save it within a variable.

  • I'm just gonna say cons retraining is equal F s start re Treem.

  • And then here what we can do is listen for an event.

  • Now, the reaching object inherits in the event a middle class.

  • So that means we could listen for events to occur.

  • And one event that we wanna listen for is the data event.

  • So I'm just gonna say, creates dreams, not on data, and then this is gonna take a callback function, and now we're going to get a chunk of data and return.

  • So every time we read a piece of data, this event data is gonna be invoked.

  • So this callback function is gonna be called every time we get a piece of data.

  • Now, I'm just gonna print out the data onto the council and you're going to see why I call it a chunk and not the full file in a second.

  • So now if I save this go to note, uh and you can see that we read this data within two chunks, so we have to print outs here.

  • We have a buffer here and a buffer here, so let's actually ah, set the encoding type.

  • So it's a lot easier to what?

  • The sources, and we're just gonna set it to ut f ate and save this.

  • And now let's rerun this application.

  • And there you go.

  • You see that?

  • We read our text file.

  • So what are the benefits of reading this and a chunk as opposed to loading the whole file?

  • Well, for one, when I get this chunk, I could immediately start manipulating that chunk.

  • So I don't have to wait for the whole file toe load in order to manipulate it.

  • Or let's say that I don't want to manipulate this trunk of data that I got already.

  • I want to send it to a new file.

  • Well, I could start sending that data to a new file while I'm reading in the data itself.

  • So let's actually send it to a new file, and for that we're gonna have to create a right herbal stream.

  • So I'm just gonna go here and I'm gonna say cons right stream is equal to f s that create right screen and within here is gonna be the name of the file that I want to write to.

  • So I'm just gonna call example to not t x t.

  • And now with in here what I conduce.

  • Oh, is, Instead of waiting to get the whole data, I could start writing chunks to the new file.

  • So if in here, instead of just printing out counseled out Log Chunk, I can say right stream dot right.

  • And I could write this data to a new file as I'm reading it ends.

  • I could say trunk here and let's say that.

  • And now if I run this application node up so enough I hit control B, you see our newly created file and if I go here, you can see that we wrote onto a new fire.

  • So basically what's happening here is I'm able to write Chunk by Chunk and our example two trunks.

  • In order to get the full file, I'm able to start writing before the file is fully loaded, and that's pretty much the power of using a stream in this tutorial.

  • I'm gonna show you guys why you should be using streams by trying to read a very, very large text file.

  • So to get started, I'm just gonna show you how big the file on this.

  • So I'm just gonna hit control B.

  • We have our large file dot t x t file here.

  • And if I click reveal an Explorer, right click properties.

  • You can see that the size.

  • It's about 2.7 gigs.

  • So let's actually take a look at what would happen if we were to use the reed file method from the file system module.

  • So now if I was to execute this and type node app, you see that we get an error.

  • And the era says file size is greater than the possible buffer.

  • Now what does that mean?

  • Well, the reed file uses a buffer, but it uses a full buffer.

  • Now, what that means is that I need at least 2.7 gigs in memory in orderto load this file because this file has to be loaded because this file has to fit within the buffer.

  • And it's saying, Hey, your buffer size is just too small for this file.

  • Now I'm just gonna comment this out And now what?

  • We're gonna uses a readable scream.

  • So I'm just gonna comment this.

  • Ah, and then we're gonna uncommon this.

  • And by the way, I have tutorials on both readable streams and using the reed file method.

  • So now I have the stream.

  • But look what happens when I execute the code now.

  • So if I was to save this and now execute it, you can see that are readable.

  • Stream has absolutely no problem reading this file.

  • So I'm just gonna hit control C to cancel this because I don't want it to finish, you know, loading the entire thing.

  • But why can this be accomplished with streams?

  • But not using this read file met then well, one.

  • A stream uses a buffer Awesome, but it doesn't use a full buffer.

  • Uses a very, very small buffer.

  • And what that allows us to do is basically what's happening is that you're ending out reading the file and chunks you're not reading the entire file at once.

  • So basically every time a data event occurs, I am getting a chunk of the fire.

  • I'm not attempting to read the entire file at once, and therefore the buffer size could be a lot smaller, as opposed to the reed found method, which uses a full buffer which needs to be big enough to load the entire file at once and then serve streams allow for a smaller buffer size, which is memory efficient and for data to be read and written and chunks.

  • And this tutorial, we're gonna be going over pipes.

  • Now what a pipe allows us to do is basic

Hey, what's up, guys?

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

Node.jsを学ぶ - 初心者のための完全なチュートリアル (Learn Node.js - Full Tutorial for Beginners)

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語