Instructor: We've now got Mongo set up,
we've got MongoDB running in the background
and we've got Robomongo to inspect our database
after we make changes to it.
I think we're at a great place right now
where we could start actually saving user models
to our database based on incoming requests.
So in other words, implement our signup route.
To implement our signup route
I'm gonna want to change the structure
of our router a little bit here.
Our current route handler
has all of its logic inside the router.
This is good for now
when we've got just this one route handler
but you could kinda imagine that if we start
to have a lot of different route handlers
with a lot of logic
this router file is gonna grow to be very large.
So let's implement our authentication logic
or our signup logic, but we're going to do so
in a separate file
and then pull it into our router.
So let's look at a diagram of what this might look like.
So we're going to have an incoming request
and it's always gonna go through the router
but the router is gonna decide
where it wants to send the request off to.
In this case
I've created three kind of imaginary controllers
one for comments, one for authentication, and one for posts.
We're only gonna have an authentication controller.
The other two here are just here for examples.
We're going to define the logic to handle these requests
inside of what we call controllers.
So we're gonna have one controller
kind of for each resource you can kind of think of.
And then each controller
is gonna be individually responsible
for creating a response and actually sending it back
to whoever made the request in the first place.
So our goal right now
is to create this authentication controller
and create our signup route handler
and place that inside of our router.
So let's go ahead and give it a shot.
In my route directory
I'm gonna create a new folder called controllers
and this is where is where I would put
all my different controllers inside my application.
I'm going to make a new file in here
and I'm just gonna call it authentication.js.
Now, this is where I'm gonna put my logic
to runt or to process a request.
So this is where we're going to pull in a request object,
a response object, we're gonna put in some logic
and then we're going to ultimately respond to it.
So to do this
we're going to define a function and then export it.
So we'll try to export it
and we're going to use the exports keyword.
And we're going to export a function called signup.
And this will be a function that takes a request,
response, and next.
And this is probably gonna be good for right now
let's wire it up to our router
and then we'll come back here
and maybe put in a little bit more logic for testing.
So now all we really wanna do
is whenever a user visits a a very particular route
we wanna send them to that authentication controller.
So let's first import the authentication controller up top
and we'll do so with constant authentication
and it'll require controllers authentication.
We don't really need this test route anymore
so I'm gonna take it off for now
and we're going to replace it with a route handler
for our signup route.
So this is going to be a post request
because the user's gonna be posting
some username and password
and they're gonna post it to something
like basically /signup.
So let's say app.post,
post specifically to handle a post request
for anything posted to /signup.
We want to run the function authentication.signup.
So let's save this.
I'm gonna check my Terminal real quick
just to make sure that I didn't make any errors.
So everything is still agreeing over here.
I don't have any errors or anything like that.
That's good.
Let's go back over to our authentication controller.
So I've got a reasonable expectation
that this is where any request
is gonna be piped to if I make a post request
to the signup route.
So let's just make sure everything's working right now.
Let's make a response and send it back
and we'll just say success true.
Just some simple json that we'll send back to the client.
So I'm gonna save this and I'm gonna flip over to Postman
and we're gonna give it a quick shot.
We're gonna see if this is working.
So in Postman, I've got a post selected
to localhost:3090/signup
and I'm gonna send this off and I get back success, true.
Perfect, that means that my route is working.
This looks good.
Let's go ahead and continue in the next section
where we will implement the ability
to create and save users inside of our signup handler.
I'll see you there.