1
00:00:00,410 --> 00:00:00,830
Okay.

2
00:00:00,830 --> 00:00:09,860
So express is a very Unopinionated Unopinionated minimalistic framework and it basically lets you do

3
00:00:09,860 --> 00:00:10,980
things however you want.

4
00:00:11,000 --> 00:00:17,660
You can structure your files in your code however you want, and I think it's really important to learn

5
00:00:17,660 --> 00:00:21,560
good practices when you're using Express because they're not forced on you.

6
00:00:21,560 --> 00:00:24,110
So you have to kind of do it yourself.

7
00:00:24,140 --> 00:00:33,590
Now you could continue to add routes in your route files and keep the logic in these files, but I think

8
00:00:33,590 --> 00:00:39,590
it's better to create a controller for each route and use that controller function.

9
00:00:39,590 --> 00:00:42,510
It just it just helps you keep things organized.

10
00:00:42,530 --> 00:00:43,730
So that's what we're going to do.

11
00:00:43,760 --> 00:00:48,860
We're going to create in the back end a new folder called controllers.

12
00:00:49,830 --> 00:00:55,500
And let's create a file called Product Controller dot.

13
00:00:55,530 --> 00:00:57,180
JS Okay.

14
00:00:57,180 --> 00:01:03,570
And basically what we're going to do is instead of having this stuff in here to get all the products,

15
00:01:03,570 --> 00:01:06,240
we'll have a function called get products.

16
00:01:06,240 --> 00:01:11,250
And then here we'll have a function called get product by ID because that's what it's doing.

17
00:01:11,340 --> 00:01:20,550
So let's go into our product controller and I'm going to first of all, create a function called get

18
00:01:20,550 --> 00:01:21,780
products.

19
00:01:21,780 --> 00:01:27,260
And we're going to set that to an asynchronous function.

20
00:01:27,270 --> 00:01:33,870
Now, we do need to use our async handler because it's the same code we're using here, but we're just

21
00:01:33,870 --> 00:01:36,360
going to have it in a in a separate function.

22
00:01:36,360 --> 00:01:41,370
So let's bring the async handler into the controller, okay?

23
00:01:41,370 --> 00:01:42,900
And let's just make sure.

24
00:01:42,900 --> 00:01:43,140
Yeah.

25
00:01:43,140 --> 00:01:44,820
So we're in the controllers folder.

26
00:01:44,820 --> 00:01:46,470
So we go up one level.

27
00:01:46,470 --> 00:01:48,030
So that's, that's correct.

28
00:01:48,030 --> 00:01:51,010
And then we're going to wrap this.

29
00:01:51,010 --> 00:01:59,440
So let's say async handler, just like we did in directly in the row and then just have ending parentheses

30
00:01:59,440 --> 00:02:00,100
there.

31
00:02:00,850 --> 00:02:01,180
All right.

32
00:02:01,180 --> 00:02:07,210
And then our logic will go in here and this is going to still take in request response, just like the

33
00:02:07,210 --> 00:02:09,220
callback does right here.

34
00:02:09,460 --> 00:02:12,370
So now we're also going to need the product model.

35
00:02:12,370 --> 00:02:17,230
So we'll grab that, we'll bring that into our controller.

36
00:02:17,230 --> 00:02:22,540
And then let's just get the code from this first route here to get all products.

37
00:02:22,750 --> 00:02:24,370
So we'll paste that in.

38
00:02:24,400 --> 00:02:27,430
Then what I'll do is copy this function.

39
00:02:27,430 --> 00:02:32,500
We'll create another one called Get Product by ID, Okay.

40
00:02:32,500 --> 00:02:36,040
And then same thing, we're using our async handler.

41
00:02:36,710 --> 00:02:39,710
We'll get rid of this code here and then grab.

42
00:02:40,690 --> 00:02:41,470
This.

43
00:02:41,470 --> 00:02:46,120
So getting the product, checking if it exists, etcetera.

44
00:02:47,450 --> 00:02:48,920
Put that in there.

45
00:02:49,490 --> 00:02:49,880
All right.

46
00:02:49,880 --> 00:02:53,270
And then we just want to export both of these functions.

47
00:02:53,270 --> 00:02:55,730
So get products and get product by ID.

