Transcript from the "Node as a Webserver" Lesson
>> Kyle Simpson: All right, for now we're gonna set aside the hello worlds module because we've had our fun learning about the module system and how we work with that. So from here on out, I want us to create a separate file, so rather than copying over, we're gonna create a separate file and I want you to call this file, lemme double check and make sure I'm calling it correct.
[00:00:24] I want you to call this file 4.server.js. And the reason for that name, it sounds a little bit weird but you'll understand a little bit later why I'm calling it. So we're gonna start with a blank file now, [COUGH] kind of starting from scratch. And what I'm gonna now teach you, we already learned the console.log version of Hello World, that's when we're dealing with the command line, but now we want to learn about dealing with node from the networking perspective, listening for HTTP requests.
[00:02:00] So we need to pull in the HTTP module.
>> Kyle Simpson: And then we need to create ourselves an HTTP server, so I'm gonna say http_serv, is what I'm gonna call it for now. Now I'm gonna say http_serv = http.createServer() and I need to give it a function that's gonna handle my http request, so I'm gonna call my function handleHTTP.
>> Kyle Simpson: So let me just define what that function's gonna look like. This function is gonna get called every single time a new inbound request comes to our server. And it's gonna receive two parameters. Those should look familiar from the code I showed you earlier. It's gonna receive a request stream and a response stream.
[00:02:52] We'll come back to the implementation of that in a moment. The other thing that we want to do we want to have a host, which we're gonna call local host and I'm gonna choose the port for the rest of these examples of 8006. I don't really care what port you choose.
[00:03:09] But if you don't wanna have to change your code from the stuff I'm doing, you might choose 8006 as well. Now the final thing we need to wire this up is we need to tell our HTTP server to listen on that port. So let me actually take those and move them above, just for simplicity sake.
[00:03:30] And let's call .listen on host and port. I'm sorry, it goes the other way around, port and then host.
>> Kyle Simpson: Now, inside of our handleHTTP, as our very simplest approach, we're gonna do more here in just a moment, but as our first approach at this, I want you to just do res.end("Hello World").
>> Kyle Simpson: Now let's go back to our command line. We now have a 4.server.js, when we run node 4.server.js, you're gonna see something different happen that you didn't see before and that is that it doesn't come back to the command line because our program didn't finish. In fact, theoretically our program is now an infinite program, because there's an infinite listening loop set up.
[00:04:32] That's what turns our node program from being sort of a command line program into being what we'd call a Daemon program. At this point, before I go any further, you should start to ask yourself, well, how am I gonna manage that sort of thing? So there's another part of the ecosystem that deals with managing node processes that are long running processes like servers.
[00:04:53] And I already have installed but if you don't already have it installed, if you do npm install- g, the the minus g part is gonna make it a global utility. Which you're probably gonna want. If you're on a Mac or on some systems, you may need to do a sudo in front of it.
[00:05:09] So if this fails, you might try it again with sudo. But NPM install -g forever, this is only one of a dozen utilities that are out there for this. But they're process managers and what they essentially do is they run your process and they monitor to see if the process dies and if it does, it restarts it right away.
[00:05:28] So it literally keeps your node process running forever. And node processes do die by the way because we don't write perfect programs, there's out of memory errors, there's bugs that we didn't anticipate, our node programs do die. As a little anecdote on that, by the way, just kind of ever so slightly self-serving, I wrote a site last year back in the late summer and early fall.
[00:05:54] Some of you may have heard of it. It was called the Museum of Mario. I built it for IGN, the Online Gaming Network magazine, and Museum of Mario is an interactive anthology of the 30 years of Mario. I can even turn on my sound and you can hear if it loads the sound.
[00:06:12] The Donkey Kong game starts playing and it actually plays inside of this little Web GL thing if the network lets it load and I may have to come back to it. But as you scroll through this site, we recreated these different experiences from the Mario games and, I think maybe I have my sound muted.
[00:06:29] [SOUND] That might be what's wrong. [SOUND] So let's see if I can get the Donkey Kong one to load cuz that one's my favorite one. This is just a little Web GL thing that we built so that you move your mouse around, it rotates it in 3G. Sometimes that video is kind of slow to load, so maybe it's not gonna load for us, but it does actually show a game playing of the Donkey Kong game.
[00:06:52] But anyway, not to show off the site but I wanted to show that kind of an untold story behind this museum of Mario is that.
>> Kyle Simpson: Okay, that's enough sound for us. But an untold story behind this Museum of Mario site that I built is that I was going along helping a team build the frontend stuff, but we needed to have a server for it. And of course, because I like Node, I just arbitrarily made the executive decision that this site was gonna run on Node and I didn't tell anybody.
[00:07:31] I told my team members but I didn't tell anybody at IGN or Nintendo or any of those other companies. I just made the executive decision, we're gonna launch the site on Node. So I wrote a simple little note program, by the way the entire code for this site is open sourced, so it's on GitHub at, I think it's html5hubs/museumofmario.
[00:07:58] That's the correct site. The whole code for this site is open sourced including the Node program that I wrote. But I wrote this simple little node server and I deployed it with that forever utility. And in the first couple of weeks of this site going live, we got over four million hits.
[00:08:16] And Node didn't even break a sweat. So, it's still running even to this day. That's my kinda one shining example that I can say of me pushing something Node-based into a production environment that actually got a pretty decent amount of traffic. And I didn't go to any trouble to create a really complex, high availability mechanism with a Node farm and all kinds of load balancing stuff, it's just a single node process running and it restarts itself with forever.
[00:08:45] I checked the logs a few months back and it only restarted three times in that whole six months, so it didn't need to restart very often but it was keeping it alive and running. And in fact, there was one day where about three weeks in or something like that, the site crashed and I got somebody that called me kind of panicked, they were like, the site's crashed.
[00:09:04] What's wrong, is it node or whatever? And we checked into it and Node was still running just fine. It was the load balancers that IGN was running that'd crashed. So Node it had no trouble whatsoever and never crashed. Anyway, the other reason why I bring up, by the way, that site as somewhat self-serving is that Museum of Mario was nominated for a Webby Award.
[00:09:31] We're currently in a very distant third place. So I'd love to have more votes from people in the online stream and here if you'd go to the Web beans and sign in with your Twitter account and vote for Museum of Mario that'd be awesome. But anyway, all right, enough for that.
>> Kyle Simpson: [COUGH] Let's test our server, let's come back to our command line. And you can run it with forever if you want, I'm just gonna run it the plain old manual way. I can say 4.server.js. So, it's just running and I see it there running on my command line, I can't do anything with it.
[00:10:05] I can't do anything else with my command line, but now if I come to my browser, let me close the, I won't close it but I'll shrink it. Let's open up localhost port 8006, and we see the Hello World there. Let's test it to make sure that we're really getting multiple ones by saying something like Hello World + a random number.
[00:10:38] We gotta come back to my command line, I gotta kill the node process that's there and restart it. There are utilities that will watch your file system and automatically restart Node if you want to. Okay, refresh my page and I get a different random number, every time I refresh the page.
[00:11:01] Which verifies that our Node program is in fact working. That's the simplest network based Hello World that you can create for yourself.