Check out a free preview of the full The Hard Parts of Servers & Node.js course
The "Node Event Handling in Action" 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 server reacts to a client error and how Node auto-runs the function that was specified to be run.
Transcript from the "Node Event Handling in Action" Lesson
[00:00:03]
>> Will Sentance: Chris? Very good, all right, so it's now a day later, one day later. One day later and Jeff opens his computer. Opens his Mac, and he opens Twitter.com and [SOUND] yeah, Twitter.com, fine, whatever. Okay, out comes his HTTP message, out comes his HTTP message. It is going to be corrupted in some format.
[00:00:41]
There's a number of ways this could be done. It could be a misspelled URL. It could be some unexpected token in the URL. There's a number of ways. It could be, there's some weird stuff that happens with the DNS. There's a whole list of things that can lead to a corrupted client HTTP request.
[00:01:03]
It's coming in, and it ain't happy. It's coming in. It arrives, and there it is. It arrives.
>> Will Sentance: And Node immediately can tell something's up. Or the other way it could be is misformatted JSON data if you're posting, for example. So you actually maybe posting a tweet or miswired JSON data.
[00:01:25]
We'll see a bit more about JSON data, sorry, in a moment. I forgot to mention that. Yeah, we'll see more about that in a moment. But somehow it's corrupted, what message within node is gonna be flushed out, everybody?
>> Speaker 2: Client error.
>> Will Sentance: Client error. Exactly, Node's gonna have a look inside.
[00:01:44]
>> Will Sentance: And it's gonna go! That's glasses and eyes, just really trying to, I realize it's not the best sketch ever. Now it looks like a Ninja Turtle.
>> Will Sentance: I think, I don't know, I don't really know about ninja turtles, but I think they may have masks. I'm not sure.
[00:02:04]
And it's looking in, and it says corrupt. We don't want this being passed into JavaScript. It could cause all sorts of issues particularly, by the way, causing an error that breaks our server. We don't want this being parted in JavaScript and trying to part, passed into JavaScript and trying to parse it in JavaScript and then be misinterpreted, right?
[00:02:27]
Unfortunately, British people pronounce those words exactly the same way, which is rather unfortunate. I recognize that, that's definitely an issue with our language design. So it wants to instead have us, have it handled. Well, thank goodness. It's gonna see the bad message, it's gonna flash out the message client error, client error, client error.
[00:02:51]
The client is Jeff sending a bad message. Well, it's not even his fault, it's really DNS that did it. But some issue, and that's gonna trigger, do an error running. Gonna be, but of course, it's still gonna have data because all of these functions can't do an error, it's expecting input as well.
[00:03:10]
So what's the auto created and inserted data, known everybody as an?
>> Speaker 2: Argument.
>> Will Sentance: Argument. Known everybody as an?
>> Speaker 2: Argument.
>> Will Sentance: Auto inserted data into a function being run is known as everyone together, as an?
>> Speaker 2: Argument.
>> Will Sentance: So, what is an argument, it's auto inserted?
>> Speaker 2: Data.
[00:03:39]
>> Will Sentance: Matt?
>> Matt: Data.
>> [LAUGH]
>> Will Sentance: Yeah, yeah, yeah, that's fair. All right, there it is, that's good. And so the auto create an auto insert data is a special type of object in JavaScript. No, but JavaScript as a whole called the error type object, just an object, but it's formatted such that if you console logged it, it comes out with some stack trace.
[00:04:01]
That means where was this error show up, maybe the path, some information about where the error showed up. That is gonna have information in it, a number of different pieces of information. Due one error's gonna run, that's gonna be its auto input, so let's auto execute. We're what, we're one day later, aren't we?
[00:04:24]
>> Will Sentance: One day later.
>> Will Sentance: And we're gonna auto execute due on error. And its input is gonna be this object. Doesn't have a label, it's just in the error object format. That's the error object format. It gets inserted, it has information on the error. it's auto executed everything in purple, it's done automatically by Node.
[00:04:49]
And we go inside of it, and we have a parameter. What's the parameter,Mohamed? It is one.
>> Will Sentance: What's the parameter, Sarah Rose, of due on error?
>> Sarah Rose: Info and error.
>> Will Sentance: Info and error because we need, Mohammed, to have a label that lets us use the data here.
[00:05:12]
And then, the error object, it arrives in that parameter. So how do I get to access, what label do I use, Zet, to access this error object?
>> Speaker 5: Info an error.
>> Will Sentance: Info an error. That's all a parameter is is a placeholder that gets filled in with data automatically, known, everybody, as an?
[00:05:32]
>> Argument.
>> Will Sentance: Good job, well done, everyone. That were really quick. Yeah, and then we console log. So let's just do our console. Also, by the way, of course, people, not a feature of JavaScript, but we'll just do it in black anyway over here. And we're going to use the way of logging to the console.
[00:05:50]
We could use console.log, but if you do console.error, it's expecting a Java's error object, and it'll do a pretty, easy to read format. And so it'll take console.error, take this data as the input, and log over here our exact error details. And we can now introspect and look at that and go, corrupt request.
[00:06:16]
Now, as a quick side thing, people, you do usually still want to send something back when that happens, like some indication. Some people say status codes in HTTP. It's another that indicates, is meta information about whether the server's working. What sort of data's being sent back? Not what sort of data is being sent back, usually, it's mostly about things, information about how the server's working, and particularly things like errors, the ones beginning with four.
[00:06:47]
So we’re gonna want to send back here for a bad request from the client, a corrupted request, does anyone know what status code we send back?
>> Speaker 2: 400.
>> Will Sentance: 400, yeah, fantastic, 400. So we’re gonna want to send that back because Node doesn’t even want to give us access to this request in an object format, all it wants to give you is the error.
[00:07:08]
Cuz you don't wanna just get a corrupted thing with a URL slightly miswritten, and stuff like that. It doesn't even give us access to that. It doesn't let us do that directly. Instead, I'm just gonna give you a little bonus. I wasn't gonna tell you this, but it's a little bonus.
[00:07:21]
It actually inserts, as a second argument, the raw access to the socket, not an HTTP format, but just the socket to send, it's the data that's gonna be sent back over the socket. It's not in HTTP format. It's just a raw piece of data that we can send back.
[00:07:42]
And we do get access of that actually again as the second auto inserted input, but it's really raw. You've got to add literally, you've got to write the words of the HTTP formatted. You've got to write get, or not, sorry, not get. But you've got to write literally the full HTTP formatted message by manual hand, and attach it.
[00:07:59]
So the second argument gets passed in is actually, this message as a whole for one. So it gets passed in directly as a second argument, and we have to, sorry, we have to, it's not HTTP message even. It's just the raw data that's gonna be sent back. We have to add text, a string of characters that turn it into the HTTP format inside of here, sending back something like, what?
[00:08:27]
What would we send back?
>> Speaker 2: Error codes.
>> Will Sentance: Status code 400, exactly. So it does actually have a second auto inserted input, and it's the raw data that's gonna be sent back, and we can append to that status code 400. Like HDP status code 400 and send that back.
[00:08:43]
And over here, they'll get a 400 error, and so the user will know to complain, or whatever the situation is. Okay, but more important than that, really, was again, we now have a second function that can auto run. And we rely on Node to peal out, referencing bells, peal out, announce the message, announce the event, the message that's appropriate for the given inbound request.
[00:09:14]
Whether it's corrupted, it's going to announce client error. Don't panic, we said on client error, auto run please, Node, due on error, and then we handled it.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops