Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll learn how to use a module to retrieve information from our datastore.
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
Express provides methods for
handling requests,
0:00
like the GET method we've been using so
far.
0:02
And the POST, PUT, and
DELETE methods we'll learn about later.
0:05
But it doesn't have any hard and
0:08
fast rules about what you do inside
of these methods' callbacks.
0:10
You have the flexibility to
use whatever data store, file,
0:13
database, et cetera that you wanna use.
0:16
At the moment, we've got some hard-coded
data right here in our app.js file,
0:18
which we're sending to the client
when a request is made.
0:23
This is not ideal, because it's likely
to become unwieldy as our project grows.
0:26
So instead, we'll keep the data
in its own file, data.json.
0:31
So let's delete the data in app.js.
0:35
We'll also delete the code inside
of both of our GET routes.
0:41
Next, we'll import the records.js
file into app.js so that we can
0:49
use the methods it provides to get our
list of quotes from the data.json file and
0:52
send it back to the client.
0:57
At the top of the file,
I'll import records.js and
0:59
save it to a variable called records.
1:02
I can now type records
anywhere in this file.
1:12
I'll put it inside of our first GET route.
1:15
I've made comments to
the records.js module using JSDoc,
1:20
which is a way to annotate
JavaScript files.
1:24
Thanks to the magic of my text editor,
we can read about the methods contained
1:27
in this module without having to
dig around in the actual file.
1:31
Instead, we can hover
over the word records.
1:34
We have six methods at our disposal,
getQuote, getQuotes,
1:37
createQuote, updateQuote,
deleteQuote and getRandomQuote.
1:42
Iβm using Visual Studio Code,
1:47
which is an excellent free text
editor that I definitely recommend.
1:48
And thatβs whatβs allowing me to hover and
read about these methods.
1:52
If your preferred editor doesn't do this,
don't worry about it.
1:56
You can read the exact same documentation
inside the records.js file.
1:59
Or just read it along with me and
2:03
you should be able to follow
along with no issues.
2:05
Okay, we wanna respond to a GET request
to the quotes route with an entire list
2:08
of quotes.
2:12
I can do that using the geQuotes method.
2:13
And I'll save those records
to a variable called quotes.
2:20
If I hover over the getQuotes method,
2:25
I can read a description
of what the method does.
2:27
So, it gets all quotes and
it takes no parameters, excellent.
2:30
Now that we have a variable
containing all of our quotes,
2:35
we can send it back to the client on
the request object with the json method.
2:38
We're doing pretty much exactly what
we did before with our previous code.
2:46
Only now we're using the records.js module
to abstract away the logic of getting
2:50
information from our data store.
2:54
I do have a bit of bad news for
you, though.
2:56
If you start the server and make a GET
request to localhost:3000/quotes.
2:59
It turns out we're responding
with an empty object.
3:07
It's not quite working yet,
because we need to do one more thing.
3:10
We'll talk about that in the next video.
3:13
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