Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Event Handling in Node" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will discusses the event system in Node by going over how to edit a server after setup to respond to certain events differently or change ports.

Preview
Close

Transcript from the "Event Handling in Node" Lesson

[00:00:00]
>> Will Sentance: We're gonna do pretty much what we did before, but this time we're gonna be even a bit more sophisticated. And add a key pattern, as its known, in Node, the pattern of the event system. Where behind the scenes, when things happen in the computer internal features, Node is not just gonna auto-run a function.

[00:00:18]
It's first gonna broadcast a message that has attached to it a function that Node's going to auto-run. Meaning we can have more control, well yeah, we can tell Node more detailed what function it's gonna run under what condition. So if it comes as a happy request, nicely formatted, Charlie's not sending us corrupt data, it's gonna broadcast, it's gonna shout out in Node, request!

[00:00:40]
And hopefully we've set up, if requests gets shouted out, auto-run, do onIncoming. And if it's got an error, and it's a corrupted, nasty request that's gonna cause issues to us, I expect nothing less from Charlie. Then it's gonna broadcast a message saying or shout out, client error! And hopefully we've set up a function to auto-run on the client error event.

[00:01:05]
Well, let's go. Line one, Virginia, what are we doing in global memory, Virginia?
>> Virginia: Declaring a function, or defining a function. Do onIncoming.
>> Will Sentance: Yeah, both of those things mean the same thing, that means we're saving the code of a function, do onIncoming. Next line, Sarah Rose.
>> Sara Rose: So we're declaring another function doOnError.

[00:01:29]
>> Will Sentance: Fantastic, thank you Sara Rose, there it is, saved. Next line, my goodness, we're only gonna do two of these things, but let's get going. First, we declare a server on the left-hand side. Okay, server's declared.
>> Will Sentance: Server's declared, it's going to be the result of, it's gonna be the result in JavaScript of calling http.createServer().

[00:02:02]
We're not gonna pass any inputs this time. Remember, the input we pass is the function code that's gonna be auto-run when an inbound request arrives at our socket. I have a feeling we might still add a function to auto-run, but this time we're gonna be more manually controlling it.

[00:02:16]
It's actually going to do exactly the same thing as when we pass it indirectly but, fine, okay. But it also is going be using the JavaScript. What is the thing that http.createServer() does in Node? Raise up your hand if you wanna tell me what it does in Node.

[00:02:31]
Michael, take it away my friend.
>> Michael: Well, it returns a object
>> Will Sentance: That's in JavaScript, what's it doing in Node? Zep.
>> Zep: It sets up a socket.
>> Will Sentance: Uh-huh, it sets up the HTTP feature of Node, which is actually behind the scenes just setting up. It's gonna send a message into the computer's internals, where it's going to turn on in the networking portion of our computer.

[00:03:00]
>> Will Sentance: It's gonna turn on an open socket, which is, remember, just a fancy word people, for an open channel to the Internet that's two way. That can receive data and send data back. Okay, we've better make sure it's gonna port, so we know which of the 64,000 entry points, metaphorically entry points, that the data's gonna coming at.

[00:03:18]
So I hope we're gonna have the ability to edit, cuz I don't see in this first line here, where we set up the background feature, the socket. I don't see us telling it what port is, I hope we have the ability to edit this background feature, I hope.

[00:03:31]
Well, do we? Yes we do, because the other thing, so Node, http.createServer() opens a socket. That's what it does in Node, a connection to the Internet. But the other thing it does, was it Michael you were saying? The other thing it does is in JavaScript, it returns what?

[00:03:51]
>> Michael: An object.
>> Will Sentance: An object full of what, Zep? Yeah, full of what, Virginia?
>> Zep: Methods.
>> Will Sentance: Methods, including?
>> Zep: End.
>> Will Sentance: No, definitely not. Including?
>> Michael: listen.
>> Will Sentance: listen, yeah, we'll see that in a second, Virginia.
>> Will Sentance: There is listen, which these are, let's call them our edit functions.

[00:04:13]
They let us edit this particular Node HTTP instance. So we're gonna have ability to edit HTTP, okay, down here. Which is then what we immediately go ahead and? By the way, what other function do we get to, Zep or Virginia? What other function gets added to that object?

[00:04:35]
If you get it have a look ahead, go ahead, Jeff.
>> Jeff: on.
>> Will Sentance: on, another function that also, I have a strong feeling, is gonna let us edit stuff down here. And by the looks of it, might let us edit what function gets auto-run in Node, or by Node.

