1
00:00:00,150 --> 00:00:07,260
Now, like I already previously mentioned, middleware is all over express, so just because we're done

2
00:00:07,260 --> 00:00:12,090
with our initial introduction doesn't mean that you're not going to see middleware.

3
00:00:12,240 --> 00:00:14,400
You'll see it all over the place.

4
00:00:14,700 --> 00:00:22,150
But just to complete our initial introduction, I would want to cover two common questions first.

5
00:00:22,500 --> 00:00:29,550
Well, if we have access right now to abduct use, since we know how to use it, are we going to add

6
00:00:29,760 --> 00:00:31,880
the middleware in or out?

7
00:00:32,280 --> 00:00:38,010
And actually, the answer is yes, because imagine the scenario where I don't want to apply, which

8
00:00:38,010 --> 00:00:40,100
I'm not used to all my roots.

9
00:00:40,740 --> 00:00:47,690
For example, I only want to check for authorized users in the API for items.

10
00:00:47,920 --> 00:00:48,590
What do I do?

11
00:00:48,840 --> 00:00:52,470
Well, I simply add my authorized middleware.

12
00:00:52,680 --> 00:00:53,130
Correct.

13
00:00:53,280 --> 00:00:57,420
So now I'll be able to access all of these routes without any issues.

14
00:00:57,780 --> 00:00:58,730
But the items want.

15
00:00:58,740 --> 00:00:59,060
Yes.

16
00:00:59,880 --> 00:01:06,840
I'll check for that silly query string primer and if you're wondering, well, how we can pass two middle

17
00:01:06,840 --> 00:01:13,400
words if we would want in a single round same, we just simply grab this.

18
00:01:13,680 --> 00:01:14,090
All right.

19
00:01:14,340 --> 00:01:16,170
So we copy and paste.

20
00:01:16,440 --> 00:01:22,840
And of course, once I hit the items, not only our log, but also authorized.

21
00:01:23,280 --> 00:01:29,100
So let's go back and I'm going to take a look where in this case everything is fine.

22
00:01:29,520 --> 00:01:34,380
I was able to access the resource since I provided the Johnson.

23
00:01:34,410 --> 00:01:35,070
What's your name?

24
00:01:35,070 --> 00:01:35,430
John.

25
00:01:35,640 --> 00:01:36,690
I.D. number three.

26
00:01:36,720 --> 00:01:44,460
I also have my Solok, but if I were to go to just my home page like so I have the home.

27
00:01:45,210 --> 00:01:47,810
As you can see, I don't get anything in a console.

28
00:01:47,850 --> 00:01:49,110
I don't get my logger.

29
00:01:49,860 --> 00:01:54,500
And I'm also not looking for any kind of authorized user.

30
00:01:54,870 --> 00:01:57,390
So that's the first thing that I would like to cover.

31
00:01:57,420 --> 00:02:02,570
Second, what are our options when it comes to the war?

32
00:02:02,910 --> 00:02:09,610
So far we have covered the first one our own, so we can always set up our own middleware.

33
00:02:09,990 --> 00:02:13,020
Now, our other two options are Xpress.

34
00:02:13,320 --> 00:02:17,760
So Xpress provides quite a few built in middleware functions.

35
00:02:18,120 --> 00:02:22,230
Again, in this case, we don't have to worry about setting up the functionality.

36
00:02:22,650 --> 00:02:29,730
We just need to reference the docs to see what options are provided and then of course, supply those

37
00:02:29,730 --> 00:02:30,180
values.

38
00:02:30,510 --> 00:02:32,190
And if you remember.

39
00:02:33,220 --> 00:02:39,740
The express app example, we used app use and I said, put the pin on this.

40
00:02:40,070 --> 00:02:43,630
I'll explain this a little bit later if you want, you can retype this.

41
00:02:43,630 --> 00:02:52,000
But in my case, I'm just going to copy this from the four Express app, J.S. And if we copy and paste

42
00:02:52,240 --> 00:02:53,350
and if I place it here.

43
00:02:54,010 --> 00:02:56,860
So this is a abduh use method.

44
00:02:57,280 --> 00:03:02,230
Now, what argument the app use is expecting, it is expecting a middleware.

45
00:03:02,570 --> 00:03:11,860
So in Express we have a built in middleware by the name of Statik again somewhere in the express source

46
00:03:11,860 --> 00:03:12,110
code.

47
00:03:12,370 --> 00:03:15,600
There is a code similar to this.

48
00:03:15,610 --> 00:03:16,920
Now, of course this is silly.

49
00:03:16,990 --> 00:03:20,650
We have way more complicated setup, but hopefully you get the gist.

50
00:03:20,860 --> 00:03:22,060
So there is a method.

51
00:03:22,810 --> 00:03:25,660
The only thing this method is looking for our public folder.

