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
We'll continue to use the records.js module to create an endpoint to retrieve a specific 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
Let's refactor our other
GET route in a similar way.
0:00
We know that we want this route
to respond with a specific quote.
0:02
The client will send us a request
containing the unique ID number of
0:06
the requested quote.
0:10
We'll use that number to find
the correct quote in our data store, and
0:11
then we'll send that
quote back to the client.
0:15
Make sure that you've deleted the code
inside of this route that returns a single
0:18
quote, and type records.getQuote.
0:22
If we hover over getQuote, we can see
that it takes one parameter, an ID.
0:27
Remember that each quote
contains a unique ID number.
0:32
This function takes that unique ID number,
finds the right quote and returns it so
0:35
that our API can then
return it to the client.
0:40
An ORM, like Sequelize or Mongoose,
would do something similar.
0:43
Provide some kind of function that can
find an item in a database by its unique
0:46
ID number.
0:51
First, we'll need to know the ID of
the quote the client is requesting.
0:52
We can do that in a very similar
way as we've already done.
0:55
The client can send the ID and we can then
pass the ID to the getQuote function.
0:58
How do we pass the ID to the function?
1:03
Just as we did before,
we can access it through req.params.id.
1:06
And we can store the result
in a variable called quote.
1:13
Then we can send that quote
back to the client as JSON.
1:17
We need to make sure
that JavaScript waits for
1:24
records.getQuote to return the data before
we send the response back to the client.
1:26
We'll do that with async and await.
1:30
If you want, pause the video, look at the
other GET route we've already written, and
1:32
see if you can sus out how
to use async await here.
1:36
Remember, if I'm going to use await,
1:39
I need to use it inside
of an async function.
1:41
So I'll mark this anonymous callback
function as an asynchronous function.
1:43
And then we need to await the retrieval
of the quote we want to send back to
1:51
the client.
1:54
The code we've written here is
going to take whatever is passed in
1:57
after the /: and feed it to this function,
2:00
which in turn finds the correct
quote from our data store.
2:03
Let's test this out, save and start
your server if it's not already started.
2:07
Open up the browser and navigate to
the application at localhost:3000/quotes.
2:12
You can see in the browser,
I have my JSON with all my quotes.
2:17
Let's request a quote with an ID of 8721.
2:21
And everything's working great.
2:27
You'll notice that if I request
an ID that doesn't exist,
2:29
like a jumble of letters,
that nothing happens.
2:32
We'll talk about how to handle potential
errors such as these later in the course.
2:36
We've covered a lot in this course.
2:40
We've learned how to write GET routes
using a data store and module,
2:42
as well as a bit about how to handle
asynchronous JavaScript in our code.
2:46
Now it's full steam ahead to build
the rest of our application.
2:50
Routes to add, edit, and delete quotes.
2:53
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