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

The "HTTP Request & Response Q&A" 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 fields questions about data handling, where to declare get/post methods, errors during JavaScript execution, and the request method.

Preview
Close

Transcript from the "HTTP Request & Response Q&A" Lesson

[00:00:00]
>> Will Sentance: Michael, why don't you take it away? You've had some excellent clarification, so take it away.
>> Speaker 2: Does all the data from the computer arrive at once, or does it?
>> Will Sentance: Behind the scenes, there is a way of parsing data over HTTP in what's called chunks. But that is, even if we do want to take that into account and start handling data sooner, or start handling data in chunks, we're gonna have a way of doing that that Node is gonna give us access to.

[00:00:32]
We're gonna see that, I think you kind of got a hint of this, from me saying, we get more sophisticated at how the data comes from the internal feature into JavaScript to be worked on. We can start handling it in batches. And after the break, we're gonna see handling data in batches, not in one big load.

[00:00:48]
But this is so tiny, man, that it's not gonna be in batches. This is just straight in, but yes, in HTTP, you're gonna learn they're called streams of data. I don't love the name because it sounds like it's all happening, the zeroes and ones in a flow, whereas in fact, it's chunk, chunk, and so forth.

[00:01:09]
Okay, we'll see more of it soon. Thank you, Michael. Sarah Rose?
>> Speaker 3: So Node is running our, I maybe should have I asked this farther back. Node is running our due on incoming for us, is that happening in our client in the browser? Is that happening on, if that question makes sense.

[00:01:30]
>> Will Sentance: So everything on this side is Twitter's computer, it's the computer connected to the Internet ready to receive messages from people like Sarah Rose, and so everything here is on Twitter's computer. Excellent.
>> Will Sentance: Bhavit, you had a question.
>> Speaker 2: So we just wrote for a get service call, right?

[00:01:50]
So if you have to change, or do we have to declare all get post methods in the beginning, or can we change later?
>> Will Sentance: We're gonna discover in a second, any time we want to make any changes to the computer's internal open socket in the Node pre-setup feature, any changes, if we wanna come in and edit our JavaScript, we have to turn Node off and back on again and reload everything.

[00:02:26]
>> Will Sentance: You can imagine Node being adjusted such that changes from here that were non-breaking to existing things you set up down here. You could make them without having to turn Node off and turn it back on again. But the standard is any changes you make up here are gonna be too breaking to down here.

[00:02:49]
So you always have to layout all your jobs from code, handle the get, handle the get at this address, handle the post at this address, post at this route, this path. You have to do them all at once, run through it all in JavaScript, use that all to setup the background stuff, and then you're ready to go.

[00:03:05]
You can't later come and run more JavaScript code to setup more stuff back here. You gotta do it all at once. We're gonna start to see that now. Any more clarifications with that? Yeah, Jeff?
>> Speaker 2: So if we send in the URL tweets/A say, and this isn't gonna work and it's gonna throw an exception.

[00:03:26]
Are we gonna cover what happens when there's an exception? Because I'm curious because what I've seen in my own experience is if you don't handle it, the browser stays connected and it just stays there in that state.
>> Will Sentance: Absolutely, absolutely, and we are,
>> Will Sentance: Going to see how to handle errors.

[00:03:47]
In fact, I have a slide coming up that says well, let's see what it says.
>> Will Sentance: Yeah, it's so innate because you're dealing with someone else's computer. We have to have a really sophisticated system for handling errors, yep. Okay, all right, yeah.
>> Speaker 2: Super quick question, it's kind of a dumb question.

[00:04:13]
>> Will Sentance: I doubt it, I'm sure it's a very insightful question.
>> Speaker 2: So let's say instead of when I display hello, you just wanna display the matter that HTTP request used. So all I have to do is literally just incoming data that mattered, and I'm done.
>> Will Sentance: Done, well, you wanna console log it, right, because you wanna display it to the console.

[00:04:29]
You wanna log it to the console, yeah, console log incoming data.method, spot on. Incomingdata.method, you would see get, the string get in.
>> Speaker 2: It's just the property of the object pretty much.
>> Will Sentance: Yep, in fact, this should be a string.
>> Speaker 2: Got you.
>> Will Sentance: It's not a string here, here it's just a text file, it grabs the get bit.

[00:04:47]
It's always in the same place in the inbound message and sticks it in as a string on the object that gets inserted. Okay, all right, folk, so now we're about to come to a break in about five, ten minutes during which we're gonna pair program and take lunch.

[00:05:01]
To do so, we need to understand how the hell do I run this? It looks all very nice. It's amazing how easy it is to run code on a whiteboard. Extraordinary. You just write it, and it runs. It's fascinating. But on our computer, we need to be able to run JavaScript, turn on Node, or turn on Node that opens up a JavaScript engine.

[00:05:21]
Runs that code, switches on Node feature that switches on computer's internal features. How the hell do we do this? Well, let’s see.

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