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
The Express.router() function allows us to put our routes in their own file, as well as add a /api to all the endpoints for our API.
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
If you've accessed information
from an API in the past,
0:00
you may have noticed that often,
you request information from a /api route.
0:03
For example, if you look at the
documentation for the randomuser API or
0:08
the Star Wars API,
0:11
both expect the client to make a request
to an endpoint beginning with /api.
0:13
We can set up our API the same
way within an express router,
0:18
which is created using a method
called express.router.
0:21
Doing it this way will be
beneficial in two ways.
0:24
We can map each of our routes to
an endpoint that starts with /api,
0:28
without having to repeat /api for
each and every route.
0:32
And it will help keep our
express files more modular.
0:35
We can move all of our routes out
of app.js and into their own file.
0:38
To get started,
let's first create a new file and
0:42
save it in the root folder as routes.js.
0:44
Go back to app.js, cut and
paste the routes and
0:50
our asyncHandler helper function
into the new routes.js file.
0:53
I'll first move the asyncHandler function,
then I'm gonna leave this app.use and
0:58
move all of these routes, but
we're going to leave the error handling.
1:05
Now in routes.js, let's bring in express
by requiring the express package.
1:12
Return to app.js.
1:27
Recall that we can use app.use to tell
express that we want to add a piece of
1:29
middleware.
1:33
This time, we're going to
specify middleware only to be
1:34
used if the requested route
starts with a certain path.
1:38
Here we're saying when a request
starts with the path /api,
1:48
use the routes inside
of the routes.js file.
1:52
And make sure that you have this
beginning forward slash, or
1:56
you'll run into problems.
1:59
Before this will work,
2:01
we're going to have to import
the routes.js file into this file.
2:02
So let's go to the top and
make a new require statement.
2:05
So we are referring to this here.
2:16
Back in routes.js we'll
set up a new router.
2:19
The router method allows us to route
everything to /api without having to
2:22
specify it on every single route.
2:26
Each place where it says
app will change to router,
2:35
in reference to this
express.router up here.
2:38
At the bottom of the file we'll
need to export the router.
2:53
And finally we'll need to
import our records module.
3:00
So let's quickly review what we've done so
far.
3:11
We've created a new module named routes,
and moved all of our quote routes into it.
3:14
We're using the express router method
which allows us to map these routes
3:19
to a specified path, in this case, /api.
3:23
We're assigning express router
to a variable called router, and
3:27
using it to handle our
various types of requests.
3:31
Finally, at the bottom of the file,
3:34
we're exporting router along with all
the routes we've defined in our file.
3:35
In app.js, we're importing the file
we just made and saying we want to
3:39
use the routes in that file whenever
a request comes in that starts with /api.
3:44
And we no longer have to use records
in this file because we imported
3:50
it into the other file.
3:53
Now let's make sure that
our server is running.
3:55
And make sure that this still works.
3:59
Notice that now, if I make a GET request
to the /quotes route, I get an error.
4:10
We know that our error handling is
working just fine, so that's good.
4:17
I'll send a GET request
to localhost/api/quotes.
4:21
And there is our list of quotes back.
4:27
This should also work for
all other routes.
4:30
Let's try getting a specific quote.
4:32
So we'll ask for a quote, 8721,
and great, it's working just fine.
4:34
This is a standard way to structure and
4:40
modularize an API in express that
will allow our app to easily grow.
4:42
Well done, we've cleaned up our app a bit
as well as handled any major errors.
4:47
Let's finish up by adding
a final piece of functionality,
4:51
a route that returns a random quote.
4:54
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