Check out a free preview of the full Complete Intro to Web Development, v2 course:
The "Node.js Server" Lesson is part of the full, Complete Intro to Web Development, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian shows how to setup Node.js server.

Get Free Access Now

Transcript from the "Node.js Server" Lesson

[00:00:00]
>> Brian Holt: We're gonna do Node, and I think what I'll do is I will leave the deployment part of this to you, because this is quite a few steps. This is gonna show you how to put your web server out into the world, and I show you how to do it on Microsoft Azure.

[00:00:15]
>> Brian Holt: We're gonna write a little bit of Node real quick. So node.js, we've kind of been dancing around and using it a little bit, but we're gonna write a web server really quick.
>> Brian Holt: So historically JavaScript has been on the client, it's been a more client oriented thing.

[00:00:29] There's a guy names Ryan Dahl that took Google's Chrome engine, so they took V8 which is the name of the thing that actually runs JavaScript. And they wrapped it and they connected to a thing called libuv. They created this thing called Node that basically allows you to run JavaScript as a server-side language as opposed to a client-side language, okay?

[00:00:53] So, going, hearkening back to our example of the pizza place, right? This allows you to create your own pizza place that you can serve all these different clients.
>> Brian Holt: So one server serves many clients, that's a good thing to keep in mind. So what we're gonna do right now is I'm gonna copy and paste this into a project.

[00:01:19]
>> Brian Holt: Let's create a new folder. Node example, and I'm gonna open this in code.
>> Brian Holt: I'm gonna create a new file, call it sometimes like server.js. And I'm gonna paste that in here and we'll talk about it.
>> Brian Holt: Okay, close that stuff. First thing to do here, is I'm going to bring in a things that's called http.

[00:01:52] Which is the library inside of Node for handling connections, right? So a client reached out to me, is like, hey I need this thing. This is the library that actually is the wiring, right? Like if this is a pizza place, this is the telephone, right? So this is the thing that people connect to, okay?

[00:02:12] Here I go in here and I create a server. And this function is going to get run every single time someone calls, right? This is like the pick up of the phone, right? So that's what create server does. And every time that someone calls I'm gonna say, hey someone visited this URL.

[00:02:32] And, then I'm gonna send back to them just saying hello, and that's it, and then it's gonna end. And, that's gonna start listening on port 3000, I'll talk about port just a second but, let's actually start running this. Where is my terminal?
>> Brian Holt: Actually, lets even do this.

[00:02:51] So, code has a terminal built into it. So if I hit Ctrl+Back tick, it pops up this little thing on the bottom. This is actually just this the same terminal that I've been using before. Wait, can I make this a little bigger?
>> Brian Holt: Okay, yeah, I can't so I'm gonna make to the terminal but you could run this here.

[00:03:14] I could say node server.js, that's just too small to read.
>> Brian Holt: So coming back over here to iTerm, I'm gonna go up one, I'm gonna go into node example. And now I'm in here, so I'm gonna say node server.js and it's gonna say listening on http://localhost:3000. If I open that in my browser, you can here very small it says hello up there.

[00:03:40] So you can see it's actually working, it's calling my server. So let's talk about localhost:3000. Localhost is a alias for your own computer, right. So it's saying, please connect to myself, that's what localhost means. The :3000 is the port to connect on. The best analogy that I can make here for the :3000, it's like on your TV, where you have to watching HDMI3 to play the PS4.

[00:04:05] Or to be on HDM 4 to be on the Blu Ray player. It's like the channel that you have to be on or you have to be like on channel five to watch NBC, right? :3000 is, there are thousands of ports on your computer, and you have to be tuning into the right one to get the correct one.

[00:04:23] Right, does that make sense? So I could come back over here to wherever my server is, and I could say 5500, right, and save. I have to change that as well. If I come back over here, if I refresh this. Sorry, now I have to restart my server.

[00:04:44]
>> Brian Holt: Where is my iTerm?
>> Brian Holt: So I come back here, now this is broken, right? Because I changed it from port 3000 to 5500, so I'd have to come back over here to change it. So that's what ports are, okay?
>> Brian Holt: Coming back here to Node.js, no one writes Node.js by using the HTTP directly.

[00:05:13] We always do some sort of framework on top of it. I don't know anyone that's writing it directly on top of it. Typically what we do, I didn't talk about this either. If you come here you can see people, or what URLs are visiting on your server, right.

[00:05:27] So if I come back over here and say, local host 500. And I come back and say, some path, right. It still gets the same response, but if I come over here, you can see that the person visited some path, right? That's what the URL is. But what's this strange favicon thing?

[00:05:47] Well, the browser every single time that connects to a web server, it always requests the favicon. And the favicon is actually, you see how there's a little Firefox guy right there? Or girl, I have no idea, what the gender is. Stop gendering me, anyway. That's what a favicon is, it's the little tiny thing right there.

[00:06:10] So that's what it's requesting and it's not getting that back, so it doesn't put anything there. So that's why there's nothing there. That's what a favicon is. That's why you'll see a bunch of those requests.
>> Brian Holt: Node still uses CommonJS, that's why I had to do requires http, import wouldn't work there.

[00:06:31] And we didn't talk about req and res. Let's talk about req and res real quick.
>> Brian Holt: Req, which is short for request, and Res is short for response. So req represents, it's an object representing everything coming from the request. And res is everything that you're going to send back to the user.

[00:06:49] Okay and there's just like a bunch of objects on there. In fact I think, yeah, you can see here that code is actually quite smart about what's on there. There's a bunch of methods, there's a bunch of headers that you can see. Just a ton of stuff that you can see off of it, okay?

[00:07:06] So here I'm getting the req.url, I'm getting the URL that they visited and I'm saying res.end. You have to end your request to the user and I'm sending them back some text saying hello. Make sense? So that's where, and req and res is coming from HTTP.