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

The "Request & Response with 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:

After summarizing the functionality created by the server creation code, Will gives a real-world example of how Node is being used in relation to server requests and responses.

Preview
Close

Transcript from the "Request & Response with Node" Lesson

[00:00:00]
>> Will Sentance: We've build out, though, for here a full server in three lines, say to function, just do it one more time. Say to function, use the label hb.createserver that sets up a no background feature, which really is not really about setting something up in Node, more importantly about setting something up in the computer's internals and open channel to the Internet.

[00:00:20]
Perfect, and in order that we can set up what port, what entry point we come in at, which needs to be 80 because that's what the browser request defaults to entering at. It's arriving at 80, so you better have opened port 80. We were able to do that because hdp.createServer, the label that sets up the port, the socket, not the port, sorry, the socket.

[00:00:43]
Also returned out an object full of functions, it's called an edit functions that when run, tapped into this instance of the Node HTP feature, and updated the port to 80. We can do other stuff, we can edit more stuff, there's a whole bunch of functions on here that allow us to edit.

[00:01:01]
We're gonna see a few more of them in a moment. And then, when the inbound message came in, we didn't panic with what to do. We didn't have to have the JavaScript code running, running, running, running whenever the message came in. Instead, we relied on Node to automatically trigger the running of some JavaScript code, executing it, put it in an execution context to run the code.

[00:01:23]
And I think the most important piece of all, that 98% of even Node developers don't really get, it's gonna auto insert these two objects that were automatically made in Node in the JavaScript objects, but automatically insert them. And one of them is all the data from the inbound message packaged up in a nice format.

[00:01:44]
They basically take all the pieces out of it. They take the string or Node, and they take and put as URL property, but be really clear, it does not arrive with a URL property. The HTTP message, it's a string of text. You can look at them, and they do not look like that.

[00:02:00]
But we love jobs with objects. So they put it in a nice bundle for us, Node does. And then it gives us an object full of functions, nice bundle of functions, all of which are linked to the auto created response message, not object, message that we can add text to, or content to, HTML files to, image data to, JavaScript files to.

[00:02:26]
We see all this stuff to come by running that, some of the functions, on that auto inserted second object. One of the functions is end, that one tells actually, overall, Sam, as Sam rightly said, overall, its job is to say hey, Node, the message is ready to send back, go, end.

[00:02:44]
End's not a stupid, end's a sensible name for it. But actually, we can also pass to it one thing that we want to send back if you want to, but typically, we don't use ends to add the data to send back. Typically, we'll use something like right to add the data to send back.

[00:02:59]
We'll see that all to come. Okay, folk, here we go. Messages that were sent in a HTTP format. And this is the last code we're gonna see, by the way, before we hit pair programming. We start to do this in action ourselves. Messages are sent in HTTP format.

[00:03:15]
The protocol for browser-server interaction. That means the rules, the format of data by which a message from the browser is sent to Twitter's computer, and lands in here. It has a certain number of pieces to it. Let's see. Let's have actually a new person.
>> Will Sentance: Michael, Michael, Michael, my friend, you've arrived, and you're sending an inbound message.

[00:03:48]
And Michael, you're going to be in orange. Here we go, your computer. Michael's getting his microphone ready. Microphone ready so he can, I mean, what? But there you go, Michael's Mac, there it is, I'm not gonna try and draw it any other way than I always have. There it is.

[00:04:08]
Okay, he opens twitter.com/tweets. It turns out this Twitter only has one user, it's Michael. /tweets, /3, he opens it in the web browser. Michael, why do you look concerned? This is a fantastic thing to be searching. There you go. We wanna see his third tweet. It instantly is going to send out over its network feature an HTTP formatted message.

[00:04:38]
Okay, It has three parts to it.
>> Will Sentance: One, let's do it up here. One is known as the request line, and this, he's trying to get data. If you were posting a tweet, it would be posting data, this is getting data. So, the first, it says get, and what part/tweet/3, that's literally the first line of HTTP request.

[00:05:05]
Well, actually, yeah, that's the first line of your HTTP request, or HTTP message, text message. It's just a text message, the first line is this.
>> Will Sentance: Or does it say something else? Anyway, whatever, second line, the second piece or the second part are headers. This is meta, that means data about data.

[00:05:26]
Meta data about Michael's Mac, things like, it can be all sorts of stuff like his browser type, his, I don't know, location. It can be all sort of things that are important as information to help when I get the inbound message to know precisely what to send back to him.

[00:05:46]
Maybe I also have in there information about whether he's logged in or not. So that I can determine whether to be able to send him out any data at all. So there's all the metadata, and then part three is the body. This is if I'm sending more larger data like if I was posting a tweet, that tweet data would go in the body.

[00:06:03]
If I'm getting data, I don't even need a body cuz I'm not sending you anything besides exactly what I want to get. So here's the message. In it's gonna come, whee, okay, it's gonna come in here. And this time, we're actually going to get much more precise, much more personalized than what we sent back to our user from our server.

[00:06:24]
We're gonna write code to investigate. So presumably, all this data is gonna be bundled up on an object inside of, with the help of node, we're gonna look into it. We're not just gonna ignore it. Did you see, everybody, where we ignored it last time? We just sent back generically to Sarah Rose welcome, using the end function.

[00:06:43]
This time, we're gonna look at actually what Michael wants. And from that, determine what we want to attach to the message that gets sent back using the end function.
>> Will Sentance: I have a feeling that's what we're gonna do. He's gonna want tweet number three. That's gonna be hello.

[00:07:03]
>> Will Sentance: Okay, by the time we've finished with this, people, we have most of what it takes for Netflix that uses Node to send you back, I don't know, whatever, what you watch on there. My favorite show's not on there, Superstore is not on. The point of this was to say that we all have almost all it takes for Hulu, shout out to Hulu for having Superstore on it, all it takes for Hulu to be able to send back that video to the user when they wanna see the new episode of Superstore, by the time we've done this.

[00:07:37]
Cuz we're gonna introspect the inbound message asking specifically for the Superstore episode, season three episode 16 right now, the specific episode to be able to send back to the user. That's what we're gonna do here. It's a larger scale, for sure. But it's exactly the same paradigm again and again and again.

[00:07:56]
Let's do it, people. I'm gonna do a board clean, and we're gonna walk through it together, and this time, I'm gonna to call on you quite a lot, people. So be thinking what is each thing doing here? So I'm gonna be calling on you quite a lot, especially Michael cuz that look of concern on his face just inspires so much confidence in me that he is gonna be my guy.

[00:08:15]
I'm very excited, Michael, thank you.

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