- [Instructor] Now after creating the user schema model,
the next step is creating a route
to let users register themselves.
The form for that is already provided
with the sample project, simply head to
Account, Register, and you should get that form.
Let's review the HTML, we had to form a little bit
so I'm using inspect here and you see
the form does a post request to slash users slash
registration and we will now implement exactly that prompt.
For that we head over to Visual Studio Code
and there I open server, browse, users, index dot JS
and you see already the get route, this is the
form that we just saw and under this route
I will now add a new one for post.
Router dot post slash registration and it will be async
and it takes request, response, and a next.
And next, we want to implement the logic
for creating a user and usually I tend to organize
functionality in service classes, you see them
when you look into the services folder.
To keep things a little bit more compact though, I will now
implement the user creation logic in the route directly.
If you start to add more functionality, you can still
factor things out into a class of its own.
To add the user creation logic, I will now bring in the
user model, so right after we required express
I will now add const, user model equals require
and we go two levels up, enter as models, user model.
In the route now, I will first add a try catch block
because we will use await in here
and we can only handle errors in await
using try catch so I add error and if an error occurs,
I will just return next error.
Now let's implement the logic for storing the user
inside the database, for that I will add
const, user equals new user model, and this takes an object
with the data that we actually want to store.
So I want to store the user name and this comes in
request dot body, username, because we have the express body
parser as middleware in place so it will
resolve every form inputs into req body.
We also want store the email so it's req body dot email
and, of course, also the password, req body password.
Next we have to save the user so I add const,
saved, user equals await, user dot save.
This is a function that Mongoose gives us
and it will store the user in the database.
Now we have to make sure that the user
was actually really saved so I add, if saved user,
then everything was fine so I will return, res redirect
and I will send the user back to users slash registration
and I will add success equals true to the query.
If you look into the get route above, you see that
we pass on this argument further into the form
and the form has some logic in there, that will show
a success message when this variable is true.
If there is no saved user, I will return, next, new error,
because obviously we could not save the user
so I will add, failed to save user for unknown reasons.
We are omitting validation here because the
model will do that for us, in real world scenarios though,
you may at least want to validate if the user name
or email already exists in the database
and then provide a user-friendly error message to the user.
Now let's test this out, the sample project
comes with a test for that so I now run NPX mocha test
server routes users index dot registration test dot JS
and we see that all tests run through successfully.