[00:04:53]
So function to auto-run functions, I feel it's gonna be functions now. But we've got nothing stored in it yet, you've got nothing stored here. Well, we'd better be able to edit this from JavaScript. Remind me again, why can I ongoing edit stuff down here, or how can I ongoing edit stuff down here in Node from JavaScript?

[00:05:19]
Raise your hand and tell me why. Mohamed, how can I ongoing edit this HTTP feature in the background that edits the, how do I get to go ongoing edit it? Mohamed, do you have an idea?
>> Mohamed: No.
>> Will Sentance: Pavi.
>> Pavi: Node daemon? Node daemon, no.
>> Will Sentance: How do I get to ongoing edit down here, Michael?

[00:05:39]
>> Michael: The C++ interface?
>> Sam: You use the objects, the response and request objects.
>> Will Sentance: No, well, yes later on, but how do I get to ongoing edit my setup down here, Sam?
>> Sam: Those functions. The functions that the [COUGH] HTTP returns.
>> Will Sentance: The functions that the http.createServer() return down into JavaScript.

[00:06:01]
What is the server label here? It's a label for an object full of functions that allow us to do what, Roman?
>> Will Sentance: That allow us to do what, Charlie?
>> Charlie: Modify the-
>> Will Sentance: Modify the underlying HTTP instance or feature we've set up. So if I want to set up my port in the background, after I've set up the HTTP feature with createServer.

[00:06:27]
What function from that list of functions that got returned out as an object in JavaScript from http.createServer(), what function would I use to edit the port, Andrew?
>> Andrew: The .listen?
>> Will Sentance: The listen one. What function do we think, Zep, that I would use to edit or set up what functions to auto-run down here?

[00:06:49]
>> Zep: on.
>> Will Sentance: on, excellent.
>> Will Sentance: So Mohamed, how am I able to, after I set up the HTTP server feature of Node, continue to edit it?
>> Mohamed: You use the methods that come with the HTTP objects.
>> Will Sentance: Not quite. When I ran createServer, it set up the background server in Node, but it also did something in JavaScript.

[00:07:23]
What did it do in JavaScript, Zep?
>> Zep: Created the object, the server object?
>> Will Sentance: With what?
>> Zep: With the port, the listener-
>> Will Sentance: With a bunch of methods-.
>> Zep: Yeah, the function.
>> Will Sentance: That would allow me to edit this Node HTTP server with this particular socket I set up down here.

[00:07:41]
Right, this is a particular instance, it's the one we just set up. It's the particular open socket that we just set up here, that we want to be able to continue to edit it. So when we set it, what functions set up the background open socket? Charlie, which function do we run to set up the background open socket?

[00:08:01]
>> Charlie: createServer.
>> Will Sentance: createServer, that's what it did in Node, not in JavaScript, but in Node. What did it do in JavaScript, again Zep?
>> Zep: listen.
>> Will Sentance: What did it do in JavaScript, William?
>> William: It created an object with the functions of createServer.
>> Will Sentance: With the functions that link to this background server instance in this socket, including which functions, Charlie?

[00:08:25]
>> Charlie: listen and on.
>> Will Sentance: Fantastic, and if I run either of those functions, they're gonna change, they're going to edit this particular Node background feature HTTP that we've set up, that will edit this particular socket. Andrew.
>> Andrew: So could I have a server1 equals?
>> Will Sentance: Sure, I could set up, I could do this multiple times.

[00:08:49]
So Sam,
>> Will Sentance: How am I able to continue after I've set up this background open socket via Node with the help of HTTP? What function let me do that, by the way?
>> Will Sentance: What function let me set up this background open socket?
>> Sam: createServer.
>> Will Sentance: createServer being run.

[00:09:10]
Once I've set it up, I still wanna make a ton of changes, I don't even know what port to listen on yet. I don't know what functions to auto-run when a message comes in. How am I able to do that, Sam?
>> Sam: Through the methods in the server object?

[00:09:22]
>> Will Sentance: Yeah, exactly, the methods in the server object which came from where? They were JavaScript's output of running createServer. Charlie, what was Node's output of running createServer, what was the C++ output of running createServer?
>> Charlie: Setting up the socket.
>> Will Sentance: Fantastic, what's JavaScript's output of running createServer?
>> Charlie: The object with the functions available.

[00:09:42]
>> Will Sentance: Which do what?
>> Charlie: Which modify Node's behavior.
>> Will Sentance: I think Charlie deserves a hand for that. Thank you, Charlie, excellent, well done.
>> Speaker 13: [APPLAUSE]
>> Will Sentance: No, that's excellent, okay, all right. It's really complicated, there's a lot to it. But we've got to get that absolutely 100% precise.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now