48
00:02:55,970 --> 00:03:01,070
Now, one thing I like to do with my controller functions, if you guys have taken the first course

49
00:03:01,070 --> 00:03:06,290
or any NodeJS course, I like to put a couple things before each one.

50
00:03:06,290 --> 00:03:12,650
So first I want to do the description so I do at DSC and then what it does.

51
00:03:12,650 --> 00:03:14,300
So this fetches all products.

52
00:03:14,300 --> 00:03:19,610
Then I do the route that this controller is going to be hooked to, which is going to be get requests

53
00:03:19,610 --> 00:03:22,250
to API products and then the access.

54
00:03:22,250 --> 00:03:24,950
So if you have to be logged in, it would be private.

55
00:03:24,950 --> 00:03:30,080
If you have to be an admin, it would be private admin and if it's public then it would be public,

56
00:03:30,080 --> 00:03:31,850
which this is okay.

57
00:03:31,850 --> 00:03:35,030
And then we'll just grab that, put that in here.

58
00:03:35,030 --> 00:03:36,530
So this will fetch.

59
00:03:37,510 --> 00:03:45,880
Say fetch a product, get request to API products, slash and then the ID and it's public.

60
00:03:45,880 --> 00:03:47,350
So completely optional.

61
00:03:47,350 --> 00:03:52,390
But again, I think it just makes your code cleaner and you can tell exactly what's going on.

62
00:03:52,540 --> 00:03:53,500
Now.

63
00:03:54,370 --> 00:03:55,690
Back in the roots.

64
00:03:55,690 --> 00:03:59,740
We no longer need the async handler or the product model.

65
00:03:59,770 --> 00:04:00,850
Get rid of that.

66
00:04:00,880 --> 00:04:03,580
What we do want to bring in is our controller function.

67
00:04:03,580 --> 00:04:06,850
So we want to bring in get products.

68
00:04:07,470 --> 00:04:14,100
And also get product ID from our controllers and make sure you put a dot JS here.

69
00:04:14,370 --> 00:04:18,000
And then as far as the routes.

70
00:04:19,120 --> 00:04:19,690
Let's see.

71
00:04:19,690 --> 00:04:22,029
We can actually just.

72
00:04:22,029 --> 00:04:23,770
Let's get rid of both of these.

73
00:04:25,400 --> 00:04:27,860
And now we can just do router.

74
00:04:28,220 --> 00:04:31,040
I'm going to do router dot route.

75
00:04:32,060 --> 00:04:38,420
So in addition to doing like router dot get router dot post, you can do router dot route and then put

76
00:04:38,420 --> 00:04:43,970
whatever the endpoint and then the method and then what that should do, which in this case is going

77
00:04:43,970 --> 00:04:45,620
to be get products.

78
00:04:46,130 --> 00:04:46,430
Okay.

79
00:04:46,430 --> 00:04:50,540
Then we want one for the ID, so we'll say router dot route.

80
00:04:50,570 --> 00:04:57,140
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

81
00:04:57,140 --> 00:04:58,070
product by ID.

82
00:04:58,430 --> 00:05:01,640
So that really cleaned up our router.

83
00:05:01,850 --> 00:05:03,830
And now it should work the same way.

84
00:05:03,830 --> 00:05:06,950
If I go to our front end, you can see that works.

85
00:05:06,980 --> 00:05:08,870
Fetch a single that works.

86
00:05:08,870 --> 00:05:13,550
If we test test it with Postman, which are both doing the same requests, right, whether we're in

87
00:05:13,550 --> 00:05:15,740
the browser or whether we're doing it this way.

88
00:05:15,740 --> 00:05:17,360
So that gets all the products.

89
00:05:17,360 --> 00:05:20,990
If we want to fetch a single product, that works as well.

90
00:05:21,080 --> 00:05:24,380
All right, so same functionality, but cleaner code.

91
00:05:24,440 --> 00:05:24,740
Okay.

92
00:05:24,740 --> 00:05:30,380
We really cleaned up the routes and we now have nice organized controller functions.

93
00:05:30,770 --> 00:05:31,070
All right.

94
00:05:31,070 --> 00:05:37,200
So now we're going to jump into the front end in the next video and start to set up Redux and Redux

95
00:05:37,200 --> 00:05:37,830
Toolkit.

