Okay.
So express is a very Unopinionated Unopinionated minimalistic framework and it basically lets you do
things however you want.
You can structure your files in your code however you want, and I think it's really important to learn
good practices when you're using Express because they're not forced on you.
So you have to kind of do it yourself.
Now you could continue to add routes in your route files and keep the logic in these files, but I think
it's better to create a controller for each route and use that controller function.
It just it just helps you keep things organized.
So that's what we're going to do.
We're going to create in the back end a new folder called controllers.
And let's create a file called Product Controller dot.
JS Okay.
And basically what we're going to do is instead of having this stuff in here to get all the products,
we'll have a function called get products.
And then here we'll have a function called get product by ID because that's what it's doing.
So let's go into our product controller and I'm going to first of all, create a function called get
products.
And we're going to set that to an asynchronous function.
Now, we do need to use our async handler because it's the same code we're using here, but we're just
going to have it in a in a separate function.
So let's bring the async handler into the controller, okay?
And let's just make sure.
Yeah.
So we're in the controllers folder.
So we go up one level.
So that's, that's correct.
And then we're going to wrap this.
So let's say async handler, just like we did in directly in the row and then just have ending parentheses
there.
All right.
And then our logic will go in here and this is going to still take in request response, just like the
callback does right here.
So now we're also going to need the product model.
So we'll grab that, we'll bring that into our controller.
And then let's just get the code from this first route here to get all products.
So we'll paste that in.
Then what I'll do is copy this function.
We'll create another one called Get Product by ID, Okay.
And then same thing, we're using our async handler.
We'll get rid of this code here and then grab.
This.
So getting the product, checking if it exists, etcetera.
Put that in there.
All right.
And then we just want to export both of these functions.
So get products and get product by ID.
Now, one thing I like to do with my controller functions, if you guys have taken the first course
or any NodeJS course, I like to put a couple things before each one.
So first I want to do the description so I do at DSC and then what it does.
So this fetches all products.
Then I do the route that this controller is going to be hooked to, which is going to be get requests
to API products and then the access.
So if you have to be logged in, it would be private.
If you have to be an admin, it would be private admin and if it's public then it would be public,
which this is okay.
And then we'll just grab that, put that in here.
So this will fetch.
Say fetch a product, get request to API products, slash and then the ID and it's public.
So completely optional.
But again, I think it just makes your code cleaner and you can tell exactly what's going on.
Now.
Back in the roots.
We no longer need the async handler or the product model.
Get rid of that.
What we do want to bring in is our controller function.
So we want to bring in get products.
And also get product ID from our controllers and make sure you put a dot JS here.
And then as far as the routes.
Let's see.
We can actually just.
Let's get rid of both of these.
And now we can just do router.
I'm going to do router dot route.
So in addition to doing like router dot get router dot post, you can do router dot route and then put
whatever the endpoint and then the method and then what that should do, which in this case is going
to be get products.
Okay.
Then we want one for the ID, so we'll say router dot route.
If it hits slash colon ID and it's a get request, then it's going to hit get it's going to call get
product by ID.
So that really cleaned up our router.
And now it should work the same way.
If I go to our front end, you can see that works.
Fetch a single that works.
If we test test it with Postman, which are both doing the same requests, right, whether we're in
the browser or whether we're doing it this way.
So that gets all the products.
If we want to fetch a single product, that works as well.
All right, so same functionality, but cleaner code.
Okay.
We really cleaned up the routes and we now have nice organized controller functions.
All right.
So now we're going to jump into the front end in the next video and start to set up Redux and Redux
Toolkit.