1
00:00:00,300 --> 00:00:00,790
Beautiful.

2
00:00:00,960 --> 00:00:09,090
We are familiar with the middle where we have our first Mendler function, but there are two issues

3
00:00:09,090 --> 00:00:10,080
with this current setup.

4
00:00:10,500 --> 00:00:17,850
First, our objets is getting somewhat clunky because, I mean, we have this longer than we have the

5
00:00:17,850 --> 00:00:18,320
methods.

6
00:00:19,020 --> 00:00:24,570
It's definitely nicer if we have this a longer function in a separate file.

7
00:00:25,320 --> 00:00:32,640
It's just going to keep our objets lean and in turn it's just going to make it easier for us to navigate

8
00:00:32,640 --> 00:00:35,300
and essentially work with our application.

9
00:00:35,910 --> 00:00:37,210
And the second issue.

10
00:00:37,380 --> 00:00:44,360
What if I have 50 more out and I don't want to add this function manually to all of them?

11
00:00:44,760 --> 00:00:50,910
Would wouldn't be nicer if there would be a method that essentially just adds my middleware function

12
00:00:51,150 --> 00:00:52,320
to Enero.

13
00:00:52,680 --> 00:00:56,060
And of course, the answer is yes, there is such a function.

14
00:00:56,080 --> 00:00:58,710
In fact, we used it a few years ago.

15
00:00:59,050 --> 00:01:03,420
Now, let's start, though, by moving this sucker into a separate file.

16
00:01:04,530 --> 00:01:13,080
So I'm going to go to my not bar, I'm sorry, this is what happens when you have a bunch of projects

17
00:01:13,080 --> 00:01:15,390
open, meaning a bunch of folders open.

18
00:01:15,660 --> 00:01:22,010
Let me go here where I have my abcess and I'm just going to create a new file.

19
00:01:22,470 --> 00:01:24,600
And this is still going to be created in the final.

20
00:01:24,600 --> 00:01:24,930
Sorry.

21
00:01:25,270 --> 00:01:31,770
So let me go here and I'll create a new file and I'll call this a logger just now.

22
00:01:31,770 --> 00:01:35,460
If you really want, you can add the middle order in name as well.

23
00:01:35,460 --> 00:01:41,450
But in my case I'm just going to add the name of my function, then I'll cut it out.

24
00:01:42,300 --> 00:01:45,150
And what's really cool that we know how to export this, right?

25
00:01:45,510 --> 00:01:52,980
So we have const logger and then we go with module exports and we'll just set it equal to our water.

26
00:01:53,340 --> 00:01:56,160
So now we have default export that our logger.

27
00:01:56,430 --> 00:02:01,260
So of course in the address I simply need to go with const logger is equal.

28
00:02:02,140 --> 00:02:12,130
And then I require it, I go with lager, and if I navigate to localhost five thousand, I still should

29
00:02:12,130 --> 00:02:17,070
see in a console my log and if you do, then everything is correct.

30
00:02:17,920 --> 00:02:25,340
So that's the first part, not a second, how can I apply this loga to all my routes?

31
00:02:25,960 --> 00:02:36,340
So, for example, let me just copy and paste these ones and I'll say API and then product and learn

32
00:02:36,340 --> 00:02:43,690
API and I don't know, not about maybe items, then it doesn't really matter what you place here.

33
00:02:44,090 --> 00:02:48,040
I'm going to go with products as well as items.

34
00:02:48,490 --> 00:02:56,500
Again, I can add them manually, but the more route I'm going to have well, the bigger issue, this

35
00:02:56,500 --> 00:03:01,850
is going to become right, because for every route I need to manually add this login.

36
00:03:02,230 --> 00:03:06,340
So a better solution is this one where I select all of them.

37
00:03:06,350 --> 00:03:08,090
I just remove them like.

38
00:03:08,110 --> 00:03:14,200
So now, of course, if I go to any of these routes, I'm not going to have anything in the console.

39
00:03:14,200 --> 00:03:19,210
But there is a method by the name of App Dot Use.

40
00:03:19,540 --> 00:03:22,990
And in that app that use, this is what we do.

41
00:03:22,990 --> 00:03:24,900
We pass in the middleware.

42
00:03:25,210 --> 00:03:27,340
So we simply go with a logger.

43
00:03:27,790 --> 00:03:40,120
And once I say check it out now if I go to about for example, or if I go to home or API and then products

