Planning out a REST API3:18 with Treasure Porth
Let's plan out our REST API application by mapping HTTP methods to CRUD operations.
We can plan a RESTful application by describing what we want it to do 0:00 in plain English, 0:04 using the most common HTTP request methods, get, post, put, and delete, and 0:05 mapping them to CRUD operations, create, read, update, and delete. 0:10 Let's start to break this down in our app.js file. 0:14 We want clients to be able to use our 0:17 API to Send a GET request to READ or view a quote. 0:22 Send a POST request to Create a new quote. 0:29 Send a PUT request to UPDATE or edit a quote. 0:34 And send a DELETE request to DELETE a quote. 0:38 Using Express, we'll write code to respond to these requests. 0:42 This is a good basic list but applications are often more complicated then this. 0:47 What else might we want to do? 0:55 Let's add a second GET request to read all of the quotes. 0:57 Users of the API may want to display a whole list of quotes rather than just one. 1:02 So up here at the top, we'll say, 1:06 Send a GET request to READ a list of quotes. 1:11 Finally, we want the API to send back a random quote, so 1:16 we'll add one more GET request. 1:20 We're going to handle this route last, so I'll add it to the bottom of the list. 1:22 We want users of our API to be able to send a GEt 1:28 request to Read, or view, a random quote. 1:32 This is a good plan so far, but let's not forget the nouns we discussed earlier. 1:37 We use nouns to describe the resources, or data representations, 1:41 that the client can request from the API. 1:46 A resource can be any number of things, users, customers, books, horses, cats. 1:48 In our case, it's quotes. 1:53 So we want the client to be able to send a GET request to 1:55 /quotes to READ a list of quotes. 2:00 To view a single quote, the client will send a request to /quotes/:id. 2:03 Remember, the colon here indicates that :id is a representation, 2:10 or a parameter, of the actual endpoint. 2:15 To get a single quote, the client must request a quote by its unique ID, 2:18 so quote/1234 for example. 2:24 We’ll talk more about this later. 2:27 So the client will send a POST request to /quotes to create a new quote, 2:27 send a PUT request to /quotes/:id to UPDATE a quote. 2:35 Send a DELETE request to /quote/:id to DELETE a quot. 2:42 And to read a random quote, we'll send 2:47 a request to quotes/quotes/random. 2:53 Notice that many of these endpoints are the same. 2:58 That's because each endpoint can have multiple HTTP methods. 3:01 The HTTP method, the action, will tell Express what to do and 3:05 how to respond to the request. 3:10 Our REST API is planned and we're ready to get going. 3:12 In the next video, we'll write the first couple of routes. 3:14
You need to sign up for Treehouse in order to download course files.Sign up