Placeholder Image

字幕列表 影片播放

  • everybody.

  • My name's Logan Huskins and I guess just a little bit about me before we get started.

  • I am a soft developer.

  • I work love travel Stop sign.

  • Been writing node at work pretty much exclusively for the last nearly three years.

  • Um, married.

  • My wife showed up.

  • That's pretty exciting.

  • Glad she gets to see me talk.

  • Um, I also helped run Oklahoma City in a Palm City mechanical keyboard user group.

  • So if you're interested, he would set me up, and we can I can show you where all that stuff that's pretty pretty, pretty dorky.

  • But I have, um So my plan with this talk is basically I'm gonna give some Hi, I'm really like high level concepts on some stuff.

  • And if it is complete and utter nonsense, as most of it will be initially I will circle back to pretty much every bullet point I have and kind of explain more in depth what those things are.

  • But if you have questions before that, if you have questions after that, just kind of wave at me, get my intention, I'm be happy to answer as many questions as I can.

  • So, um, we'll start off with what no Js is.

  • And this is pretty much right off the bat.

  • Gonna sound like nonsense.

  • And like I said, I'll circle back on everything.

  • No.

  • J S is a jobs for print.

  • Time built on top of Chrome's V engine is scalable.

  • Event driven has fast asynchronous io, which is input output.

  • It's generally used to make Web servers and developer tools, so first thing right off the bat is talking about V eight.

  • Um, if you haven't written before, you've probably not really heard much about V.

  • A Is the Java script engine inside of chrome that is used to execute execute the Java script you have on your website.

  • So if you ever have built a website with drama script, other using just plain JavaScript using angular react Js anything like that, Uh, when you run that application inside of a Web browser, the part that actually execute the drama script is V eight.

  • If you're using chrome, the cool thing about via is it's not actually tied to crow itself.

  • It can be embedded into any c++ application.

  • It's cross platform, so it runs on basically any operating system that you would expect a user have Windows, Mac OS, lynch, attributions, BSD, stuff like that.

  • The c++ application that it is used in that we're talking about is no.

  • So it is basically via outside of the browser and embedded into a separate run Time for Java script.

  • On top of that note provides a lot of libraries that aren't initially available n v eight and will run through a couple of those.

  • So I want to go through some quick examples to show exactly what that means.

  • Up until this point, what questions You guys have anything, All right.

  • First example.

  • We're just gonna do a simple hello world application.

  • Um, if you guys have ever programmed anything, I've sure you've seen this before, but this is just, um who on here doesn't have experience of jobs before I get started on this, they didn't have a lot of experiences.

  • Like any experience.

  • Okay, good.

  • So I won't explain what concert log is, but, um, we're gonna go ahead, just run that, and right there, it's printed out.

  • Hello, world.

  • So how we ran this application as we just call the note, execute a bowl and told it what script wanted to run, and then it'll run through that script, and without a browser, it will execute all of the drama.

  • This is a super exciting example, but it goes to show just the ability to run Java script without a Web browser.

  • So we'll go to a little bit more in depth example that shows some stuff that you couldn't necessarily doing chrome, and I'll go ahead and run through what this code does before I run it.

  • First thing it does is a prince Hello World for you guys again.

  • Then access is a global variable that's a part of no called process, which is something that's no specific and you don't you don't really do in the browser.

  • And, uh, it stores a lot of metadata about what application you're running and a lot of global thing going global things to it.

  • So if you have variables that you set outside of the application or something like that and we're accessing the arguments on it, so basically I want to call this application and pass the arguments, and then it's going to iterated through all those arguments and print out with the values of them all r and then a print were done when we're done.

  • So what it's done is iterated through all those arguments.

  • It actually took the executed all of note as the 1st 1 It took the script that we one and then all the things that I passed it from there on.

  • This shows that we can basically use things from our operating system that aren't they don't have to do with our browser so I can pass it anything I want to.

  • A lot of people will use us to do environment variables.

  • So if you have things like secrets that you don't want to actually storing your code passwords to service's database keys, stuff like that, they'll get past in and sort of a similar fashion, depending on how you set up your application.

  • What questions do you guys have about those before I go back to the flights?

  • Uh, well, so you you can pass 1/3 argument.

  • So for each return, the third argument, that is the actual right itself.

  • If you wanted to, you could check the length of that.

  • All right, but what for?

  • It does Is it liberates over there, Ray, and it stops at the last element of it, so you won't go over how many there are.

  • So it it takes all those arguments as an array and goes through each single one of those and performs this bit on each single one of those.

  • So you're not gonna go out of the bounds of that already or anything because of that?

  • And so, what you looking for?

  • Okay, you can write developer tools with no J s.

  • And there are lots of those will cover probably the biggest one, a little bit, a little bit called in p.

  • M.

  • But beyond those notice, primarily used to write Web servers, Web servers, for those of you that aren't familiar are basically how content is stored and served on the Internet.

  • It's where users, data, tweets, posts and all their information are stored.

  • So kind of an example of this is when you're say you're on Twitter and you tweet something when he when he hit tweet, it doesn't just stay on your computer.

  • It has to go somewhere that way when other people who are using Twitter and are following you on Twitter want to see your tweets, they can get them from somewhere.

  • So you send to what you hit tweet.

  • It sends it up to a Web server.

  • In this case, Twitter's Web servers and information is stored there for anybody later to come back and request.

  • So we're gonna kind of cover what something called the client server model is.

  • So I want to rent a couple examples of that, too.

  • Um, Web servers communicate with databases with file systems.

  • They communicate with other Web servers.

  • So if you have, if you ever heard of, like sequel server or my sequel or anything like that, they're running.

  • They're not running on your computer.

  • When you're running Web applications, they're running on a Web server somewhere.

  • Say, you have a, for instance, that you have a tweets that has a picture in it.

  • I don't know how exactly Twitter handles were images of how you could handle it is he could save it locally onto that Web server, which you can't, which means that anybody who makes a request to that Web server that Webster will then access the file system on the Web servants in the image down, so it has a centralized place for all of your tweets.

  • All the images you send up.

  • Basically, any information that's stored on Twitter is on their Web server.

  • It can.

  • Also, Web servers also communicate to other Web servers through networking protocols.

  • Like http, which is most of what we'll discuss today.

  • An example of that would be you send a tweet that has a link to a free code camp block post.

  • And when you see that sweet on Twitter, you'll see like usually like a little image that has to do with it.

  • The link A couple sentences about the link.

  • What happens is you said That's where it goes from your browser to a Web server to Twitter's Web servers.

  • Then Twitter's Web servers see there's only a minute and then make a network request to free code can slip servers so none of that happens on your computer.

  • It all happens on their server.

  • Web servers could be written a note written in Java, Ruby C.

  • Sharp and a host of other languages.

  • Today we're gonna be focusing on note what I just described to you guys, though it has a specific name, it's called the Client Server model, and it's a way to split responsibility between your Web server and the user's browser, where the Web server handles databases in her client communication and authentication.

  • The client handles you logic requests, info from other servers, stores, data in a database.

  • Um, for instance, with something like Facebook.

  • If you are on Facebook Messenger, you'd send a message to somebody on Facebook.

  • It wouldn't go directly to their phone.

  • It will go to Facebook servers, and then their phones would request that, or it will push it down to their phone, depending on how they set that up.

  • Do Does anybody have any questions about what Servers are kind of what the client server model is?

  • I can show you.

  • After this, I'm gonna show you some code of what a Web server like Looks like no questions.

  • All right, Before we get into that, actually, I'm gonna talk about why you would and wouldn't use No Jess.

  • And once again, this is gonna be nonsense until I kind of break it down and explain it step by step.

  • But no Js is good for asynchronous.

  • Not a second of applications that require non blocking input output.

  • And our event driven doesn't mean much.

  • I'll show you examples.

  • No, Jess is not good for CPU intensive tasks, so anything that requires a lot of math machine learning a I stuff like that note isn't very good at that because it doesn't have a way to handle it in a non blocking way.

  • And it's also good for non server applications.

  • This is not good for non server applications like game engines, mobile applications, stuff like that.

  • So, like I mentioned earlier when I mentioned uh, not blocking events and asynchronous I know what that means is so say you had a Web server that read a file from the file system.

  • Incentive back down to a client's browser.

  • Who is requesting it that say it was an image.

  • Sometimes reading files from files over takes time so you could have a very slow disk and it could take a couple seconds.

  • So person A, with their Web browser, makes the request for that image.

  • Person be with another Web browser, then makes a request to the same Web server, but they request something that's a lot quicker.

  • For instance, they want to get the information about a user.

  • They wantto look at the profile on Twitter and for some reason that takes longer than reading that image from the disc does for personnel.

  • So person A makes the request and because the input output and noticed non blocking, it basically pushes that off to the background by itself and continues that that discreet while person be can really make a request for that user information, pull it back down and it gets pushed back down to their computer just fine.

  • Then, when person A When the images done loading from the disk, it gets sent back down to personnel.

  • What what happened?

  • And something that was not was not non blocking her was blocking is one person a made the request for that image?

  • The server would stop everything until that image was read in from the disk and person be would be stuck waiting to see the profile information, even though that's a lot quicker than what person A is trying to do.

  • So they'd have to wait for person a to get done before they could do anything in.

  • That isn't good, because these things are running on your servers and their clients on completely separate computers, completely different parts of the world.

  • You don't want them to have to think.

  • Oh, this is going slow because person over here is doing something else like that.

  • That's not gonna I'm gonna fly with them.

  • Really?

  • Do we have any questions about not walking Io or when you would or wouldn't want to use node was an example of something that would be secret.

  • This okay?

  • The question was, what's an example of something that would be synchronised versus why just mentioned that something that was a synchronous So reading that image from your disk on your computer could be synchronous but no does it in an asynchronous away.

  • So what?

  • That what I mean by that is when you when you make that request for that image like I said, node pushes it off into the background, continues doing other things.

  • If it was synchronous and you could read, you can note you can actually read and you read some from the file system in a synchronous way, but a lot of what it's like default asynchronous.

  • If it was synchronised, it would stop everything else down the chain from happening so all their users, so the things that are in synchronous could also be synchronous it just depends on what you what you wanted to dio and with No, the synchronous stuff is kind of the default.

  • So anything that is a singer, this could also be synchronous.

  • What other questions do we have?

  • All right, I have more code examples.

  • These air actual web servers this time.

  • So the 1st 1 is gonna listen for I'm gonna show you two applications.

  • They're both pretty similar.

  • The 2nd 1 Just a little more in depth.

  • The 1st 1 just listens for and events on the server.

  • The 2nd 1 listens for specific types of events and does specific things for those.

  • All right, so I will run through this code first.

  • The first line of this code, um, is requiring in the http module bits of code and node that are provided by something other than your application other than the scripture in are called modules.

  • So you can run a module in store in your application?

  • Uh, no.

  • Provides a set of built in modules.

  • Http being one of them.

  • There are third party modules that you can install through tools that will discuss later called, We'll discuss later called in P.

  • M.

  • But this is how you require them.

  • But it basically gives you access to everything that's in on exported from this http module in the variable that I'd call it GDP.

  • So the first thing we'll do is create a new server.

  • Http has a function on it called Create Server and you give that function a call back.

  • And what that means is, any time anybody is make makes requests to the server, this highlighted code will execute, and I'll show you in a second what this highlighted could actually does.

  • We signed that server to the variable server because we do some with it down here.

  • Here we are, creating a port, and I wanna be ready.

  • Miss this locally with nothing else on my computer s o.

  • The example supports.

  • Won't you have to define a port?

  • But a port doesn't matter that much in this situation.

  • But what a poor is It is basically a way to tell us specifically what you're talking to on a computer.

  • So there's a bunch of there's a bunch of network imports open on my computer right now for different reasons.

  • So I had five note applications running on my computer.

  • They can all run on the same port.

  • So one around 8000.

  • Well, don't run on 1000 and 11 8002 and so on.

  • And you may request to that specific port.

  • That way, when you make a request, you know what what you're talking to.

  • And the note applications know which which of your service is needs to respond.

  • So the server, very polite to find up here.

  • When I created that server, I'm calling the dot Listen, function on it.

  • And what that tells you is it tells the server to begin listening for network requests against it.

  • I passed the port and then I passed out a call back.

  • So this code is executed when the server begins listening.

  • So the first thing Mission run and I run.

  • This is this console log right here saying the server started to listen on port and the port number.

  • So let's go ahead and run it.

  • We're gonna rent the same way.

  • No.

  • And then the name of the fire, which is events over, Doctor.

  • Yes.

  • So and there we go.

  • The first thing and it is a server started on Port 8000 and then nothing.

  • Application didn't finish.

  • Still, just sitting there open.

  • Well, what's going on?

  • What it's doing is is waiting for me to make network requests to the server.

  • And when I do make network requests, this bit of code is going to be executed.

  • So let's go ahead and make a network request to do this.

  • I'm gonna use a tool called Postman and everybody kind of see that light team.

  • Okay.

  • Is that better?

  • I don't You can't.

  • I haven't seen a weight of human plus man.

  • I will very explicitly talk about whatever things happening.

  • So I'm gonna type local host.

  • Local host is my my computer.

  • So this is where you would type the website you're talking to, like google dot com or whatever.

  • And just local host is my machine and then the port.

  • So I know what application to talk to.

  • I'm making a get request, which is an http method We'll talk a little bit about Http.

  • As we go through these examples get is one of the requests you can make through http, which is a networking protocol.

  • There's a bunch of other ones you don't use most of them.

  • Most of the time.

  • Use a couple of them.

  • I don't want to go ahead and send that request.

  • The response is a Jason object, which, if you're familiar with Java script, you're probably familiar.

  • Chase on.

  • But does anybody not no, not know what Jason Jason isn't would like me to go into that?

  • Awesome.

  • So what Jason is is a way to basically collect unorganized data.

  • So say you have a bunch of information that you want to send around between applications or sent to a user U S.

  • Senate and Jason's a good form to send that in.

  • Jason has a series of keys and values, and the values could be more Jason.

  • So the key in this instance is Message in the value is, Hello world.

  • You can have as many keys and values as you want, So if you are making a request for, like your profile on Twitter, it would have your user name and then the value would be your user name.

  • And then it would have your profile picture, and the value would be where you get your profile picture from, and stuff like this status.

  • I'm going to status in a second.

  • I'll show you the code first.

  • So what happened when we made the request of this server?

  • This?

  • Like I said, this code is what's being executed.

  • Any time a request is made in a note application, you dinner, you're gonna have access to two variables requests in response.

  • The request is the data that the client sends you and the response is the data.

  • You're sitting back to the client the first line of code.

  • We're taking this response and calling a function called right head.

  • And what that does is that writes, essentially metadata to the concept that you're sitting back to the client.

  • I'm sitting back a 200 which is an extra TV status Co 200 is the universal way an issue to be to say that went great.

  • Everything did exactly what was supposed to do.

  • Here is your information, and I'm writing a content type of application, Jason.

  • And all this is doing is it's telling postman or the browser or the user's mobile phone, or whoever's making this request that this is the type of data I'm sending back.

  • I'm sending back Jason.

  • Then I'm writing the actual data.

  • I'm sitting back to the response called by calling response not right.

  • I am called.

  • I'm sitting back that message.

  • Hello, world.

  • Back down to Postman.

  • And then I'm calling response on end, which sends the response back down to the client.

  • So we'll go back to Postman, and I'll show you where all of these things are showing up.

  • So this is the data that got sent back that explained earlier the message.

  • Hello, world.

  • This is that application, Jason, that I showed you being sent back.

  • The rest of it is stuff that notice adding itself.

  • And here's where it shows the 200.

  • Okay, there's lots of http.

  • Status codes you've probably seen four or four before.

  • Means a resource isn't found.

  • He might have seen 500 means their server, their server throughout, basically means something happened on their end.

  • And you just need to keep going about your business.

  • What questions do you guys have about this little web server?

  • Anything specific?

  • All right.

  • So I'm going to show you the more in depth example now.

  • So that server, what it did was it listen for any requests and responded to it the exact same way this one will handle different request a little bit differently.

  • So here's the main file.

  • The application.

  • It probably looks pretty familiar, except I require in an additional file.

  • So the first thing I require it is http, which is notation to model that would discuss a little bit.

  • The second thing I requiring is a file called a router router is a file that is actually inside my application.

  • So it's something that I've written made a module, and then I am exporting to be imported elsewhere.

  • That Ratter file could be used in thousands of other files, but we're only really gonna be using it for this one.

  • Uh, same thing here.

  • I'm calling http that create server.

  • It's doing something a little bit different on the request, but I go into that in a second port 8000 same deal calling server dot listen, and I'm saying the service started as listening on Port 8000.

  • So let's go ahead and start it up.

  • Starting at the same way.

  • Note in the file name server started on Port 8000.

  • So what is this doing different?

  • This honor quest is calling my router function on giving it a variable that is on the request that the user sends them.

  • So I spoke a little bit earlier about this.

  • Get up here.

  • There's also post put delete.

  • It's taking this and it's performing a different action based on what kind of issue to be request is being made.

  • And then I'm passing that function that's being called the requesting the response so it can perform operations on it.

  • The over to my router file the only two things that I'm actually going to perform anything on just for sake of brevity is it get request in a post request.

  • So to explain the difference a little bit, a get request?

  • Is a client being phone Web browser mobile app saying, Can I get this information?

  • A Post request is a little bit different.

  • A post request is you as a client saying, Go ahead and create this information on the server.

  • So back to the Twitter example.

  • When you tweet something and you you click tweet or you click, send it, post it to the server and says, Hey, here's a tweet.

  • Save this stuff or when a new users created your application using Hey, here's a new user.

  • Save this stuff that's what a Post request ISS.

  • So let's go ahead and do that.

  • Get to get request.

  • I'm gonna do it a little bit different this time just to show some different examples.

  • I'm going to add something to it called a quick perimeter.

  • So at the end of the Ural, I'm going to add some data.

  • When Adnan, I'm gonna say name is Logan.

  • So that's my name.

  • Uh, this could be any information, but it's the it's it's it's going to its there, basically to narrow down the stuff you get back.

  • We're not going into all that logic right now.

  • I just kind of want to show you an example of there being more information on your l on the server handling that information, responding that information in a specific way.

  • So I'm gonna go ahead and send it, and here we've got a Jason response back in the same way.

  • Um, it says message.

  • Thank you for getting is a different message than it was last time.

  • It's a different server doing different logic, and it returns the query arguments I sent name Nichols Logan.

  • Normally, you wouldn't just have a server that sent back the data that the client sent up to it.

  • That doesn't make a lot of sense.

  • I just kind of want to show how the plumbing of node works and how how Web Server has access to this information of what use it all is.

  • And once again, status is 200.

  • What questions do you guys have a pin to this point, All right, don't hesitate to ask questions.

  • Guys like questions, questions of good, Because if you're not asking questions, that makes me think I said everything perfect and explained everything perfect.

  • And I know that's not true.

  • All right, so here is to get request because we're over here router, not method method is going to get.

  • So it's going execute this coat being passed.

  • Their question response.

  • The only thing we're doing a little bit differently here is we're taking.

  • We're trying to discover what that those query values are.

  • We're using a model called the Earl, which is a built in module to the note that's being required up here.

  • Oh, something happen Cool.

  • Using a laptop mouse, stuff gets dragged around sometimes, so you're all has a method on it called parse.

  • You pass, parse the girl you want to parse.

  • And what part's does that breaks apart the Earl into usable chunks of information?

  • So you don't have to take that big, long string of data and take it and find the information you want on it?

  • This will give it to you in a way that's a lot easier to consuming a lot easier to handle requests has a method on there, has a property on a call to your l remember that request is what the client is sending you the Web server.

  • So it takes us your horses, it.

  • And then there's a parameter on it called Query being this quarry arguments.

  • And I'm getting that primer often assigning a query.

  • And this is gonna be little bit more of the same response.

  • That right head I'm writing a 200 saying everything was successful in the that Jason is a type of data I'm sending back to the client.

  • And then I'm writing the message, which I'm saying thanks for getting.

  • And then I'm putting that query back in that thing that I'm sitting back down to the client calling in, which sends it back down.

  • It was the question is is the mind is the module that I'm exporting right here.

  • Dictionary functions, and that's exactly what it is.

  • It's basically a dictionary or if you, these other languages.

  • They're called associative arrays.

  • Dictionaries are usually they're called a python in a couple of the languages.

  • Because Jonah's car passes called First Class Functions, The values in these dictionaries are functions.

  • So when you when you access things in addition, you can pass it stuff and then perform the logic on it that answer your question.

  • Awesome.

  • I guess it's cut back a little bit.

  • This.

  • Like I said, this is a module modules dot module exports is how you export the code as a module to be consumed by other stuff.

  • So how wanted information to not be available to anybody else?

  • But I wanted some of it to be available.

  • I could do code here, and it's now being exported.

  • It's one of the few ways of Java script offers encapsulation if anybody knows what that is, so now we're going to talk about the post request, which is a little bit more complicated.

  • Luckily, you probably won't ever actually do this.

  • There's a lot of there's a lot of note libraries.

  • That's, um, that distract this for you.

  • So you don't have to get down and dirty reading this date in like this, but I'm gonna kind of show you what it looks like.

  • And if you don't understand how some of the data is being read and it's not a huge deal, like I said, you're not gonna do this right off the bat.

  • So I want to go ahead and make that post.

  • I've actually got it right here, so don't type it all out.

  • So I've got it set to post here, which means hey, which means a client saying, Hey, server, go ahead and create this data and save it.

  • However you need to save it.

  • I'm telling the server right here in a header that the constant type it's gonna be a basically a form Um, this is the content type you usually use If you're filling out a form on a website, remember, this content type better is the same thing that the server used to tell the client that they're sending back down.

  • Jason.

  • So you're telling it you're sending your sending up this form and it will probably sent back down.

  • Jason, this isn't gonna stand down day Some of the other examples and then I'm sending out a body.

  • And what that body is is the data that I wanted to create.

  • So I gave it a name and I gave it address.

  • Um, this ever could require any any combination of information in this body.

  • But this is just for the example.

  • What?

  • What?

  • We're sending name being Logan and address being 123 Note.

  • Elaine, any questions about this post I'm about to make to the server?

  • All right.

  • It is It is not in Jason.

  • Um, you can send data up in Jason.

  • Um, how I don't do a ton of front and work.

  • How I think the formula encoded content type works is I think it's gonna send it to the euro.

  • But you could set up a considerable XML.

  • You could send up a cz a bunch of stuff.

  • I'm sorry if that information was wrong.

  • Um, so we've got the post right here.

  • So how this data is gonna come in is gonna come in in chunks, basically, So as the client is making the issue tuber http request to your server.

  • It's coming in.

  • That is one big chunk of data over the network that would make a lot of sense.

  • Would not make a lot of sense because it could be a huge request.

  • It's coming in a little tiny bits, so it's coming in part a part.

  • Be part see, and then we'll let my server know in the last chunk of data that it's the last one.

  • So I am creating an array cold body, and this is where we'll be putting the data that's being sent up to the server.

  • Here's where I'm registering events on the request object.

  • Who is this is saying is anytime this event happens on this request time this thing happens, execute the code that's given.

  • So the 1st 1 is data.

  • The data event is when one of those chunks arrives from the client to the server.

  • So this will happen.

  • Could be hundreds of times depending on how big the request is and this is end.

  • This is saying, execute this code when the last chunk of data has arrived to the server.

  • So all we're doing when this chunks come in is we're pushing them into the body area.

  • Now.

  • This date in the body array isn't exactly usable for us yet because of the way that actually request comes in.

  • But we will clean the state up and make it make it usable for purposes.

  • So here's where things get a little bit hairy.

  • And if you don't don't get all of this is not a huge deal because you probably won't actually do this.

  • Um, this is just I want to show that notice.

  • Kind of a job script.

  • A higher level language, but Node provides some lower level.

  • It doesn't provide a high level obstructions, as you might be used to their stuff built on top of note that provides those distractions.

  • But no, it has some kind of lower level stuff in it.

  • So here's query string.

  • I required it up here.

  • It's another module note provides, and I'm telling it, the parse this data.

  • So what?

  • That array is from the data.

  • It's going from a client.

  • It's called a buffer, which is a type of data.

  • Are type a bit uptight basically.

  • So basically I'm telling that buffer to do is take that big array contaminated into one long buffer and then change it to a string.

  • There's lots of good information on the nodes and no documentation about what a buffer is and how buffers work with http requests kind of outside the scope of this.

  • But if you want to get into that, it's pretty interesting.

  • So that's gonna be the kind of data that queer shit barks when it's all strings.

  • So it puts out, took that big buffer, makes it one long buffer and makes a buffer into a string.

  • And the query string library pour ce is that?

  • And then I signed that to a variable called Parcel Post Data.

  • All right down here.

  • Um, because generally when you post up to a date is post up to a server is gonna get saved in a database of some sort.

  • I made a little a little file code depository.

  • It doesn't actually do much because I don't need to set up a database to make this example, but we're gonna pretend that we save, but the safe function here saves the data.

  • But right now all it does is say that it's been saved to a database in the real world.

  • This module could there could be a risk.

  • For instance, if you're saving something to a database and you don't have, you don't have.

  • You can't make a connection to that database for some reason or the database is down or there's something wrong with the data this could throw.

  • It's called in air.

  • We're gonna This won't actually thrown Erica's.

  • I don't have a set up to, but we're gonna pretend it could.

  • Which is one of the good thing about modules.

  • Modules in this situation allow you to not know what's going on inside of a model, but still call the stuff in the module.

  • So I don't know that this isn't throwing error.

  • I just know that that it should be saving the data.

  • So if there wasn't air, I need to wrap it.

  • Let's go.

  • Try, catch, block Try.

  • It means excuse all of the code in the tri and of the code in the Tri has an air stop executing it and performance in the catch.

  • Disco will never get never get access.

  • But if it did, um, here's how it works.

  • Basically, I create a variable called response code.

  • I want to send back a two a one down to the client to save successful.

  • What a two a one is like a 200.

  • It's basically saying everything is okay.

  • But in the constants of creating that in the context of creating data, it basically just means date has been created.

  • Or if there's an air, I want to log it out and I want to say, 500.

  • There's been a surveyor, so it calls repositories, not save and passes of the data, because that thing doesn't actually do anything.

  • It succeeds.

  • And I said the response Go to 200.

  • It writes the response.

  • Go to the response, and then it ends a request, which means it sends it back down to the client.

  • So let's go ahead and do this.

  • And here we go.

  • Response goes to a one created, and I don't send any data back because there's no reason to really with a post, you're just creating the data.

  • You don't need a data back.

  • You already got the data.

  • All you need to know is if it was successful and down here, we said we saved the status of the database.

  • All right, what questions you guys have about these No questions All right, so let's talk a little bit about in P M.

  • Once again, this is one of those situations where I say much nonsense and then circle back to what each of them mean when I'm done in.

  • P M.

  • Is a package manager in a registry for note, and he was also a CLI tool to manage the packages on your machine, which is that package manager.

  • It allows users and enterprises to share and use over 600,000 packages.

  • So there's a lot of code on a P M.

  • That's not yours.

  • You have asked a lot of it.

  • Not all of it's great.

  • A lot of it's very, very, very, very good and pretty much indispensable to the node ecosystem.

  • So let's talk about what a package manager in a registry.

  • Our package manager is a way of managing code.

  • Your project depends on makes it easy to incorporate new code into a project and helps maintain consistency between installations of your project.

  • So say you have a dependency on a library that somebody else wrote.

  • A package manager makes it easy to incorporate that library and bring out library into your project.

  • It creates a way for other people who are using the project to know what libraries your code is using.

  • So that way, when you're developing something and then your friend takes over and starts developing it or your co worker takes over and start developing it, they have a way to reinstall all those packages that you're using.

  • When your code is done and you're ready to deploy it, the server knows where to get all those packages from.

  • And all of that is because of in P M package.

  • Registry makes it trivial to share the coding Britain and easy to access others code others have shared.

  • So the package manager lets you handle those packages on your computer, download them installed on everything package registry is a place where you can upload code for other people to use.

  • So use the manager to access the registry.

  • Basically, does anybody have any questions about in P.

  • M?

  • I'm gonna go into some examples of how this all works and set up a project using in P.

  • M.

  • In just a moment, all rights.

  • So we're gonna create a new director here, call it tests in P.

  • M.

  • I did not create the right place.

  • So how you create a new project and say this is the product is gonna be that's gonna use in p m either to be uploaded to in PM's website later for other people to use or for you to download other people's packages to use on your application.

  • You type in P M.

  • Minutes.

  • And this is an example of a developer tool that was written a note.

  • So it's gonna ask a couple questions for how I want to set up the coat, how they want us out of the project.

  • And then I'll show you where the settings are stored.

  • Gonna ask me the package name.

  • I'm gonna leave it.

  • The fault is called Testing PM.

  • Version 1.0, description.

  • A little test in P.

  • M.

  • Thing.

  • The entry point is the main application The main, the main file on your application.

  • I'm gonna create an index dot Js for this.

  • A test command.

  • This is if you have automated software tests we want for this example get depository.

  • There's none for this, But if there was, you put the get hub.

  • You are all here.

  • If you're familiar with cab keywords This is where you could type if there's like a library that handled managing dates or something, managing like daytime shrinks.

  • You put dates here, but this isn't gonna do anything.

  • So we're gonna put anything here either.

  • Author, Huskins license is gonna be a mighty Don't care what anybody does with it.

  • Oh, that's okay.

  • So what this did was it created a file called Package Jason.

  • This basically has all of the settings that I just specified.

  • Name version description.

  • The main file scripts, which we will go into death author and then the license.

  • We actually don't have a test script for this.

  • So we're going Thio, make another comment scrip.

  • You'll see what a start.

  • I'm gonna call node Index.

  • Dodge s.

  • Remember earlier how I whenever I run the project, I was typing in note and then the file.

  • Now I'll be able to type in in P m start, and it will do that for me.

  • So let's stay in the file called index R J s.

  • I'll show you why that's useful.

  • So we're gonna go ahead run in P M start and Iran.

  • Hello.

  • This is we'd expect my writing Hello.

  • In a consul.

  • So why, This is useful as I want to take that longer example with all the command line arguments and copy into this file.

  • And remember how, before I had type in node and in the name of file and that type hello and then my name and then free code camp, I could do all that in my scripts.

  • So now when I run it, it executes all of them, just like we had before.

  • But I don't have to type in everything every single time I do.

  • This is useful because this make this part.

  • When you have a big production application can get really complicated.

  • You can have scripts that run before your application starts up.

  • You can make sure your databases in a good state, you can have lots of variables that your application needs that are provided from here a lot.

  • You can do lots of stuff.

  • You could also have scripts that generate documentation for your application, scripts that run database migrations, anything of that nature.

  • So now I'm gonna show an example of us installing a package through in P.

  • M.

  • Then we'll wrap it up and I'll take questions so to install a package from in P.

  • M.

  • You can actually Ms show you in PM's website where all the packages are.

  • So this is the PM's website and there are 600,000 packages, give or take on here.

  • Popular example is a package called low dash load ash provides a lot of little utilities for you to use in your code.

  • It's kind of a generic set of functions that do stuff that she don't wanna have to implement yourself because they could be kind of complicated and miserable.

  • So we're going to install Low Dash in our application.

  • How we're gonna do that.

  • His example.

  • Have an example of right here, for instance, You go see how popular that stuff is that people use it.

  • Load.

  • Ash has been downloaded almost 15 million times this week.

  • That's a lot.

  • That's just this week.

  • I'm probably responsible for five or 6000 those we run a lot of continuous integration.

  • That might not have been an exaggeration.

  • It might actually be a couple 1000.

  • Um, so we're gonna stall in P m.

  • Install cashed s safe load.

  • Ash, make sure you spell what you're installing, right, because their stuff on in P.

  • M.

  • That looks like other stuff.

  • So make sure you're actually looking at what you're installing when you install it so you don't sell something else is code that acts like low dash but isn't actually low dash, so we'll install it.

  • It's gonna contact in PM's registry to get that.

  • Get that data.

  • Look, we now have a dependency list and tells us Load, ash and what version of Low Dash were using.

  • So now in our application, I'm gonna show you how to use load after what load ashes.

  • But it's just a popular example.

  • So here we've required it in like we had everything else.

  • Everything else that we had from the note standard library stuff, the stuff that I've written myself a lot of people will name the variable load ash and underscore uh, you could name this you could name is Low Dash or whatever else you wanted to.

  • But most people just call underscore.

  • So if you ever see anybody using underscored not something and JavaScript there either using load after underscore probably to do stuff and there's like I said, there's there's tons of other packages on in P m that abstract away.

  • A lot of the nasty stuff that I was doing in that post request One popular one is called Express.

  • I use express every single day.

  • It's got about $5 million on in P m this week.

  • Express makes it super easy to do those gets and those posts without having to get down and dirty with that data.

  • All right, what questions you guys have about P m.

  • With him?

  • Yes.

  • Yes.

  • So what?

  • The question was was in my package.

  • Jason, I have a script called Start.

  • Can I have more scripts here?

  • Yes.

  • You can have tons of scripts here.

  • For instance, you could have a script called Doc.

  • And what that does is generates docks.

  • You got your documentation for your A.

  • P.

  • I, um it's not actually gonna do that, because generate, Doc doesn't do anything but you for that.

  • You would type in in P m.

  • Doc, and it would pretend it does something.

  • It would generate your documentation for you.

  • You can D'oh.

  • Probably most popular when you'll see other than this is test.

  • And if you have automated software tests, you type in NPM test and it would do all the things that orchestrate those four tests for you.

  • You can put as many scripts there as you want These scripts can be.

  • So this this stuff could be other files so you can have a file.

  • You can have a directory in your project called scripts that does a bunch of really complicated stuff and just call it within PM test or whatever.

  • That pretty much what MPM test does multiple scripts at once.

  • Yeah, there's let me I'll just do the same thing twice.

  • I'll run it once with all the arguments and one's one once without, um, though you can.

  • The way I would do this, though, is I would do a make a script called Start and Test and do know or do in P M start in P m.

  • Test and order on both of them for you or you could do that from your command line.

  • But it was something that you did frequently.

  • You could make a script for it, which is?

  • The good thing about in P M is anything you do a lot you can do through Indian script.

  • All rights.

  • Are there any other questions?

  • One more question, guys.

  • Your question.

  • Am I Am I responsible for any of the downloads of it?

  • Uh, he asked me if I if I am responsible for any of the Dallas of the express thing.

  • Yes.

  • For every download of Low Dash, I probably also downloaded express the same amount of times with all of the projects we run in production that loves youse.

  • Both load ash and express.

  • So any time we publish those, which is could be 100 times a day, uh, does get downloaded.

  • So we do it all the time.

  • And having in P m do that automated for us is great.

  • I'm not responsible.

  • Yes, if I was responsible for all those weekly things, I'm not responsible for all, but I'm responsible for a non trivial amount of them.

  • All right, Are there any other questions before we wrap up?

  • Good question.

  • The question was, Do I have any resource?

  • Is for how to learn more about note or in P m.

  • This is, knows website they have on their documentation page.

  • They have guides, and there's lots of interesting guides and note.

  • A lot of them aren't super beginner focused, but if you have a little bit of programming background.

  • They're a great place to look at.

  • It explains some more advanced concepts.

  • A note like the event Lupin how buffers work and how blocking and on blocking and put out put work.

  • Um, free code camp is a great resource.

  • I'm sure they have tons of note stuff on there.

  • I've seen some of it in there, Have you some of it personally, and it's been pretty solid.

  • Um, and then there's tons of just like people posting stuff on medium that are like note articles.

  • Not all of them are great

everybody.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

Node.js和npm介紹 (Intro to Node.js and npm)

  • 5 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字