API Design in Node.js, v5

Requests and Responses

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Requests and Responses" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott explains that requests to unhandled routes cause 404 errors, while not responding can hang the server. He emphasizes understanding HTTP as a client-server system and the constraints affecting real-time communication and dynamic Express paths.

Preview
Close

Transcript from the "Requests and Responses" Lesson

[00:00:00]
>> Speaker 1: So let's say I want to do a post request, right If I say let's do a post request to slash, I don't know, you want to create a cake, right

[00:00:00]
So I do this And I'm gonna say, actually, I'm not gonna put anything here just so I want you guys to see what's gonna happen because I want to talk about that too

[00:00:00]
So if I do that, start my server And I'm gonna open up Postman and I'm gonna say, "Cake." I'm gonna keep it a GET

[00:00:00]
I had it as a post here, but I'm gonna run it as a GET Anybody know what's gonna happen if I do that

[00:00:00]
No, any guess Or would you expect to happen Maybe that's a better question We've all interacted with servers before

[00:00:00]
What would you expect to happen if you tried to hit a server with a route and a verb combination that it does not listen for

[00:00:00]
Yeah, I think you would expect a 404 Let's see what happens Cannot GET cake, and if we look right here, you can see we got a 404 not found

[00:00:00]
By default, Express will just send back a 404 if you try to access a route in which you did not register for, so by default you get a 404

[00:00:00]
404 is a status code I know I have a section here about status codes, but 404 is a status code That means whatever you're asking for doesn't exist

[00:00:00]
That's what that means Anything in the 400 range means your request is jacked up That's what that means

[00:00:00]
Anything in the 500 range means the server is jacked up Anything in the 200 range means, yeah, this is probably good, and anything in the 300 range is like, yeah, this is probably good, but it's usually something like caching or redirecting or something like that

[00:00:00]
So 400 means that person messed up, your server's good to go, assuming you wrote your code right So, let's change this to a post now

[00:00:00]
I put a function here, but I'm not responding What do you expect to happen now if I were to do a post request to this route and a server is handling the route, but it doesn't actually respond

[00:00:00]
What do you think would happen And we've all seen this error before, a million times I promise you you've seen this error

[00:00:00]
But now it's going to click to you why this error happens You like YouTube Okay, it will spin, that's called hanging

[00:00:00]
Does it hang forever Timeout, exactly We've all seen this error Oh, service timeout or there's a timeout issue

[00:00:00]
Yeah, that's eventually what's going to happen here, so it's going to do both, it's going to hang, which is what we're seeing right now

[00:00:00]
This is called a server hanging if you don't respond back to a request It's just like, "Oh, okay, well, I'll just sit here," and depending on what defaults you have and what other configuration options you set up, it'll eventually time out

[00:00:00]
I have no idea what the default timeout is for Express, or if there is one But if you saw the timeout issue from a service, it's because they knew that either A, something was hanging, and they caught it and sent back a timeout issue, or B, it hit their threshold of just taking too long and that automatically triggers a timeout

[00:00:00]
But yeah, we've all seen this issue if you're streaming anything on YouTube or sometimes even Netflix, you might see a timeout issue

[00:00:00]
[The transcript continues in this manner, maintaining the speaker's voice and technical explanations while cleaning up speech patterns and minor errors.]

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