44
00:03:40,120 --> 00:03:41,590
and hopefully get an idea.

45
00:03:42,530 --> 00:03:46,480
All the time you'll get of this log in council.

46
00:03:46,790 --> 00:03:47,190
Why?

47
00:03:47,240 --> 00:03:52,090
Well, because Abdah used will invoke this for any group.

48
00:03:52,340 --> 00:03:54,380
Now, please do keep in mind two things.

49
00:03:54,380 --> 00:04:03,810
First or matters here, if I'm going to place this below douget and if I'm going to try to do that in

50
00:04:03,810 --> 00:04:06,700
the home page, I'm not going to see anything in council.

51
00:04:06,710 --> 00:04:07,170
Why?

52
00:04:07,610 --> 00:04:13,600
Well, because I invoke my use only after get an express.

53
00:04:14,000 --> 00:04:16,029
Everything goes in order.

54
00:04:16,070 --> 00:04:23,150
So if I get is before the use one, the one that applies to all the out and yes.

55
00:04:23,570 --> 00:04:26,780
Well first will hit the homer and then we'll send back home.

56
00:04:26,910 --> 00:04:28,150
So there is no longer.

57
00:04:28,310 --> 00:04:35,360
So that's why you'll see all the middleware functions, all the app that uses at the top of the document.

58
00:04:35,780 --> 00:04:42,940
So you'll have your mail functions first and only then you have all your other methods, whether that's

59
00:04:42,950 --> 00:04:45,640
get post and you get an idea.

60
00:04:45,980 --> 00:04:48,620
So that's the first thing that I would like to mention now.

61
00:04:48,650 --> 00:04:54,170
The second thing that I would like to mention is the fact that we can add here a path.

62
00:04:54,350 --> 00:04:57,170
So if I go to abduct use, that's my method.

63
00:04:57,440 --> 00:05:04,130
And instead of providing only one argument, which in my case is the longer I can set up a first one

64
00:05:04,130 --> 00:05:07,960
and that is going to be path, and I just need to come up with a value.

65
00:05:08,180 --> 00:05:12,680
Now, in my case, I'm going to type API and you'll see in a second one.

66
00:05:12,980 --> 00:05:19,820
So if you save with an API, you'll notice something interesting where this is going to be apply to

67
00:05:19,820 --> 00:05:24,640
both of them, to the product as well as the items.

68
00:05:25,070 --> 00:05:25,910
So here's the deal.

69
00:05:26,120 --> 00:05:33,730
Once you apply this path over here, basically it's going to apply to any route after this API.

70
00:05:33,980 --> 00:05:41,210
So, for example, if I go with API and then some crazy one home about and then products all the time

71
00:05:41,210 --> 00:05:43,580
is going to keep on applying this more.

72
00:05:43,730 --> 00:05:44,690
So that's something new.

73
00:05:44,700 --> 00:05:52,370
Right, because previously we worked only with a specific Ruzzo forward slash or about or product here.

74
00:05:52,550 --> 00:05:59,300
In this case, of course, these are different now when we add this path to use and of course it's going

75
00:05:59,300 --> 00:06:05,330
to be applied pretty much to anything that comes after the path that you provide over here.

76
00:06:05,360 --> 00:06:12,110
So in my case, since I provided API and of course, this is going to go for any path that after that.

77
00:06:12,440 --> 00:06:18,060
Now, if you want to find out more info about Abda use, I suggest navigating to your docs.

78
00:06:18,080 --> 00:06:23,270
Again, we're looking for API reference in our case, that is for and if you take a look at the app

79
00:06:23,270 --> 00:06:30,170
that used Mount's specified middleware function or functions, and we're going to cover that later as

80
00:06:30,170 --> 00:06:32,390
well at the specified path.

81
00:06:32,960 --> 00:06:37,650
And the word function is executed when the base of the request path matches.

82
00:06:37,970 --> 00:06:40,730
So this is going to be our base.

83
00:06:41,150 --> 00:06:46,460
And then whatever comes after, we'll still invoke that middleware function.

84
00:06:46,820 --> 00:06:54,610
And if you augment the path, then this is going to be applied to all of your requests.

85
00:06:55,010 --> 00:07:03,170
So if I were to remove the path now, it's going to be applied to home about API product as well as

86
00:07:03,170 --> 00:07:04,520
API items.

