Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed REST APIs with Express!
You have completed REST APIs with Express!
Preview
In this video, we'll add functionality to our app that will allow the client to request a random quote.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Before I show you how to
create a random route,
0:00
I encourage you to give it a try yourself.
0:02
Look for the correct method to use
in the records.js file, look at code
0:04
we've written so far to remember syntax
and how things should be structured and
0:08
go ahead and
write the route in the routes.js file.
0:12
Pause the video, give it ago, and
remember, this challenge is open book.
0:15
Howβd you do?
0:21
If you got stuck somewhere along the line,
donβt sweat it.
0:22
Watch my solution, finish the course,
sleep on it, come back and try again.
0:25
If this is pretty new to you, youβve
absorbed a ton of information throughout
0:28
this course and sometimes your brain just
needs a little time to process it all.
0:32
Okay, so letβs dig into it.
0:35
Iβm simply requesting information,
so Iβm going to write a GET request.
0:37
I'm going to put our new GET
request route just underneath
0:41
our other two GET requests.
0:45
So right after the route
that gets a specific quote.
0:46
And I will go ahead and
move our comment, Up here.
0:50
I'll start with router.get.
1:03
And we want to respond to a get
request to /quotes/quote/random.
1:05
The next parameter is an anonymous
function that accepts the request,
1:13
response, and next objects.
1:17
Now I'll set up a try catch block, passing
any errors to our global error handler.
1:25
Inside the try block, I'll await the
results of the records.getRandomQuote()
1:37
method, and
save it to a variable called quote.
1:42
Finally, I'll send back a response
containing the random quote as JSON.
1:53
I can also route my callback in
the asyncHandler function so
2:00
I don't have to use tryCatch.
2:03
Let's make sure the server is running,
go to Postman, and test this out.
2:16
Remember, I've set up a router so that
all requests must now be sent to /api.
2:20
So I will send a get request
to /api/quotes/quote/random.
2:25
And fantastic,
we are receiving random quotes.
2:37
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up