So we had views, we added a model,
now the controller part is missing for our blog
or our post-related routes.
Hence, that is what we'll add.
And for this, I'll add a new sub folder in this project,
which I'll name controllers.
And in there, I'll add my post-controllers.js file
because I wanna store all my post controllers in there.
Now, actually, we'll only have one post controller
because all the different functions
that we need for the different routes
will be called controller actions
if we wanna be really precise.
But still here for the moment,
I'll name it post-controllers.
Later, once we add a second controller for authentication,
which we'll do later in this section.
I'll rename this to then clarify that actually,
we got two different controllers
then for the two different areas of our application.
Right now, since this is the only file,
I'll treat the individual functions in there as controllers,
even though technically these functions,
which we are about to add in that file,
will just be actions that belong to a single controller.
But again, we'll change this later.
And therefore, of course, if we have a look at our routes,
these route handler functions,
which we pass as second parameter values to our routes
could be considered controllers.
Now, I just wanna outsource them into separate files
and then also make them a bit leaner in the next steps,
so that the routes file is way slimmer
and it's way easier to quickly see
all the routes that we are supporting,
because that is something you commonly wanna do.
You don't wanna have large route files
with a lot of logic in there.
Instead, if you look into a route file,
you wanna be able to quickly grasp
which routes are supported by this website.
And that's why we'll use a dedicated controller file
for these route handler functions.
And therefore, I'll simply go through
that blog.js routes file from top to bottom
and outsource all these functions here,
these route handler functions
into the post-controllers.js file.
So therefore, I'll cut this first function,
this home function and add it here.
Now, we just need to give it a name,
because it's no longer valid
to have an anonymous function here.
Instead, we want to be able to reference it by name.
And it's now up to you
how you name your controller functions,
but you wanna name them such that it's clear what they do.
And here I'll name it getHome
because this function will be responsible
for getting the home page.
Now, to make it available outside of this file,
I'll again export an object
and there I have to getHome key,
which points at this getHome function.
So that in blog.js, where I wanna use this function,
I can get my blogControllers object,
which I'm exporting, by requiring
controllers/post-controllers.
And then here, for this first get route,
we use blogControllers.getHome.
And very important, we don't execute it,
instead, we again, just at point it
because we just wanna register this function with Express,
and Express.js should execute it for us
whenever we got an incoming request.
So it's a little bit like an event listener
in the frontend JavaScript world,
where we also just pointed at functions
when we added a click listener.
Now, we're doing the same here so that this function
is executed once a request is arriving.
We did the same before
with the anonymous functions by the way.
We also just the register the function there
and we didn't execute it ourselves.
So I'll continue with the admin function,
and I'll cut that function here,
move to post-controllers.js, and add it there,
and again, give it a function.
And here, since this is responsible
for loading the admin page, I'll name it getAdmin,
just as I name the other function getHome.
This is still an async function
because we still use the await keyword in there.
Now, we can also make that available outside this file
by using getAdmin as a key here
and pointing at this getAdmin function.
Very important though, in this getAdmin function,
I am relying on this Post model now.
So in order to do that without errors,
we need to import that into this post-controllers.js file.
We should get post here by requiring models/post like this.
Otherwise, working with that Post model
won't succeed and we would get an error.
Now, back in blog.js, I can, therefore, now point
at blogControllers.getAdmin here.
And again, we continue with the post route
for adding new posts now.
I'll cut to that, and then go to post-controllers.
And in there, I now do add this function as well
and give it a name.
And here I'll name it createPost
describing what will happen in there.
And I will, of course also expose this
under a createPost key just as I did it before.
And hence, here in blog.js,
I now point at blogController.createPost.
Of course, we do the same for getting the detail page.
So back in post-controllers, we added that function.
Give it a meaningful name, like getSinglePost,
and then expose that under that same key here
to the outside world.
And back in blog.js,
we now point at blogControllers.getSinglePost.
Of course, we do the same for the post route
that belongs to that, which we use for updating a post.
So we put that into blogControllers as well.
Give it a function that could be updatePost,
and then we also expose that of course,
updatePost pointing at updatePost.
And then here again, I use blogControllers.updatePost.
And last but not least, I do the same for deleting.
So now in post-controller, I now add this function,
name it deletePost and expose that to the outside world
like this.
And back in the blog.js routes file,
I now point at blogControllers.deletePost.
And now this is a way leaner routes file.
Of course, we just moved the logic into a different file,
but that was the goal.
Now, it's super easy to quickly see
which post related routes this website supports.
And that's typically the goal you wanna achieve
with your routes file.
So that if another developer takes a look at it,
he or she is able to quickly see what's going on.
We can now remove the Post model import here
in the routes file
because we're not using the model here anymore.