The Hard Parts of Servers & Node.js Creating a Server Under the Hood
Transcript from the "Creating a Server Under the Hood" Lesson
>> Will Sentance: Node inserts two inputs, two arguments automatically. One is going to be, well, let's see it, let's just see it. Here we go, here it is, let's have Sara Rose's message come in. Here comes Sara Rose's message. Here it comes in, she's asking for what tweets?
>> Sarah Rose: Sorry, we're asking for the Node tweets.
[00:00:47] It's a string of characters, so it's an HTTP message. I can actually get access directly. If I instead set up my open channel using the net, N-E-T-, built in label, then the data that would come in, I'd actually get it back in here in just the pure stream of characters, the actual message itself.
[00:01:07] But because I set up this background feature, well, before we see that, before we see that, just a second before we see that, in comes the message. What do we want to have happen at that moment, Zep? Before we talk about any inputs, what we wanna have run at that moment, Zep?
>> Zep: Do incoming.
[00:01:48] It's gonna execute code, so it's gonna need what, Andrew?
>> Andrew: Parentheses.
>> Will Sentance: Parentheses, Charlie, you're losing your job.
>> Charlie: [LAUGH]
>> Will Sentance: Parentheses, put parentheses on the end and execute that function. So we're gonna see this again and again and again cuz it's all that Node does. So we're gonna see this again, so we'll get very, very used to to this model.
[00:02:37] How are we gonna do that? We better have inside this function's code access to this inbound message, and somehow, access to this message going back so we can add stuff to it, some data, some tweets, some HTML, some CSS to send back. Do I get these two strings of characters directly available to me in here?
[00:02:58] I actually don't, because Node wants to make my life easier. So instead, and this is the other big part of the process, we're gonna see this again and again, folks, so don't worry. Instead, the next thing it does down here in Node, as soon the message comes in, it's immediately going to set up a HTTP message ready to send back by the way.
[00:04:06] What word would you repeat?
>> Speaker 6: Parameters.
>> Will Sentance: Yeah, but we'll see that in a second, all right. So it's gonna automatically create, when the inbound message from Sara Rose arrives, it's gonna immediately automatically create two objects. And these are the most important objects in all of Node. Folks, you may be like, my God, why don't I just build my nice pretty app?
[00:04:28] But if we understand these two objects, we have everything we need to be successful Node developers. I started off my telling you about that Samer Buna guy, who like the Node authority. He's got a book called Node Beyond the Basics, and a list of 27 questions, interview questions that if you are asked in a Node senior interview, you need to know the answers to get a senior Node job.
[00:04:51] We are gonna answer, and already, by the way, already we've hit three or four of them, no, more than that, seven of them. And I'm making that up, I have no idea, but many of them already because this is what's going on behind the scenes, two auto created objects.
>> Michael: The body?
>> Will Sentance: Well, there's no body here, we'll see that later on. What's the-
>> Michael: The URL.
>> Will Sentance: The URL, and it's gonna give it a little property on this object called URL, which is a string of, well, not the not the domain, just the path which is the next bit here, the specific bit, it's slash node because that's what Sara Rose wants.
[00:05:38] She wants the Node tweets, and it's gonna literally, Node, and I'll do an N to show you that Node is doing this, is gonna look into that HTTP text message. This is a trigger text, and grab out the URL, it's gonna parse it and grab out the URL and stick it in this object.
[00:05:55] And that object is gonna be [SOUND] inserted a magic word automatically into this execution context. It was automatically created by Node, automatically putting parenths on the end of the function that we passed to it, do on incoming, to say when you get an inbound message, auto-run do on incoming.
[00:06:18] And there's two parts to auto-running a function. One is running its code, and the other, raise your hand if you know, Virginia?
>> Virginia: Inserting the argument.
>> Will Sentance: Inserting the argument, which is good which is as good as saving data, that's what it is. It's inserting data to make it available in here and saving it in here, yeah.
[00:06:32] It's not a crazy thing to say, but so that Matt wasn't wrong, but specifically, Matt was wrong, but specifically, it's inserting arguments. Okay, here they come, the first one is that first object with all the data we need. Let's stick it in, it's in the local memory, it comes in Node, people, without a label.
[00:06:55] So how the hell are we meant to access a property of an object that has no label, Michael?
>> Michael: We have to call it something.
>> Will Sentance: Which we do. We give it a placeholder, we know when we say the do on incoming function, we now that when it gets run, it will have automatically, by Node, inserted two objects.
[00:07:14] And so we give it two placeholder names that those objects will be accessible by. And we can call them whatever we want, as long as when we then try to refer to them inside the function, we refer to them with the right label. And so Michael, what do we give the object that's gonna be auto inserted, the first one with all the data?
[00:07:34] What do we give it, what do I give it?
>> Michael: Incoming data.
>> Will Sentance: Incoming data, I don't call it record. We'll see that's what we always used for the term in general req, or requests, but I don't want you thinking that's like a keyword. It's not, it's whatever we wanna call it, okay?
[00:07:50] There it is, incoming data, and what gets inserted? This whole object with a whole bunch of properties, by the way, I just put in only the URL, but it's a whole bunch of properties added on here, headers, body. Yeah, body is captured an interesting way which would we might see later on.
[00:08:12] But all that data is on there, okay, I've got all the data. But now, I didn't bring all that data in from Sara Rose's request to not send some data back. My little message here, let's show then it's ready to go back, it's sitting there, it's sitting there ready to send back, Node made it for me behind the scenes.
[00:08:32] It's sitting there, I wanna go back. How the hell do we get access to it? Well, wouldn't it be nice, people, if the second object maybe had a property on it called, I don't know, maybe data. Something like that which we could then, hopefully that object would go in here and then we could access and add value to it, add value to it and send back.
[00:08:55] [SOUND] It's not how it does it for some reason. Instead, here's what it gives us. It does give us an auto created object automatically created by Node that, yes, is inserted in here as the second input. But that object does not have a property on it that we can add data to that then gets sent back.
[00:10:38] Rich, what's the parameter name for the second inserted object?
>> Rich: Functions to set outgoing data.
>> Will Sentance: Fantastic, fantastic, functions to set outgoing data. Is this a key word, Rich?
>> Rich: No, it's not.
>> Will Sentance: No, it's just whatever, it's just cuz they came in, the auto created objects are anonymous.
[00:10:54] They got no name, so we better have given them placeholders, we did, no problem. In comes the object, it has on it a whole bunch of functions. But one of them is end and I think I have a feeling we're gonna use that function. And look at that, what are we gonna look for, Zep?
[00:11:14] Which object by which parameter name are we gonna look at?
>> Zep: The end?
>> Will Sentance: Which object, which one, it's the functions to set outgoing data object, and then we're gonna look at what function on it?
>> Zep: The end.
>> Will Sentance: The end function and that's exactly what we're going to run.
[00:11:32] Functions to set outgoing data .end, and whatever we passed to it, which is here, welcome to Twitter, say welcome. This function, .end, when run, has an intimate connection back to Node. It's a Node function, and it's going to send a message into node down to this HTTP message, where it's going to add welcome to the message.
[00:12:09] And then that message is going to be sent back to Sara Rose's Mac.