Check out a free preview of the full Real-Time Web with Node.js course:
The "Handling Requests" Lesson is part of the full, Real-Time Web with Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle modifies the handleHTTP method to return the correct headers for the incoming request. He also incorporates request routing. For example, responding differently to GET and POST requests.

Get Unlimited Access Now

Transcript from the "Handling Requests" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Kyle Simpson: Now I mentioned that there's a lot more details that you wanna handle when an incoming request is coming in. There's a lot more details to it than just responding on the end. The first one that I wanna point out is that we can control the headers that go back out in the response.

[00:00:18] So we can write the header, we can say writeHead, and the first parameter that we pass is a status code. Since this is a success condition, we would wanna pass the 200 status code most likely. But you can, of course, pass other status codes, and we can also optionally pass other response headers.

[00:00:37] In this particular case, I wanna pass the content type of text/plain.
>> Kyle Simpson: And just to show you that these are in fact real streams, we could do the Hello World,
>> Kyle Simpson: In a write statement, and the end statement would be just the Math.random.
>> Kyle Simpson: So the response stream that we got is a stream that we can add stuff to.

[00:01:18] We start up our server. We restart. Oops, I threw some error, what did I do? I forgot, it's not gonna coerce this for us, so we need to coerce it ourselves. So the best way to do that, simplest straightforward way to do it is to call to string on the number.

[00:01:47]
>> Kyle Simpson: All right, now, just to prove to ourselves that that's actually happening, we can look at the network requests for this, and we can see that the content type was in fact text point.
>> Kyle Simpson: All right, the next thing that we wanna handle is that not all requests are made equal.

[00:02:20] We don't always gonna respond to all requests that are coming in. And so, we have this idea of routing requests to different handlers, and some requests, we're not gonna handle at all. Now there are lots of frameworks out there, and I could spend days and days teaching about express and connect and every other kind of middle framework that's out there.

[00:02:39] But there's already tons of great books and blog posts and all kinds of great free content on that. So rather than us spending our time re treading stuff that's easy for you to read about, I'm gonna show you, again in the spirit of everything that we've been doing, just the core level of how you write this stuff yourself without needing one of those frameworks to do it.

[00:02:57] So the first thing that we can know about is that we've got a request stream, which is gonna give us some properties about the request. For example, it can tell us req.method, and it will be a GET if it was a get request. Okay, now if it's not a GET request, if it's a post or a head or whatever, we don't wanna respond.

[00:03:21] Or we don't wanna respond with the proper thing, so the other way of doing that is for us to say if it wasn't one of those wrecked up methods that we understand. Let's respond,
>> Kyle Simpson: With a 403.
>> Kyle Simpson: You don't have to put that Get outta here part, that's just me being funny.

[00:03:46] [COUGH] But this is gonna return a 403 forbidden, okay? Now it's not all that easy for me to test that part, but we can test this in just another moment. Now let's say, what if we don't wanna handle anything other than the plain old root slash requests? Like if you ask for their path things, we don't wanna handle those.

[00:04:06] So the other thing that we can do is look at req.url. If it is the plain old normal slash, then we wanna handle it.
>> Kyle Simpson: Otherwise, we wanna return our 403 forbidden.
>> Kyle Simpson: Is everybody with me so far?
>> Kyle Simpson: For those of you that are on the live stream, if you can just speak up and let us know if you're following along, if we're going too fast or too slow, or something like that.

[00:05:04] I'd just love to know, make sure since I can't get your feedback, I'd love to know that online, just give me those thumbs up or smiley faces to let me know that you're following along and that things are working for you as well. But here, I'm showing you the beginnings of writing your own sort of routing logic.

[00:05:19] And there are literally a billion different ways that you could write your own routing logic. You can do them with IF statements, you can do them with switch statements, you could have a configuration JSON file that you load into some middleware. You could go the Ruby on Rails approach with things like Connect and Express that are very Ruby in style, Rails in style.

[00:05:41] I mean, there's all kinds of different ways for you to set up your routing. The point is I just wanted to show you that it's really just a question of what was that incoming URL matching it and telling it to do something. So if, for instance, you were using Express, I believe you have an app variable or whatever you call it that represents your application.

[00:06:04] And you listen for GET requests at the slash, and you call some function, so the way you do it with Express, I believe, looks a lot like.
>> Kyle Simpson: Because you get a req and a res, and I think you get some other parameters too, but that's what it might look like if you're using Express.

[00:06:24] You can notice that it's basically the same thing. It's just some sugar wrapped around an if statement essentially.
>> Speaker 2: Hey, Kyle.
>> Kyle Simpson: Yes.
>> Speaker 2: James L wanted to ask at some point if you had a favorite framework.
>> Kyle Simpson: James, can you clarify if you mean favorite middleware framework, like what we've been talking about here, or do you mean JavaScript framework in general, like the backbones and angulars?

[00:06:50]
>> Speaker 2: I think he meant middleware frameworks, something like Express.
>> Kyle Simpson: I don't use any of those middleware frameworks because the middle end architecture that I refer to is simple enough that I write the if statements for myself. But if I were going to turn to a middleware, I'd probably start with Express cuz it's several orders of magnitude more popular than any other one that I've ever heard of.