So handled true, it doesn't necessarily mean that the response has been sent. It just means that we have called a handler and we will make the assumption that it has handled the response.
All right, I think we're all caught up. Like I said, I put all the code in there. I put the code for the server in there, as well, the server file. And I think we are ready to go onto the next part.
So what were you thinking for the next part? Well, the first thing I'm gonna do, I'm load testing this so we can see what's happened so far. So remember, we were getting like 200 and something thousand requests. Look at where we're at now. We're at 176,000, 17,000 instead of 25. So that's per second, 17,000 requests per second. So what has slowed this thing down? Well, it turns out that as you start processing these requests and doing things like creating the URL here, this right here, is enough to slow it down that much, just creating the URL variable. Or instantiating that class, right? So, say that's why when you're building these frameworks, you have to continuously load test them to make sure you didn't do anything crazy. Now, this is on purpose, like we want to do this. We don't, there's nothing we can do about it. We want to create an interface in our framework that is more approachable than just the raw, here's a string for URL and making people handle it. So that's something that we're willing to deal with. We want to keep track of this over time to make sure that we're not degreating the performance much more than that.
All right, so now, let's add another route just for fun. We can just add another one and say, server.all. So this is cool. Now we have like a functioning server. I'll put, I'll do slash hello. I'll grab a requests response next and we'll just respond with just world this time instead. So say response.send a world. So I got a slash hello now. I should just get a world. So let's see. There it is. That's pretty cool. I mean, it's not an insignificant amount of code but it's also not that much code now build out a router that works for a single handler. That's kind of neat.
So what won't work right now is what we want to be able to do is have a logger. Like the main, one of the primary things that you end up doing with middleware is to have a logger. Another thing that we'll do before that is that that's gonna get the most complex. So why don't we go ahead and process the body? So let's support a post request. Yeah, that makes sense. Now that's probably a better thing to do first. So what we'll do is we want to do something like this. We want to go to like slash JSON and then we wanna be able to get our request response next. And then we want to be able to just respond with that request body. So we want to be able to say like dot body and then stringify that or parse it. Or we actually want our response obviously have more than just send. We want to be able to say response.json and pass in the body. Right? So we want, it's this right here, when we do that send, we're just sending a string in the body. But we want to be able to take an object and send that as a response. So you could think about like, or creating a REST API. We don't want to have to make every route that we have, like, take our object and stringify it and send it back. We just want and we don't, then we'd also have to set the headers for application JSON. So let's go ahead and add to our response object a JSON function. So we'll go down to response right here, or class. I really should have put all these more files. That's like, crazy now. So we're gonna pass in an unknown object. So we were expecting an object at this point. And then we can pass in a status and headers. Keep me honest here, Will. I'm not looking at any notes, so.
All right, then we'll, from here, we'll call this.send. And we need to do json.stringify on our object. And then we're just passing the status and the headers. So now we've created a JSON function on our response object, where we can pass in any object. We'll stringify it and send that out. And part of the headers we want to add is headers.built. I think to add... Yeah, so for this one, I don't think we take in headers. Let's not worry about that for now. You just, yeah, just send the content type through. Yeah, so here we'll just do content type, application.json. So you want to tell, we're gonna add this header that says that, hey, we're sending you JSON back. That way, the browser knows and everything. It's not just a string. It's actually JSON data. And then we use our send method to do it. So that way, anytime we want to change how we're sending data, we only have to do it in one place, right? Yeah, but for this, we want to change the requests so that the request can parse the body. That's right, yep. So now we're expecting on this, whenever this request comes in, we're expecting that we're gonna get JSON or something there. So we need to process the body. It may not be JSON. It could just be a string but we need to process this. So we're going to say, let me pull this up because I always get this wrong. Yeah, it's gonna turn into an async function and then we're gonna have to use the text decoder to decode the body and then parse it. And we can be, just make assumptions for now that you're always gonna send a parsable JSON text in your request. But this is where, in Express, if you've ever used the body parser, that's what we're coding here. And the body parser can accept all different types, body parser can do all different types of request bodies, but we are just going to process JSON request bodies. So now we're gonna say body equals decoder dot decode raw value, yeah, and then we want to try to JSON parse this essentially, and if it's successful, fine, if not, we don't really care, it's fine, we're just gonna return as a string, essentially, so person ignore this error because it's not a real error, we just wanna give it a shot and say, hey, this could be JSON, let's see if it is or not, if it is, we're gonna parse it, if not, we just leave it as is and it might just be undefined depending, and I wanna show you what this does to the performance too, so before we do anything, Oh, let's have an error somewhere, let me see. Yeah, so now you have to await it down below. Okay. So, await on line 83. Wait no, but I want, I want, oh wait.
Comments