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 Morgan, we build our Answer routes in app.
Our routes are all written and
0:00
we've implemented all the HTTP
verbs we plan to handle.
0:02
However, there is a problem.
0:05
There isn't much code to handle the
actions outside of what we planned for.
0:07
In the next video, we'll see how to handle
the common errors that might come up.
0:12
When a request is sent to our API,
0:18
it would be nice to get a little
more information about it, and
0:20
the details about the response
the server has to that request.
0:24
In other words,
0:29
we want an automated logger, something
that logs out useful information.
0:30
Let's install another piece of
middleware to help with that.
0:35
We can use a module called Morgan.
0:39
We can configure Morgan to log HTTP status
codes as well as the routes for us.
0:42
In the terminal, run npm install
0:48
--save morgan@~1.7.
0:54
When that's done,
we can move over to our app.js
0:59
file in our text editor and include it.
1:04
Let's put a required statement with our
other required statements at the top
1:12
of the code to keep our code organized.
1:17
Let's call this logger,
and require morgan.
1:21
Now we can connect it to our app with use,
passing in dev to our logger function.
1:35
This will configure the middleware
to give us colorful status codes for
1:52
our API's responses.
1:56
Feel free to check out the documentation
from Morgan in the teacher's notes.
1:59
Let's build our answers routes now.
2:03
When we go to test them later,
we'll see what Morgan does in action.
2:06
Going back to our routes.js
file in the text editor,
2:12
let's enter our next
route that we planned.
2:15
We planned a route to create an answer.
2:19
This route is POST
2:36
/questions/:id/answers.
2:40
We plan to use the post method for
this route.
2:49
When implementing this route handler,
remember to leave off /questions.
2:53
Our router is already
mounted to questions.
3:03
Let's modify the response we say
when we're reposting to /answers.
3:12
And return the questionId, too.
3:20
The next route we can handle is
the PUT request to a specific answer.
3:30
As we build this route,
You might notice a problem.
3:54
The problem is,
4:09
is that we have two parameters in
the URL that have the same name.
4:10
This won't work.
4:15
One will override the other.
4:17
We need to give them different names.
4:19
Let's go back and
4:21
add updates to all the routes with the
question ID with a new parameter of qID.
4:22
We can call the answers ID, aID
4:58
Let's copy the handler
from the post route and
5:11
paste it into the put route and
modify it with the appropriate response.
5:14
And have it respond
with the answer ID too.
5:25
With the request params aID
5:34
We also need a delete handler for
this route.
5:46
This will delete a specific answer.
5:50
Let's update the method we call from put
to delete, then delete the req.body.
6:08
And finally update the response text.
6:21
Last of all, we'll create the routes for
voting on answers.
6:28
Let's copy and paste this DELETE route.
6:34
And change delete to post.
6:41
Let's describe what URLs we
want handled by this route.
6:45
The POST route,
6:50
Where we can upvote, And downvote.
6:55
These are to vote on a specific answer.
7:10
The URLs are identical except for
the vote-up or the vote-down at the end.
7:16
We can capture these two
possibilities with a parameter, and
7:24
we can use one route for both.
7:28
We can capture only part
of the string we want.
7:39
So let's grab the up or
down from the last part of the route.
7:43
We can call the parameter dir for
direction.
7:49
Now let's modify the response
7:52
in the POST request, To
7:58
/vote-" and then
8:02
the req.params.dir.
8:09
Let's also include the vote
down here at the bottom too.
8:18
Now let's test our routes out.
8:29
Switch over to the terminal,
stop the server if it's running, and
8:32
clear the screen.
8:35
Now start up the server again.
8:38
In Postman,
8:40
let's send a POST request to
8:42
localhost:3000/questions/6/answers
8:48
with the body prop value.
8:59
We see that works.
9:14
In the Postman results window,
we see the response that we constructed.
9:15
Now let's switch back to the terminal
to see our new logging in action.
9:20
Our Morgan logging middleware
has given us the HTTP verb,
9:27
the URL requested, the color coded
status code 200, among other things.
9:33
Let's change our HTTP method to PUT.
9:43
Then change the URL
9:49
to localhost:3000/questions/9/answers/33,
9:52
with the same JSON body.
10:00
Hit Send, and let's see what happens.
10:06
We get the correct response.
10:13
Make sure the questionId is 9 and
that the answerId is 33.
10:17
Now let's send a DELETE to the same URL.
10:23
Check the response is what you expect,
that it's a DELETE
10:28
request with the questionId of 9 and
the answerId of 33.
10:33
Finally, let's send a POST request too.
10:39
Localhost:3000/questions/5/answers/78/vot-
10:44
e-up.
10:53
See that the route was read correctly.
11:00
Let's check vote-down.
11:03
That works too.
11:10
You need to sign up for Treehouse in order to download course files.
Sign up