1 00:00:00,454 --> 00:00:02,290 Our routes are all written and 2 00:00:02,290 --> 00:00:05,850 we've implemented all the HTTP verbs we plan to handle. 3 00:00:05,850 --> 00:00:07,356 However, there is a problem. 4 00:00:07,356 --> 00:00:12,920 There isn't much code to handle the actions outside of what we planned for. 5 00:00:12,920 --> 00:00:17,060 In the next video, we'll see how to handle the common errors that might come up. 6 00:00:18,370 --> 00:00:20,530 When a request is sent to our API, 7 00:00:20,530 --> 00:00:24,930 it would be nice to get a little more information about it, and 8 00:00:24,930 --> 00:00:29,200 the details about the response the server has to that request. 9 00:00:29,200 --> 00:00:30,140 In other words, 10 00:00:30,140 --> 00:00:34,870 we want an automated logger, something that logs out useful information. 11 00:00:35,970 --> 00:00:39,310 Let's install another piece of middleware to help with that. 12 00:00:39,310 --> 00:00:42,160 We can use a module called Morgan. 13 00:00:42,160 --> 00:00:48,190 We can configure Morgan to log HTTP status codes as well as the routes for us. 14 00:00:48,190 --> 00:00:54,071 In the terminal, run npm install 15 00:00:54,071 --> 00:00:59,537 --save morgan@~1.7. 16 00:00:59,537 --> 00:01:04,117 When that's done, we can move over to our app.js 17 00:01:04,117 --> 00:01:07,723 file in our text editor and include it. 18 00:01:12,310 --> 00:01:17,192 Let's put a required statement with our other required statements at the top 19 00:01:17,192 --> 00:01:19,680 of the code to keep our code organized. 20 00:01:21,480 --> 00:01:28,971 Let's call this logger, and require morgan. 21 00:01:35,054 --> 00:01:41,840 Now we can connect it to our app with use, passing in dev to our logger function. 22 00:01:52,300 --> 00:01:56,691 This will configure the middleware to give us colorful status codes for 23 00:01:56,691 --> 00:01:58,160 our API's responses. 24 00:01:59,600 --> 00:02:03,890 Feel free to check out the documentation from Morgan in the teacher's notes. 25 00:02:03,890 --> 00:02:06,320 Let's build our answers routes now. 26 00:02:06,320 --> 00:02:10,715 When we go to test them later, we'll see what Morgan does in action. 27 00:02:12,901 --> 00:02:15,969 Going back to our routes.js file in the text editor, 28 00:02:15,969 --> 00:02:19,390 let's enter our next route that we planned. 29 00:02:19,390 --> 00:02:21,290 We planned a route to create an answer. 30 00:02:36,439 --> 00:02:40,289 This route is POST 31 00:02:40,289 --> 00:02:47,480 /questions/:id/answers. 32 00:02:49,945 --> 00:02:52,740 We plan to use the post method for this route. 33 00:02:53,890 --> 00:02:58,125 When implementing this route handler, remember to leave off /questions. 34 00:03:03,155 --> 00:03:06,450 Our router is already mounted to questions. 35 00:03:12,512 --> 00:03:17,566 Let's modify the response we say when we're reposting to /answers. 36 00:03:20,130 --> 00:03:22,520 And return the questionId, too. 37 00:03:30,436 --> 00:03:35,210 The next route we can handle is the PUT request to a specific answer. 38 00:03:54,704 --> 00:03:59,939 As we build this route, You might notice a problem. 39 00:04:09,128 --> 00:04:10,221 The problem is, 40 00:04:10,221 --> 00:04:14,450 is that we have two parameters in the URL that have the same name. 41 00:04:15,500 --> 00:04:17,150 This won't work. 42 00:04:17,150 --> 00:04:19,430 One will override the other. 43 00:04:19,430 --> 00:04:21,820 We need to give them different names. 44 00:04:21,820 --> 00:04:22,920 Let's go back and 45 00:04:22,920 --> 00:04:28,835 add updates to all the routes with the question ID with a new parameter of qID. 46 00:04:58,179 --> 00:05:01,635 We can call the answers ID, aID 47 00:05:11,323 --> 00:05:14,254 Let's copy the handler from the post route and 48 00:05:14,254 --> 00:05:18,849 paste it into the put route and modify it with the appropriate response. 49 00:05:25,245 --> 00:05:28,295 And have it respond with the answer ID too. 50 00:05:34,158 --> 00:05:38,451 With the request params aID 51 00:05:46,270 --> 00:05:49,700 We also need a delete handler for this route. 52 00:05:50,730 --> 00:05:52,700 This will delete a specific answer. 53 00:06:08,495 --> 00:06:15,075 Let's update the method we call from put to delete, then delete the req.body. 54 00:06:21,580 --> 00:06:24,980 And finally update the response text. 55 00:06:28,150 --> 00:06:32,820 Last of all, we'll create the routes for voting on answers. 56 00:06:34,200 --> 00:06:36,010 Let's copy and paste this DELETE route. 57 00:06:41,178 --> 00:06:43,880 And change delete to post. 58 00:06:45,800 --> 00:06:49,280 Let's describe what URLs we want handled by this route. 59 00:06:50,430 --> 00:06:53,475 The POST route, 60 00:06:55,180 --> 00:07:03,215 Where we can upvote, And downvote. 61 00:07:10,522 --> 00:07:13,890 These are to vote on a specific answer. 62 00:07:16,622 --> 00:07:22,110 The URLs are identical except for the vote-up or the vote-down at the end. 63 00:07:24,310 --> 00:07:28,380 We can capture these two possibilities with a parameter, and 64 00:07:28,380 --> 00:07:30,110 we can use one route for both. 65 00:07:39,730 --> 00:07:43,390 We can capture only part of the string we want. 66 00:07:43,390 --> 00:07:49,040 So let's grab the up or down from the last part of the route. 67 00:07:49,040 --> 00:07:52,700 We can call the parameter dir for direction. 68 00:07:52,700 --> 00:07:58,143 Now let's modify the response 69 00:07:58,143 --> 00:08:02,774 in the POST request, To 70 00:08:02,774 --> 00:08:09,646 /vote-" and then 71 00:08:09,646 --> 00:08:18,170 the req.params.dir. 72 00:08:18,170 --> 00:08:21,080 Let's also include the vote down here at the bottom too. 73 00:08:29,542 --> 00:08:32,090 Now let's test our routes out. 74 00:08:32,090 --> 00:08:35,260 Switch over to the terminal, stop the server if it's running, and 75 00:08:35,260 --> 00:08:35,960 clear the screen. 76 00:08:38,350 --> 00:08:40,057 Now start up the server again. 77 00:08:40,057 --> 00:08:42,820 In Postman, 78 00:08:42,820 --> 00:08:48,850 let's send a POST request to 79 00:08:48,850 --> 00:08:59,657 localhost:3000/questions/6/answers 80 00:08:59,657 --> 00:09:05,191 with the body prop value. 81 00:09:14,446 --> 00:09:15,779 We see that works. 82 00:09:15,779 --> 00:09:20,800 In the Postman results window, we see the response that we constructed. 83 00:09:20,800 --> 00:09:24,300 Now let's switch back to the terminal to see our new logging in action. 84 00:09:27,575 --> 00:09:33,529 Our Morgan logging middleware has given us the HTTP verb, 85 00:09:33,529 --> 00:09:41,560 the URL requested, the color coded status code 200, among other things. 86 00:09:43,800 --> 00:09:46,300 Let's change our HTTP method to PUT. 87 00:09:49,985 --> 00:09:52,531 Then change the URL 88 00:09:52,531 --> 00:10:00,971 to localhost:3000/questions/9/answers/33, 89 00:10:00,971 --> 00:10:04,320 with the same JSON body. 90 00:10:06,210 --> 00:10:09,040 Hit Send, and let's see what happens. 91 00:10:13,775 --> 00:10:16,640 We get the correct response. 92 00:10:17,720 --> 00:10:23,270 Make sure the questionId is 9 and that the answerId is 33. 93 00:10:23,270 --> 00:10:25,390 Now let's send a DELETE to the same URL. 94 00:10:28,185 --> 00:10:33,551 Check the response is what you expect, that it's a DELETE 95 00:10:33,551 --> 00:10:39,040 request with the questionId of 9 and the answerId of 33. 96 00:10:39,040 --> 00:10:42,366 Finally, let's send a POST request too. 97 00:10:44,729 --> 00:10:53,642 Localhost:3000/questions/5/answers/78/vot- 98 00:10:53,642 --> 00:10:56,346 e-up. 99 00:11:00,112 --> 00:11:03,180 See that the route was read correctly. 100 00:11:03,180 --> 00:11:04,760 Let's check vote-down. 101 00:11:10,449 --> 00:11:11,360 That works too.