This course has been updated! We now recommend you take the Introduction to Node.js, v2 course.
[00:00:17] So for Browsers, yeah, it's for building interactive apps for the web, right? Visual things that people see and click on and interact with, they're visual things. Node is not for that, there's no visual graphics. There's no visual thing here, you have terminal, that's it. I mean [LAUGH], you could build visual apps in a terminal if you want to, people do it all the time.
[00:01:00] You can think of Node as literally just pure logic. There is no UI, there is no CSS. There is no animations, it's just pure logic. If you were to write a React app and you have like all this code and like for state management, take all that, that's what's node
[00:01:13] It's just the logic part. It's just pure logic. Any questions on that difference? So that's just like a mindset difference.
[00:01:37] The Nodejs doesn't have a DOM. Like I said, there's no GUI, there's no such thing as a DOM. But you can virtualize it. And what that means is you can fake the DOM, which if you've ever done anything with React, or Angular, or View, or anything server side, that's kind of what they do, they virtualize it.
[00:01:53] I mean literally React is built on something called a virtual DOM, that's literally what it is. So it's faking the DOM out because you don't really need to DOM to work with the DOM, you just need a fake DOM, it's very complicated. So yeah, you can virtualize it, but there is now DOM in the browser.
[00:02:09] You can't do document.querySelector and expect. I mean what would you expect to get of you did that in Node? I wouldn't even know what you would expect to get. Because there is nothing to crawl, there literally nothing there. So there is no DOM. So that's another way of thinking.
[00:02:23] So that includes all the DOM APIs. So anything that you would ever do on a DOM, as far as selecting documents or working with fields and anything DOM related, forget about it. It just doesn't exist in Node. You will get an error like, I have no idea what you're talking about, so.
[00:02:41] That's that. Any questions on the DOM?
[00:03:08] That doesn't mean you should attach things to it, just like you should never attach things to the window. If you do attach things to the window now, I want you to make a promise to yourself from today going forward, that you will never attach things to the window ever again, just don't do it.
[00:03:24] If you want to know why, I can tell you why. Does anybody want to know why? Okay, so the reason you don't want to attach things to the window is, one, it's just sloppy code. It really is a sloppy code, and the thing about attaching things to the window, it enables you to write really bad code.
[00:03:53] Two, it's like security risk. Your whole application is just sitting on window. Like, I can do whatever I want now. Just make it easy for me to just do whatever I want and I'll do it. And yeah, it's all in the window, it's all shared state. Other thing is that it's on the window where everything else is there.
[00:04:07] Everything that the browser attaches and put on the window, you put there. So you might have some collisions. You might overwrite something that was there and not know it, or the browser might overwrite something that you put and you might not know it. You're gonna have some side effects.
[00:04:18] Whenever you mutate any object, especially the global object, so just don't do it. And then, four, you can do modules in the browser now. That's very advanced, but you can. You can do modules in the browser, and we're gonna talk about that in a little bit. So there's really no reason to ever use the window ever.
[00:04:35] Actually, you know what? The only time I can think about using a window.
>> Speaker 1: Is if you like did a script tag for some third party resource, like Google Analytics, or segment, or something like that where they have to attach to the window. That is it. You're doing some marketing website, that's it.
[00:04:52] Because that's their code. That's not your code. So you don't really care if it's attached to the window. But you just never put your code on the window. If their code's inside your other window, that's fine. You can't help it. You just copy-pasted the script they told you to do and you did it.
[00:05:03] But do not put your stuff on the window. That's like the only thing I can think of. But even then, if you're building a web application today that's being bundled, you would still probably not put that on the window, you would encapsulate that in some type of library and dynamically load it up.
[00:05:17] All different types of stuff. We just wouldn't just like here, just here, put that there, right? You just wouldn't do that.
>> Speaker 1: Okay, fragmentation in a browser is real. There's literally a website called Can I Use. Anybody ever been to Can I Use, right? I look at it like ten times a day.
[00:05:33] Can I use this? Can I use this, right? If you don't know what it is I'll show you. Can I use?
>> Speaker 1: Put myself on the spot, showing my search history here. So yeah can I use, let's space it like, can I use flexbox here? Right, and Can I Use will show what browser support that feature, right?
[00:05:54] And that's because there's so much fragmentation in browsers. Different version of a browser, different browsers, different machines that these browsers, they all support different things. And then not only will they support those same things, it's like yeah, I kind of support it, partial support, not really, not like everybody else, but kind of.
[00:06:29] Look it, look at all that red. So yeah, you wanna use modules and your customers are using Samsung Internet version four, they can't use it. You just killed them. Yeah, I don't know how to jump from four to seven.
>> Speaker 2: Php.
>> Speaker 1: [LAUGH] Yeah, yeah, yeah exactly so.
[00:06:46] It's very fragmented. The thing about Node though, I'm just gonna keep it like this, and no do full screen. The thing about Node is that its version, as you found out, you install whatever version you want. And it's guaranteed to work in that version, right? You can switch versions whenever you want.
[00:07:47] Stuff like that, that just comes out and kill you, where as you don't have to worry about that in Node. If a new version of Node comes out, doesn't matter. You didn't upgrade to it so your app still runs on the same version. You're totally fine. You can upgrade whenever you're ready versus being forced to.
[00:08:00] So no fragmentation. That's pretty awesome when you think about it. Like I said, the browser does have support for modules. They're optional. And they use ES6 as their flavor of modules, and we'll talk about different types of modules. But yeah, they're optional modules, they're super-advanced. They really just came to browsers this year.
[00:08:19] Highly recommend looking into them and checking them out. But Nodejs has required modules. There's no optional, you have to use modules, there's no way around it. Unless you write all your code in one file. I promise you no one is ever going to work with you if you do that.
[00:08:34] You're never going to get a job if you write all of your code and your whole API in one file. I can promise you it's just not going to work. You might not make it. They use something called common JS For their module syntax. And we'll talk about CommonJS in a little bit, but by default it uses CommonJS.
[00:08:56] But you can use different tools to support different types of module syntax like ES6 or, I don't know why you would, but AMD or anything like that. But by default, Nodejs uses CommonJS built into it. Again, we'll talk about CommonJS and what does that mean. The differences and stuff like that.
[00:09:13] And they are required. In the browser, you can't access the file system yet. I think they're gonna enable that some part soon with Web Workers and stuff like that. But right now, you cannot access the file system because, imagine how big of a security risk that would be if I could access the file system from a web app that I put on your machine.
[00:09:30] Aw man, I could do whatever I want. I can go steal your Lastpass vault and figure it out somewhere else. So yeah, you cannot access the files inside of the browser, that's a security problem. But in Node you can, that's one of the biggest benefits. I can access the file system!
[00:10:08] In fact, it's even more in Node because you're gonna find out it's like async is a bigger deal in Node. Because one, you're probably gonna do more async things with hooking into databases and making more http calls. But also because of how your application is working, right? And we'll talk about that later.
[00:10:26] It's not making sense right now, but we'll get there. But async is definitely a bigger deal in Node than it is the browser. But it works exactly the same way.
>> Speaker 1: And like I said, no browser-based APIs. Not only are there no DOM APIs, there's no browser-based APIs.
[00:11:48] Any questions on that?
>> Speaker 1: And trust me, there's not gonna be a soft things where it's like, I'm not sure if this exists. I'm like, no, Node will just break. [LAUGH] It will tell you this does not exist. It just doesn't work. So you won't have to figure it out, like I don't know if this is a thing.
[00:12:07] No, you'll know immediately. They'll tell you.