The Hard Parts of Servers & Node.js Event Handling in Node
Transcript from the "Event Handling in Node" Lesson
>> 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.
>> 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.
[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:31] Michael, take it away my friend.
>> Michael: Well, it returns a object
>> 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.
>> 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.
>> 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: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?
>> 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.
[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.
>> 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?
>> 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.
>> 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?
>> Charlie: createServer.
>> Zep: listen.
>> 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?
>> 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?
>> Charlie: Setting up the socket.
>> Charlie: The object with the functions available.
>> 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.