What Is Node.js?
Hey there. So I've got a lot of Node.js videos coming up. I've already recorded some videos on how to do deployments with Node.js, automated deployments, fun stuff. But first I wanted to answer the question with an introductory video of what is Node.js. I've had a lot of people ask me this. People are saying stuff like, "Hey, I've been doing PHP for a lot of years, but I don't exactly get what Node.js is." It sounds confusing, seems like it means a lot of different things, and it kind of does to some people. So we're going to answer that.
The simple answer is, the makers of Node.js took JavaScript, which is normally confined to a browser, and they allowed it to run on your computer. So normally, you know, JavaScript runs in the browser, it can only access your web page. But when you allow it to run, when you give it an environment to run on the machine, they took Google Chrome's V8 engine, which is an awesome JavaScript engine, and now V8 runs on your machine. And so this whole new world opens up.
Now you can access the files on your computer, which you normally can't do with JavaScript. You can listen to network traffic on your computer. You can listen to HTTP requests your machine gets and send back a file. You can access databases directly, all sorts of great stuff. Basically anything you could do with PHP or Ruby on Rails you can now do with JavaScript in Node.js.
The Two Main Use Cases of Node.js
And so there's basically two categories of what people are doing with Node. They're building utilities on the machine, which is like utilities for your day-in, day-out development. That would be Gulp, Grunt, Yeoman, you know, things that you'll concatenate and build JavaScript files with, or it'll listen to file system changes and it'll do live reload, or you know, whenever you save a Sass file, it will automatically convert a CSS file. Utilities on your machine. If you see a job description that says, you know, "front-end developer" and Node.js is in there somewhere, they're probably referring to this kind of Node.js.
If you see a job description that says "Node.js engineer," "Node.js developer," they're probably talking to the other use case, which is let's build a web server, a web application with Node instead of using Ruby on Rails, PHP, Python. We're going to use Express framework for Node.js or Koa framework for Node.js or whatever, and we're going to build our web application in this. And so that's kind of what the other use case is.
The Node.js Runtime Environment
So, let's do some super basics on how this Node.js thing works. Well for one, if you go to nodejs.org, you can install it on your machine. Then you can type node -v. There you go, I've got a version. I can actually just type node right now and now I'm running a process, a process, you know, kind of a program that's running on your computer. I've now created a new process. I can type JavaScript, var a = 1, and type a, a equals 1, just like I would normally do on a console.
But what's different about this is this process right here runs within window. So if I go var a = 1 then window.a equals 1 because window is my global scope. There is no window object on a Node.js process because there's no window. What there is is a global object. global.a equals 1. And then there's also no document object. JavaScript has a global document object, which is my HTML document that this thing's built upon. Well, this isn't tied to an HTML document, it's tied to a process, which is the actual program process that's running right now. So I have a process object. And that's kind of two differences. I'm going to control+c twice to get out of that.
And you can also, I have my module1.js right here, you can also run node module1. And I don't have to put .js, but I can. And so it runs this and exits because nothing's happening. So I can go console.log('hi'). And then I can run that again. Hi. Now you can do var a = 1; console.log(a);, and then run that again. Says hi, it says one. So that's kind of how you'll actually execute files just like you would execute PHP files or something like that on your machine.
Working with Node.js Modules
And how they, let's get into modules real quick, because this is going to apply either way. And I'm probably, I'm pretty much only going to get into two modules: npm and a basic web server. And then we're done. And that's kind of going to give you the overall scope of how you program in Node.js.
So modules are how you basically load one file into another. If you've used RequireJS, you already get the concept. Let's go there to m2 = require('./module2'). So that's saying before I do any more code, I need to load in module 2. So here's my module 2. I actually have to do this, which means I'm looking for a file in the same directory. Or I could go, you know, if there was other folders going on here, I could go folder2/module2. It basically assumes .js is added, just like, you know, if you were running a file over here. So in this case, it's in the same directory. I'm just going to require('./module2') and let's console log this m2. Run it, and module 2 is an empty object because module 2 is not exporting anything at all.
I can go var a = 1. Run it, still nothing. I've defined var a in here, but I'm not exporting that. To get this from module 2 into module 1, it's whatever is exporting from module 2 is what will get passed into this m2 variable. So if I go module.exports.a = a; So now I'm assigning a to module.exports.a. There you go. Now the value of a is one. I can go module.exports.b = 2. And now you see I'm just basically building a whole object. You can also shorthand this. You can just go exports.a, same thing works just fine. Or you can override the entire module.exports object and make it a function. So module 2, so now I can actually go m2 = require('module2') and I can run m2() because it's a function now. So there you go. When I run function module2, it's going to console log it. So that's kind of how modules work. That's kind of how your separation of files works in Node.js.
Managing Dependencies with NPM
And what they've also built is this Node Package Manager, which is npm. It comes with with Node, and that allows you to actually download and manage packages, which is really cool. So I can go npm install underscore. It's a pretty popular module. So now it made a node_modules folder and it added underscore in there. And so now I can actually go var _ = require('underscore'). No, no file path, no dot-slash, just underscore. And so that's going to look in my node_modules folder for the underscore module, and the underscore package will tell me what file that loads, and that is by default just going to load underscore. So now I can go, uh, console.log(my underscore character) and when I run module one, you see that's the whole underscore library. Awesome, super cool.
So npm is a super easy way of installing common things. And then the other piece to npm is ideally you want to save them in a package. If you're working on a project and you have 15 different Node modules, you don't want to have to run npm install every time somebody downloads your, you know, GitHub repository and needs these dependencies. You want to be able to save the many dependencies you have. So you'll actually run npm init, and we're actually going to start an npm package here real quick. Name, what is node, sure, sure, sure, sure, sure, sure. And so I just said okay to everything, and it made a package.json file for me. And so it's called "what-is-node", has a version, blah, blah, blah. And you see it already added underscore as a dependency for me because I've already installed that.
So now I can do npm install backbone. If I run npm install backbone, it added to my node_modules as well, but it did not add to my package.json because I didn't do the --s or --save flag. So now if I do --save, that's for save, you can see that it now added Backbone in here into my package.json as well. And so now I've got the two going on. Let's say I delete this node_modules folder altogether. Bye-bye. Go away. You don't want to keep the node_modules in. So now my my whole node project consists of this one package.json file. And all I have to do is go npm install. npm install is going to look through all the dependencies and it's going to get them all. There you go. Now I've got all my node modules and now everything's going to work. I can require underscore again. So that's kind of if that made sense, that's how npm works. Npm allows you to maintain all your dependencies.
One of the awesome things about Node is there's thousands and thousands of dependencies that people are building all the time that allow you to do cool things like, you know, access web services or log into Google with your Google email address. There's packages out there for all this stuff. So in Node you can just load in a package real time, the package will do the dirty work for you, and you can focus on building your application.
Creating a Simple HTTP Web Server
Let's look at the HTTP package, which is what we use to make a basic web server. We don't have to install this with npm because it comes built into Node.
http and we can go http.createServer and let's give that a function. Whoops. And we'll go var server = that. It returns a server and that server can listen to port 3002. We normally run Node stuff on port 3000 actually, so we'll do port 3000. Makes this a little bigger here.
So now I'm going to create a web server and that web server will get request, response. And so whenever I get an incoming request on port 3000, this function will fire and I get to access what they're requesting and I get to actually make a little response. So I'm going to do is I'm going to console.log('got a request') and I'm going to do response.write('hi') and I'm going to response.end(). There we go.
And now let's run this. So you notice it runs and it keeps running, it doesn't stop because there is an open server running that's listening to any request on Port 3000. So if I go to Port 3000, it says hi and it says hey, I got a request. Let's do it again. Nice. Got another request. It sent back high. If I keep doing this, it's going to keep saying hey, I got a request, I got a request, let's send 'hi' back every time. Super cool, it's super awesome.
And so that's kind of how you build a basic web server. You could listen to if I went localhost:3000/give-me-a-file, then it's still just going to say hey, because there's no smart code in here listening to what the path is and what we're going to do with that path. But we could listen to the path and if the path is image.jpeg, we could go get image.jpeg and respond back with that. So that's kind of what Node.js is. From here on out, you get to mess around with it. If you want to learn how to build an Express web app, then I have a video on that already. I'm going to put that video in the description. And that's an introduction to Node.js.