The Hard Parts of Servers & Node.js

Parsing HTTPRequestObject

Will Sentance

Will Sentance

Codesmith
The Hard Parts of Servers & Node.js

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

The "Parsing HTTPRequestObject" 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 demonstrates how the HTTP message arrives in Node and is then parsed to acquire needed information from the request message.

Preview
Close

Transcript from the "Parsing HTTPRequestObject" Lesson

[00:00:00]
>> Will Sentance: There we go people. We're set up. We're ready to receive messages and what do you know? Michael's Mac arrived. He's opened it. Arrived. Michael's Mac's here, he's opened it and he wants to get his tweets and not just any old tweets, he wants tweet number three. Okay here we go, let me just quickly,

[00:00:24]
>> Will Sentance: Actually any thumbs or questions at this point? Cuz now we're about to take the next step. Yeah, Andrew.
>> Andrew: If the HTTP reserve is what sets up the socket, what's it doing while it's waiting for the .listen?
>> Andrew: You can draw it open.
>> Will Sentance: [CROSSTALK] Yeah, it's a reasonable question.

[00:00:48]
I will say this, Andrew, our draw, you can always go down further to more completely understand every single last detail, but there is a level of abstraction that's right to choose and it's sort of our job to figure out what's the right level abstraction. I think the level abstraction exactly how that open, how that socket sits until it's assigned a port, I think is a level of right.

[00:01:17]
We're talking about affect how we write code. It's a fair question. But it's gonna effect how you write code. We need a set of a port in which to listen. Okay, let's now have our wonderful friend Michael open his Mac. It's a fair question Andrew but we've, someone's eyes have to sort of set a level for the phrase, it's turtles all the way down.

[00:01:37]
Sort of applies, we have to sort of set a level at which we stop. Okay, Michael's Mac, he opens it, there it is. I know you open it, don't touch it. Michael's Mac, he opens it. He opens browser. Browser when he opens a URL is gonna instantly send out a message, know it has an HTTP message.

[00:02:00]
That is gonna have three parts. We're only going to use two of them here because we're just getting data. The first is the request line that says get specifically tweet/3. Doesn't need to twitter.com bit. That's the bit that's used to figure out where this computer is. So once we're there, we don't need that in the message.

[00:02:19]
And then it gonna have the headers. And people, you can literally just go and investigate and look at the HTTP message using, well, we'll see later on something called Curl, or Postman, there's a number of ways you can do it. Then you can have the headers, which is a bunch of meta data about Michael's Mac, and his browser, and who he is.

[00:02:38]
Yep. And that then arrives. In it comes, look, you fit it in the channel. No problem. In it came, libUV helps bring it into Node. And it arrives as a little message, just a string of texts with some important info on it. Get tweets. You can probably see this is going already people.

[00:03:02]
And then headers. But just because it is so many pieces of this puzzle, it's the same pieces again and again after this by the way, so you're gonna get very happy with it. But now, it's still worth being quite precise on all this and really kind of repeating again and again because it is so many pieces.

[00:03:18]
But from now on, it's the same pieces forevermore. Thank goodness. I mean, what auto inserted data is inserted, it's gonna depend on which background feature we're using, but it's always gonna be an autorun function and auto inserted data from Node, again, and again, and again. In response to some background computer internal feature, which in this case is the inbound message that lands on the network card, and then with the help of libUV, gets thrown into Node's C++ features.

[00:03:45]
Okay, here it is. By the way, at the same time as the inbound one, my little inbound arrow, at the same time, it spins up, ready to go, a message, which is gonna be sent back, which is where we need to attach all our data. And attach just means that, attach means write the text.

[00:04:02]
It was an image is gonna be right in the text of the image, text of the zeros and ones of the image or the way we capture the image format. The HTML is gonna be a string of string of HTML code. JavaScript string of JavaScript code. Okay, this is the, let's call it the outbound.

[00:04:19]
They don't really have official, well they do have official names that the response HTTP message. But without a bit more, you know, current fuel, and then the inbound one.
>> Will Sentance: Okay,
>> Will Sentance: So far, by the way, notice how much of the stuff we have to diagram is not showing in the code, right?

[00:04:38]
Cuz this is actually being triggered by someone else's computer isn't our code. It's kind of, like all this stuff right now, looking at the code, we finished running our code. We finished our bit. Now it's all Node. But we need to know how it's working. Otherwise, we've no idea what's happening.

[00:04:54]
Okay, in it comes. We have an auto-run function, but there's two parts to running a function, putting friends on the end of it. Node does that to execute this code and what's the other part? Michael.
>> Michael: Parameters
>> Will Sentance: I don't whom in parameters, parameters that will leave there's place holders to receive what, Matt?

[00:05:14]
>> Matt: Data.
>> Will Sentance: Yeah, not, that's partially.
>> [CROSSTALK]
>> Will Sentance: Arguments. Matt's very attached to the data one, yeah, exactly, but yeah, he's right it's data. Arguments are data. Arguments are values. Arguments are actual data. Parameters are labels that like variable names without anything stored in them. So we put those in ready for Node to create data that gets inserted.

[00:05:38]
Let's create that data. Auto created and inserted data known as, everyone?
>> Speaker 5: Arguments.
>> Will Sentance: Arguments, well done people. Look at that catchy little subtitle there. All right, first one. Raise your hand if you can tell me what the first one that's created is.
>> Will Sentance: Yeah, Sam, thank you.

[00:06:04]
>> Sam: It's gonna be an object.
>> Will Sentance: Yeah.
>> Sam: It's gonna contain the URL.
>> Will Sentance: It's gonna add a property. Node did this and it's gonna take, it's gonna look into here and go what's the URL? What's the path? Why do they call it URL? It should be the path, cuz the URL is the whole thing, isn't it?

[00:06:22]
The domain name plus the- it is actually just the path which is just the specific bit you need from the server itself. Interesting. Anyway, yep, URL which is, am I doing orange represented a literally is grab from this message? Yeah, which is turned into a string tweet/3, everyone see that there?

[00:06:46]
And then what's the other thing? Can anyone guess what other property it gets?
>> Speaker 7: Method.
>> Will Sentance: Method, yeah, it gets many properties, but another important one is method, which is set to get. There we go, perfect. And before we run the function, theres one other object that needs to be auto created in order we can access and stick stuff on this outbound needs to be a message.

[00:07:13]
Well that's other object isn't it? So the other object is full of functions like AND and right that when they're run from inside of JavaScript will stick stuff on this important response object. I know we've done this three or four times now but it it's complicated. It's complicated.

[00:07:40]
I was using complex and complicated. Complicated there's lots of moving parts, complex as its conceptual abstract and the way the mind doesn't usually think about. I would say this is, all I see both complex and complicated. It's complex and it's weird to think of someone else running out code for us, as opposed to us.

[00:07:58]
That's complex as conceptually unusual thing. It's complicated in that, what it runs and what is insert as the arguments is a bunch of little intricate things. It's actually unfortunate but complex and complicated, which is a shame.

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