1 00:00:00,320 --> 00:00:02,270 Before I show you how to create a random route, 2 00:00:02,270 --> 00:00:04,390 I encourage you to give it a try yourself. 3 00:00:04,390 --> 00:00:08,670 Look for the correct method to use in the records.js file, look at code 4 00:00:08,670 --> 00:00:12,540 we've written so far to remember syntax and how things should be structured and 5 00:00:12,540 --> 00:00:15,395 go ahead and write the route in the routes.js file. 6 00:00:15,395 --> 00:00:18,460 Pause the video, give it ago, and remember, this challenge is open book. 7 00:00:21,540 --> 00:00:22,440 How’d you do? 8 00:00:22,440 --> 00:00:25,060 If you got stuck somewhere along the line, don’t sweat it. 9 00:00:25,060 --> 00:00:28,860 Watch my solution, finish the course, sleep on it, come back and try again. 10 00:00:28,860 --> 00:00:32,050 If this is pretty new to you, you’ve absorbed a ton of information throughout 11 00:00:32,050 --> 00:00:35,700 this course and sometimes your brain just needs a little time to process it all. 12 00:00:35,700 --> 00:00:37,300 Okay, so let’s dig into it. 13 00:00:37,300 --> 00:00:41,170 I’m simply requesting information, so I’m going to write a GET request. 14 00:00:41,170 --> 00:00:45,050 I'm going to put our new GET request route just underneath 15 00:00:45,050 --> 00:00:46,930 our other two GET requests. 16 00:00:46,930 --> 00:00:49,380 So right after the route that gets a specific quote. 17 00:00:50,770 --> 00:00:56,746 And I will go ahead and move our comment, Up here. 18 00:01:03,028 --> 00:01:05,870 I'll start with router.get. 19 00:01:05,870 --> 00:01:13,560 And we want to respond to a get request to /quotes/quote/random. 20 00:01:13,560 --> 00:01:17,236 The next parameter is an anonymous function that accepts the request, 21 00:01:17,236 --> 00:01:18,835 response, and next objects. 22 00:01:25,530 --> 00:01:31,968 Now I'll set up a try catch block, passing any errors to our global error handler. 23 00:01:37,756 --> 00:01:42,619 Inside the try block, I'll await the results of the records.getRandomQuote() 24 00:01:42,619 --> 00:01:45,439 method, and save it to a variable called quote. 25 00:01:53,113 --> 00:01:56,785 Finally, I'll send back a response containing the random quote as JSON. 26 00:02:00,480 --> 00:02:03,640 I can also route my callback in the asyncHandler function so 27 00:02:03,640 --> 00:02:05,100 I don't have to use tryCatch. 28 00:02:16,790 --> 00:02:20,750 Let's make sure the server is running, go to Postman, and test this out. 29 00:02:20,750 --> 00:02:25,690 Remember, I've set up a router so that all requests must now be sent to /api. 30 00:02:25,690 --> 00:02:33,328 So I will send a get request to /api/quotes/quote/random. 31 00:02:37,034 --> 00:02:40,433 And fantastic, we are receiving random quotes.