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
Now we'll use the username to redirect to the correct location.
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
Now that we have the user name out of the
post body,
0:00
let's investigate on how to redirect to
the student's profile page.
0:02
So, if we go down to the post part of our
home route, we can see that
0:07
we're just printing out the username to
the response and we end the response.
0:11
Actually, what we want to do is use this
username.
0:17
And add a slash in front of it and then
redirect to that username.
0:21
So want to do something like this.
0:25
Redirect to slash chalkers slash whatever
your username is.
0:29
So we need to figure out some headers on,
and how will we need to do that?
0:34
So if we go to http
0:39
header fields.
0:44
And there's a list of HTTP header fields
on Wikipedia so
0:49
let's take a look at the various different
fields so.
0:54
We've done content type already and
0:57
we see how we can just change it to
constant type and
1:02
then whate, whatever lets see if
1:09
there anything to do with a location.
1:13
[BLANK_AUDIO]
1:18
So here we go.
1:20
Location is used in redirection, or when
new resource has been created.
1:22
So the location, and then the url.
1:28
So we can modify the headers, and send
location to this.
1:31
Let's just click on this redirection and
see what there is.
1:38
If you look here, this HTTP status code so
let's have a look at these.
1:44
These 300 multiple choices as in different
languages,
1:50
permanently moved, found or see other.
1:53
Forces a get request to a new URL even if
the original request was a post.
1:57
So that's exactly what we're doing right
now so instead of giving a 200 status
2:02
code, we want to give a 303 because we're
doing a post, but
2:07
we want it to go to a get URL.
2:11
So.
2:15
We can do writehead and then 303, so
instead of 200 saying okay,
2:17
we're saying 303 with going to say see
other,
2:23
see another location, and remember, the
writehead has
2:27
headers as a dictionary of a JSON object,
so content type.
2:32
So instead of content type, we want to put
in location down here,
2:37
so we do location and
2:43
then the URL we want it to go to.
2:48
Now we don't need to do the full URL, we
don't need to do HTTP colon forward slash,
2:53
forward slash, whatever the workspace URL
is slash.
2:57
chalkers.
We can just do slash chalkers because
3:01
we're on the same domain, we're not
redirecting it to another website or
3:04
anything like that, so if you were
creating some sort of like URL shortener,
3:08
you'd probably want to put in the full URL
there because it's
3:12
probably going off to an external site.
3:16
But because we're just dealing with a
local site.
3:18
This would redirect it.
3:21
So in theory, this should work.
3:23
So save node app.js.
3:26
Click on preview.
3:34
And let's type in chalkers.
3:36
And hit search.
3:38
And it redirected to my page.
3:41
So let's click on search again, and try a
different user.
3:43
Joykesten2'.
3:47
Oops, I actually put a li, an apostrophe
at the end there, and
3:51
as you can see it's not found, so our
error's working correctly.
3:54
But let's do it properly just to see.
3:58
That's another username.
4:00
And well, it worked as well.
4:04
So that's our app, up and running.
4:06
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