This course will be retired on August 10, 2021. We recommend "REST APIs with Express" for up-to-date content.
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
Using Express, we begin to build our Question routes.
Let's start by creating
a module to handle the routes.
0:01
First we'll create a new file and
save it to the root folder as routes.js.
0:04
At the top of the file don't
forget to put use strict.
0:11
Now let's bring in Express in
requiring the Express package.
0:19
var express = require("express").
0:25
Then we can setup a new router.
0:35
You can read more about the express
router in the teacher's notes.
0:39
The router object is what
we'll pass into the main app.
0:49
To do this at the bottom of
the file we'll export the router.
0:52
Let's save that and move back to the
app.js file to connect the two together.
1:06
Underneath these two dependencies,
1:13
let's bring in the router
module that we created.
1:16
Let's use the variable routes.
1:28
And require our routes file.
1:33
Routes can be connected to the app
in the same way as middleware
1:48
using the use method.
1:53
We only want the router to kick in
when any route starts with /questions.
2:01
So we'll pass in the string
/questions as the first parameter.
2:08
When a request comes in for
2:15
any route that starts with questions they
will be ushered into the routs module.
2:17
The routes module will try to apply one
of the handlers were trying to code.
2:21
Let's switch back to the routes J.S. file.
2:26
Let's build a fast route handler.
2:30
The first route is slash
questions with the get verb
2:33
one the router objects we
can call the get method.
2:38
The first parameter will be the rank
string that we want to handle.
2:48
Inside this particular router this
will be represented by a single slash.
2:54
That's because the app handles
the request to the router.
2:59
It strips away what was
already matched /questions.
3:03
So any sub routes to questions
would be built onto the slash.
3:14
For example If
3:20
the app gets a request
to /questions/5/answers
3:25
it would see that the URL
meets the criteria for
3:32
our router and it gets handed The route.
3:38
/5/answers.
3:48
The second parameter in this
method is a callback function.
3:54
This is our route handler.
4:04
It will execute when a request comes
in that matches the route and verb.
4:07
When the handler is called, it will
receive the request and response subject
4:15
which we'll abbreviate to (rec,
4:20
res).
4:25
And we want this route to //
Return all the questions.
4:28
Notice that this function
looks a lot like middleware
4:36
without the next function
as the third parameter.
4:39
It works in the same way and
we could have put the next in.
4:42
But because this is likely to be
the last stop in our application before
4:47
the response is sent back to the client,
we wouldn't need the next function now.
4:51
We will use it in future videos
as it can help us handle errors.
4:55
To handle this request to read
all questions we would need to
4:59
talk to a database and
we haven't set up a connection yet.
5:04
We'll need the database connection for
all these routes eventually but for
5:09
now let's send some hard coded responses
from them to see the API working.
5:14
To send a json response to the clients
we'll use their responses json method.
5:25
This call will end the life
cycle of the request process,
5:35
we can put javascript objects
into the jsson method.
5:39
And it will take care
of the serializing and
5:43
the stringifying of the json
before sending it out.
5:45
Let's follow the same
5:57
pattern to handle
6:03
post requests using
6:08
post instead of get.
6:14
We'll include one more property
to send back the data we
6:52
received as an extra check that
we're able to access the data.
6:55
Now we can create the route for
retrieving specific questions.
7:06
We'll use the ID parameter to represent
the question ID portion of the route.
7:10
We can update the response so
it says for a specific ID.
7:42
Now when we test these we should see
the results in Postman rather than
7:55
the terminal.
7:59
Let's save the file.
8:01
Stop the server.
8:06
Switch over to Postman.
8:09
Create a GET request on
localhost/questions and click Send.
8:12
You should see a json response showing
up in postman's response window,
8:23
now we'll send a POST request
To localhost:3000/questions.
8:31
For the body you may already
have the json from our last POST
8:42
request we made, but
if not puts it back in.
8:46
Hit Send.
8:52
And you should see the response we
expected with a reference to the json
8:57
we sent.
9:02
Finally let's test
localhost:3000/questions/89
9:07
with a GET request to verify if we
can see the ID in the response.
9:14
It works, great job.
9:24
Let's take a peek in the terminal and
9:26
as you can see there's
not much happening there.
9:29
We've got the notification that
the express server has started but
9:32
nothing after that it
would be nice to see some
9:35
information about the requests in the API,
and its responses while we're developing.
9:38
In the next video will add locking
functionality that build and
9:44
test the rest of our routes.
9:48
You need to sign up for Treehouse in order to download course files.
Sign up