HTTP Status Codes5:40 with Treasure Porth
Providing the client with informative HTTP status codes is part of building a useful and usable API.
You've probably seen a 404 page when browsing the Internet. 0:00 This status code means the server couldn't find the requested resource. 0:03 HTTP status codes give the client important information about the request 0:07 and response cycle. 0:11 In this video, we'll use some of the most common http status codes. 0:13 Generally, the status codes you'll use the most are in the 200, 400 and 500 range. 0:16 Status codes in the 200s mean everything went as expected. 0:22 400s means something went wrong with the request, 0:26 like a missing resource or the wrong information was sent. 0:29 500s means something went wrong with the server. 0:32 Something unexpected happened that prevented the server from fulfilling 0:35 the request. 0:39 Some of the most common status codes are 200- 0:40 OK meaning the request was successful. 0:43 201- Created, meaning a new resource has been created. 0:46 400- Bad Request, meaning the request was malformed or information was missing. 0:50 404- Not Found, meaning the resource doesn't exist. 0:55 500- Internal Server Error. 0:59 See the teacher's notes for a list of other common status codes. 1:02 >> In app.js, scroll down to your /quotes post rout. 1:05 Let's send an error back in here and attempt to create the quote again. 1:10 Save, make sure your server is running and go to postman. 1:20 Try to create the quote again, and if we scroll down to the response, 1:23 you'll see that the error message is fine, but we have a status code of 200 OK. 1:28 That means that everything went just fine, no problems. 1:34 That's the code that Express sends by default. 1:37 But we have the power to choose which status codes we send in case of error or 1:39 to give consumers of our API useful information about the requests. 1:44 We can send a different status code by using the status method. 1:48 Just before we send our error message back as json here, 1:52 we can say res.status(500). 1:57 And that indicates that something went wrong with the server and 2:01 we can actually chain these methods so let’s do that. 2:05 Go back to postman and send the request one more time. 2:12 Our express application is now responding with the status code of 500. 2:15 There's also a better status code we can with this post request, 2:21 if the request is successful. 2:25 If everything goes as expected, it's convention to send back a status 2:26 code of 201, which means a new resource was created. 2:31 So here we can say, I'll delete this error first and 2:35 we can say status 201. 2:41 We can go back to postman, send a request again and 2:48 you can see we're sending a status of 201 created. 2:51 On the subject of status codes there's some error handling we could do 2:54 in our single quote route. 2:57 Let's go back up to our get method that returns a single quote. 2:59 First let's set the status of the error in the catch block to 500. 3:05 Currently, if we go to postman and request a quote with an ID number that doesn't 3:14 exist, we get back nothing, a blank screen. 3:19 Let's change that so that we send an error message and a proper status code instead. 3:21 So I'm going to set this to GET, and 3:26 let's request a jumble of letters and numbers. 3:30 And we can close this for now. 3:36 So I'm going to change this to none. 3:38 And as you can see, we didn't get anything back, but 3:40 we're getting the status code of 200. 3:43 Going back to app.js, under the line that retrieves the quote, 3:45 we'll write a simple conditional. 3:49 If the quote exists, We'll send back the quote as JSON. 3:52 So I'll just copy and paste this line up here. 3:57 Remember, Express sends a status code of 200 by default, so 4:02 we don't need to set the status code here. 4:06 So else, if the quote doesn't exist, we'll set the status code to 404 not found. 4:09 And send JSON containing an error message saying the quote wasn't found. 4:18 Let's go back to the create quote route handler and do something similar. 4:27 Go to that route and here we'll check that an author and a quote have been submitted. 4:31 So here we can say if req.body.author. 4:35 And req.body.quote. 4:42 Then we can go ahead and move this code inside of this block. 4:50 Else, if one of these properties is missing, 4:57 we can set the status code to 400. 4:59 And send back an error message. 5:04 Don't forget the message property. 5:11 A status code of 400 means bad request. 5:12 In other words something's wrong with the syntax of the request, 5:15 information was missing, or some sort of information validation has failed. 5:19 Sending proper status codes is a crucial part of building a useful and 5:23 usable restful API. 5:28 Even if you're just building an API for yourself, it can be helpful to get 5:29 confirmation that everything happened as expected or that something went wrong. 5:33 Now unto building our update and delete routes. 5:37
You need to sign up for Treehouse in order to download course files.Sign up