This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Nodejs Refresher" Lesson
[00:00:00]
>> [MUSIC]
[00:00:39]
>> Speaker 1: So let's just hop right into it then. Everybody should have the link for this. These are going to be our notes, or our slides. I don't like to use actual slides, I'm just worn out on slides, so I just made a website, with the notes on it and that's what we're gonna use, and this will be here forever, probably at the center README on the GitHub.
[00:00:59] README so you can look at it. So this is where we'll do our learnings and then we'll have an app that we'll be building and then, as far as the app, the repo is gonna be here. If you haven't got this go ahead and fork it or clone it or whatever your process is for getting your repos.
[00:01:17] Cool, so also I want to let you all know to, if you have a question here on site, just ask me, don't feel like you have to wait until the end. Just, I want to stay open. Ask me as many questions as you want, don't feel like you are slowing me down and online, if you're joining us, just hit up Mark with a K on the channel and just ask him and he will either answer it himself or he'll ask me so just feel free to ask as many questions as you can.
[00:01:43] If you think I made a mistake or if you didn't quite understand what I said, don't let it go by, just totally ask me. Great, so what are we gonna be doing for these next three days in this workshop? Well, like I said it's gonna be around designing APIs with Node and Mongo, and how all that works.
[00:02:03] So, not only are we gonna be learning how to design this stuff but we're also gonna be building it of course. We're gonna learn how to test it and we're gonna be deploying it, which makes sense. It wouldn't really be fair if I showed you how to design an API and not how to do the rest of that stuff.
[00:02:17] That wouldn't be fair, it wouldn't be setting you up for anything practical. You should have some experience with JavaScript. I hope everybody here knows JavaScript. If you don't know JavaScript this might be painful for you. Unless you just, you have tons of experience on the back end and you can get back end concepts and maybe just work your way through JavaScript.
[00:02:35] But you should totally know some JavaScript, a little Node experience wouldn't hurt either. If you're brand new to Node and you have no idea how Node works, that's fine, we can get through some of this stuff but some Node experience would be great. But by the end of the workshop, you will have built and deployed an API for a blogging app that has authentication.
[00:02:55] So as far as the application itself, you will not be building that. You actually built that on another workshop. So that's built for you, the front end, the client. You'll just be building the server part for it. So don't worry about messing around with the actual web app.
[00:03:08] We already built an app. Cool. Any questions so far? All right. So, let's just do a quick refresher on Node. Who in here has experience with Node? Let me see your hands. Okay. A little bit, a little bit, none, okay. So we're gonna do a little bit of a refresher on it.
[00:03:29] So I'm just gonna walk through some examples, talk about some of the things that are different, what it is and then move on from there. So, Nodejs is basically a way just to run JavaScript outside the context of a browser, that's it. That's all it is. It's not a server framework, it's just, I want to be able to run JavaScript outside of this.
[00:03:48] That's what Nodejs is, is using the same V8 that Google Chrome is using. Or I mean, it's actually, Node is using a different version of V8 but it is V8. So we can use Node for many things now, like tooling and servers, because we can run it outside the context of the browser.
[00:04:08] Just like you would be able to with something like Python or Ruby. So to get started, if you have Node installed, you can just type in Node in your terminal and you'll get a full JavaScript REPL. So what I mean by that is if you go in your terminal with Node, if you just type in node here, you now have a REPL here.
[00:04:25] This is JavaScript.
>> Speaker 1: All right.
>> Speaker 1: So you have a full JavaScript REPL here. So if you wanna be able to, it's the same thing if you went into the Console inside of Chrome minus the browser API stuff, the browser specific things. So that's how you know you got Node working.
[00:04:54] Like I said, but note this is just JavaScript but it's not in the context of the browser. By the way, if you see spelling errors and stuff in here, I'm really bad at spelling so just forgive me right now. [LAUGH] Like that, but yeah, so it's just JavaScript but it is outside the contents of the browser so most of the browser APIs do not exist in Node.
[00:05:14] I can't come into Node and start doing things I would expect in a browser, yeah, let me just do a document.query selector. Right, that's not gonna work.
>> Speaker 1: Document is undefined. So this is not the browser, this is just JavaScript outside the browser so those APIs aren't there.
[00:05:39] There are some that are there that are very useful, like Console, that's still there, Console.log, Console dot, all that good stuff, all that's still there, but a lot of the other stuff isn't, so just remember that. Yes.
>> Speaker 2: A question came up, is there a way to add the browser APIs to Nodejs?
[00:05:57]
>> Speaker 1: There are polyfills out there that will mock some of the stuff and in some cases, some libraries, because Node and because of this whole universal JavaScript thing where people wanna run JavaScript on both server side and client side, some code actually needs those things mocked out. So yeah, there are mocks there, but realistically you wouldn't even need that stuff.
[00:06:17] You wouldn't need it for execution. You might need it for a library to work. For instance, I had to tell somebody the other day, there's this animation library that we use that's expecting these window, or these browser APIs to exist. And if it don't it'll error out on Node.
[00:06:34] I would never use them in Node, but it needs to be there in order for it to run. So, sometimes you have to mock this stuff out. But people are getting to the point where they're being more aware of where their code is gonna be ran. So they define their libraries in something called a universal module definition, which will allow their JavaScript to either run in the browser, in Node, and in any type of module format, whether that's AMD or common JS.
[00:06:58] So that's the newer way to build your libraries. So that's the thing that people are starting to do now. If you download something that isn't like that, something that's attached to the window, and you try to run it in Node, probably gonna get an error. So you can chase the errors and figure out the polyfill yourself or just convert it to Node yourself and see what happens.
[00:07:17] But, yeah, there are some polyfills that do exist, but people are moving away from that.
>> Speaker 1: So, if you were to install Node, you will also notice that you get something called NPM which, if you look at it, you might think it stands for Node Package Manager but it actually doesn't.
[00:07:36] It doesn't stand for Node Package Manager. I actually don't even know what it stands for. So [LAUGH] it's not Node Package Manager though. I think it's a pun that the NPM people came up with, but it's not Node Package Manager, which should be it. So, NPM is actually a really, really good package manager though, as far as handling dependencies.
[00:07:56] In fact, it's probably one of the best that I've seen, not even just related to JavaScript but just in general. So, it's kind of like if you've ever used Homebrew or something like that, it's very similar to that but just, it might be a lot easier to use.
[00:08:08] So the way NPM works, like I said, it comes bundled with your installation of Node. It uses this file called a package.json to store meta data about your packages and what packages your package may need. So it just reads that file depending on the command you give it.
[00:08:24] And that's how it knows what to install, what do uninstall, what to update. It's just a file. So NPM is the standard. There really aren't any other package managers you would use with Node. But they are two separate entities. NPM is its own entity, Node is its own entity.
[00:08:41] They just happen to be working together because they just work that way. So, yeah, you'll see a lot of, everything that we do as far as dependency management will be through NPM. There are other package managers out there like Bower and stuff like that. But Bower is a Node module that you have to install with NPM.
[00:08:58] And that's more for client site dependencies. So it's still all through NPM. So, all third party modules when downloaded will be placed in a node modules directory by default. So we use NPM to go download something. If there isn't already a node modules directed at the root of our application, one will be created.
[00:09:17] And that's where all of our node modules will be placed by default, in a node modules folder.