52
00:03:25,870 --> 00:03:33,880
And then I already told you that it basically places all the contents of public folder as our static

53
00:03:33,880 --> 00:03:34,270
assets.

54
00:03:34,280 --> 00:03:36,430
So they're publicly available.

55
00:03:36,580 --> 00:03:37,990
So we already covered that one.

56
00:03:38,180 --> 00:03:42,830
And now let me show you a simple example of third party middleware.

57
00:03:43,180 --> 00:03:49,300
Now, for third party, Millewa will have to install it on one of the most popular ones.

58
00:03:49,300 --> 00:03:52,700
I guess for the login is this Morgan and PME.

59
00:03:52,720 --> 00:03:59,260
So, again, just go to your search engine and type Morgan and RPM that is going to direct you to the

60
00:03:59,560 --> 00:04:01,780
docs, I guess, for the Morgan.

61
00:04:01,930 --> 00:04:06,760
And as you can see, in order to install one, just run 9pm.

62
00:04:06,760 --> 00:04:07,680
I'm Morgan.

63
00:04:07,870 --> 00:04:13,480
So that's how we install the package and the syntax is following where we just go with Morgan.

64
00:04:13,570 --> 00:04:17,500
We required a Morgan and then we just pass in Morgan.

65
00:04:17,860 --> 00:04:22,680
Now, of course, this one, we need to set it up in our abductor's.

66
00:04:22,960 --> 00:04:24,610
So this is really optional.

67
00:04:24,970 --> 00:04:26,290
You don't have to install it.

68
00:04:26,290 --> 00:04:27,160
You don't have to run it.

69
00:04:27,160 --> 00:04:30,750
But in order for me to hammer this home, I will do that.

70
00:04:31,120 --> 00:04:32,050
So let's go back.

71
00:04:32,050 --> 00:04:39,090
I'll start my server and remember, the command was ampm are Morgan OK?

72
00:04:39,220 --> 00:04:40,180
I installed this one.

73
00:04:40,510 --> 00:04:46,870
So now of course this package is going to be in my dependencies like so, so not only I have expressed,

74
00:04:46,870 --> 00:04:52,960
I also have the Morgan and then in The Objets which I'm going to have to require it.

75
00:04:53,320 --> 00:04:58,930
So I'm going to go with Consed Morgan and that is equal to require.

76
00:04:58,960 --> 00:05:02,080
And then again, I don't have to provide the path.

77
00:05:02,560 --> 00:05:04,050
That's the module that I installed.

78
00:05:04,060 --> 00:05:11,350
So I go with Morgan and then one and a half years ago with Abduct You, since I would want to add to

79
00:05:11,350 --> 00:05:15,910
all my roles again, if you want to add four specific one, there's a different scenario.

80
00:05:15,910 --> 00:05:19,930
But if you want to add for all the rules, just go with Morgan.

81
00:05:20,770 --> 00:05:26,980
And then they basically give you multiple options and a tiny one is just going to provide you the most

82
00:05:26,980 --> 00:05:28,220
essential data.

83
00:05:28,390 --> 00:05:31,420
And again, if you're on a reference to docs, please do so.

84
00:05:31,690 --> 00:05:38,650
So I'm going to go with Morgan and now I just need to open up my app and I go with NPM Start now.

85
00:05:38,650 --> 00:05:46,510
I will remove Loga and authorized from my API items just because I think the terminal is getting quite

86
00:05:46,510 --> 00:05:46,980
busy.

87
00:05:47,710 --> 00:05:56,020
And of course, once I navigate, for example, home page in a console, I have get I have forgot now

88
00:05:56,020 --> 00:06:02,110
this is a status code and then I have two point eight milliseconds that essentially just signals how

89
00:06:02,110 --> 00:06:05,740
long it took for the server to respond.

90
00:06:06,070 --> 00:06:13,900
And if I were to go to localhost and forward slash about now, of course, I'll see that I have get

91
00:06:13,900 --> 00:06:14,410
request.

92
00:06:14,590 --> 00:06:16,870
I have about the two hundred.

93
00:06:16,960 --> 00:06:20,370
And now of course time is a bit different as well.

94
00:06:20,740 --> 00:06:29,080
So hopefully this gives you a general idea why middleware functions are so crucial and how powerful

95
00:06:29,260 --> 00:06:29,800
they are.

96
00:06:29,890 --> 00:06:33,760
And yes, the bigger your express applications will get.

97
00:06:34,610 --> 00:06:42,170
Most likely, the more mature functions you will have now, they could be your own ones, they could

98
00:06:42,170 --> 00:06:50,210
be the ones that provided by the Express or there's also going to be tons of third party middleware

99
00:06:50,210 --> 00:06:50,720
functions.

