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 handle the user route on our site.
Node.js APIs Used
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
First of, let's create a function called
userRoute.
0:00
[BLANK_AUDIO]
0:06
So the user would be slash and then the
username.
0:20
So we want to remove the first slash and
that would be our username.
0:25
So we can do var username is equal
0:30
to the request.url, and
0:35
we can replace
0:40
slash with the empty string.
0:45
So if the username
0:54
[BLANK_AUDIO]
0:56
length is greater than 0, so technically
you could
1:00
replace slash with just empty string on
that route and
1:05
it would just be the empty string.
1:10
So if I went to home, it would match.
1:13
The username would be the empty string.
1:17
So we need to make sure that the username
actually exists i.e.,
1:20
is longer than 0, and then we can do
something with that username.
1:24
So let's just look at the code here, we
can do the same as this.
1:29
But instead of search, we can just put in
the username so, username.
1:38
So we'll see the Header, we'll see the
username and we'll see the Footer.
1:45
And we'll worry about getting the actual
data a little bit later on.
1:50
But before this gets out-of-hand, we've
got two routing
1:55
functions in our app JS file, and it could
get a little bit messy.
2:00
So what we should do is compartmentalize
our code, and
2:06
we should create a router JS file which
holds all the routes for our app.
2:11
So let's do New File and we'll call it
router.js.
2:18
[BLANK_AUDIO]
2:24
And let's just cut these two handlers out
2:31
of our app.js and paste them in here.
2:37
And let's just get rid of these numbers
now cuz they don't really make any sense
2:43
in the context of the app being split up
into separate files now.
2:48
So, we create the web server.
2:54
And we can require
2:56
the router.js file and
3:01
let's do that,
3:08
var router is equal to
3:13
require router.js.
3:19
And we need to export the routes in our
router.js file.
3:25
So, at the bottom here,
3:33
we can do module.exports_home
3:35
is equal to homeRoute and
3:44
we can do module.exports_user is equal to
userRoute.
3:48
But you know what, the route at the end of
the function name is kind of redundant.
3:56
So let's just get rid of that, and get rid
of that,
4:01
get rid of that there, and get rid of that
there.
4:06
So now in our app.js, we're requiring the
router and
4:11
that means we can access those methods on
the router.
4:16
So we can do router.home and
4:20
router.user.
4:26
So, if we ever need to create new routes
we can just go in the router file,
4:31
export them and then just include them
here like this.
4:35
So, let's try this out in our browser.
4:39
[BLANK_AUDIO]
4:42
Oh, there's an error.
4:51
Can't find the file router.js.
4:52
[BLANK_AUDIO]
4:56
Oh, because I didn't do the path to the
router.js file.
5:02
So, that's why.
5:08
And that should fix it, cool.
5:10
[BLANK_AUDIO]
5:11
So, the home route is working still, so
there's Header, Search, and Footer.
5:18
And then let's do chalkers now.
5:24
And it shows Header, Chalkers, and Footer.
5:27
So, let's try a different username.
5:30
[BLANK_AUDIO]
5:32
joykestan2, and
5:36
even we can do chalkers123.
5:41
[BLANK_AUDIO]
5:48
And it still works because we're not going
out on the internet and
5:50
we're not going to the Treehouse API and
pulling information from it so
5:54
it's not erroring at the moment, cool.
5:58
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