Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed User Authentication With Express and Mongo!
You have completed User Authentication With Express and Mongo!
Preview
Use Express's built in router to add a GET route to display a registration form and a POST route for collecting user registration information.
References
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
[MUSIC]
0:00
A major part of any user authentication
system is the registration process.
0:04
After all, you can't very well
authenticate a user's identity
0:09
until you've added a way to capture and
store user information.
0:12
In this section of the course
I'll show you how to create
0:16
a registration form that
connects to a Mongo data store.
0:19
Users will be able to
sign up on your site and
0:22
their information will be
added to the database.
0:25
First let's define the routes users will
take to see the registration form and
0:28
submit the registration information.
0:32
We already have three
routes in our application.
0:35
We'll add two more routes now.
0:38
They use the same URI or
endpoint, /register.
0:40
When the user requests that
endpoint using the HTTP GET method,
0:44
they'll see the registration form.
0:48
When they use POST to send their sign
up information to that same endpoint,
0:50
the application will add
that data to the database.
0:54
Let's program these routes now.
0:58
We have some basic routes already
defined in the routes directory
1:01
in the file called index.
1:05
js.
1:06
I'll start by adding a get route
to display the registration form,
1:08
router.get and then we pass it a string
/register, that's the end point.
1:12
We had a callback function
as the second argument,
1:21
the callback tells Express what to do
when a user requests this end point.
1:24
It accepts three arguments, the request
object which has all the information
1:29
related to the request that was sent
from the client, the response object
1:33
which lets us send data back to the user,
and a function called next
1:38
which tells express to continue on
to the next piece of middleware.
1:43
In other words, what function should
Express run after this callback?
1:47
I'll talk more about next in
middleware later in this course.
1:51
So if those are new concepts to you,
don't worry about them for now.
1:54
For this route we're not
concerned about the requests.
1:57
The user is just asking for
the registration form.
2:00
So we use the response.
2:03
That's the res parameter here,
to send back our response.
2:05
Because we haven't created
the registration form yet,
2:09
let's just send back a message
to see if the route works.
2:11
Let's check it out.
2:22
I'll save this file.
2:23
And switch to my terminal or
console program.
2:25
I need to make sure I'm
in the correct directory.
2:28
So I'll print out my working
directory by typing pwd.
2:30
If you're using the Windows console
you can use the cd command.
2:34
Then I'll start the app
by typing node app.js.
2:39
This will start the express app and
2:45
keep the process running
in this terminal window.
2:47
The apps running on a local
host using port 3000.
2:50
I'll open a web browser and
enter localhost:3000.
2:52
When I click the Sign Up button I'm
routed to the /register endpoint.
2:58
And I see the Register today!
3:06
message.
Great, that route's working so
3:07
let's add a post route.
3:09
The post route to register
is where we receive
3:09
the information entered
into the registration form.
3:15
We'll use that data to create
a new user in the database.
3:19
Again.
3:33
Let's use res.send with a simple text
message, so that we can test this route.
3:33
Unfortunately, we can't test it yet, we
need to create a registration form first.
3:38
Then we'll be able to post
information to this route.
3:43
We'll build that form soon.
3:45
But first let me teach you two other
ways to start up an express application.
3:47
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