So let's start this refactoring process
and I'll start by splitting up my routes.
I'll add a new auth.js file
and I'll keep the blog.js file,
but now the blog.js file should only contain the routes
that are blog related, post related,
and auth.js should contain all the auth routes.
So I'll start by copying blog.js, the content in this file,
and pasting this into auth.js.
And now in auth.js, I'm going to remove all the logic
that's related to the blog.
Now, we can argue about this welcome page,
but I would argue it belongs more to the blog part
than to the auth part because I really wanna have
just the authentication-related routes in here,
so I will delete that.
Of course, I'll keep the get signup route here
and get login and post signup and post login
because these are all auth related,
but then I will remove the admin route
because that is primarily about displaying posts
and the post form, so I'll delete that.
I'll keep the post logout route, of course,
but I am going to delete all the other routes
because all these other routes
are only related to managing posts.
So I'll delete all of that
so that the finished auth.js file has the logout route.
And here in VS Code, you can kind of minimize this,
so it's still there,
but it's now easier to see all the routes.
I got the logout route here, then I got the login route,
then I got the signup post route,
and I got the login get route,
and I got the signup get route.
This is now my finished auth.js file.
And all the code is still there.
I just minimized that to bring it all into one screen.
And I don't need the object ID thing here anymore
because we only use that for working with posts,
so it's not required in the auth.js file anymore
because every file should only bring
what it needs inside of that file,
not what's needed in other files.
And I can therefore also get rid of that MongoDB import.
You can see it's slightly grayed out
and that tells you that it's not being used in this file.
So this is the finished auth.js file.
In blog.js, I'm going to do the opposite.
Here, I'm going to drop all the auth.js related logic.
And that, for example, starts with the bcrypt import
because I know that I'm only using bcrypt
for password management
and therefore this won't be related to posts,
so we can delete that.
I will keep the welcome route here
because we deleted it in the auth.js file,
but I will delete this get signup route
and I will delete the get login route
and the post signup route and the post login route
because these are all auth routes.
I'll delete all of them,
but I'll keep the get admin route here
because that is related to posts.
And I will delete the logout route,
but keep all the other routes.
So if I minimize that here as well, I got that home route,
the admin route, the post posts route for adding a new post,
the get posts id edit route
for loading a single post page,
the post route for editing a single post,
and this delete route for deleting a post.
That's now the route setting
or the routes that I wanna have for my blog.js file.
And here are the imports.
Now, I split that into two files,
but, of course, I still want to register all the routes.
So we need to go to app.js
and there I'm currently including only the blog.js file.
Well, now we simply need to add a new constant,
maybe called authRoutes where I require ./routes/auth.
So I'm including both route files now.
And now we just need to make sure
the requests reach all the routes in both files.
So therefore, further down below,
after registering all the main middleware,
I don't just wanna use my blog routes,
but also my auth routes here.
Now, which one you register first, it doesn't matter here.
It would only matter if you had the same path
and the same route defined in both files,
then the first file to be registered would win,
but it would be very rare
that you wanna have the same route in multiple files.
It wouldn't make too much sense in most cases.
So therefore in most cases,
the order of registering your routes doesn't matter.
Okay, so with that, if we quickly test this,
we can still navigate around
and I'll reach the signup and login pages.
So that's still working, but now we refactored our routes.