1
00:00:06,670 --> 00:00:07,990
Welcome back.

2
00:00:08,020 --> 00:00:16,149
Before we create a middleware to check if a user is logged in, let me show you another way of creating

3
00:00:16,149 --> 00:00:21,040
middleware and how we can make this off middleware chaining.

4
00:00:21,070 --> 00:00:27,130
So for this syntax, we can create the same middleware using this syntax.

5
00:00:27,220 --> 00:00:34,540
So instead of querying the function and then use it inside app to use, we can create a function directly

6
00:00:34,540 --> 00:00:36,400
inside app store to use.

7
00:00:36,430 --> 00:00:37,870
So now here we go.

8
00:00:37,870 --> 00:00:40,240
And that is going to be the middle word.

9
00:00:40,330 --> 00:00:44,380
So here we can make use of normal function or function for this one.

10
00:00:44,380 --> 00:00:52,270
Let's make use of normal function and you have access to requests, response and the next function and

11
00:00:52,270 --> 00:00:55,510
here you go so I can go ahead and console.log.

12
00:00:55,510 --> 00:01:02,770
This one, which is the same thing now I can comment this one, and now if I save it and I make a request

13
00:01:02,770 --> 00:01:05,470
to that root and now check it out.

14
00:01:05,470 --> 00:01:14,740
Inside I see a logo, but I don't see the response, meaning that I'm not calling next inside this middle

15
00:01:14,800 --> 00:01:15,330
word.

16
00:01:15,370 --> 00:01:16,660
That is the problem.

17
00:01:16,660 --> 00:01:18,490
So now let me call it as that.

18
00:01:18,490 --> 00:01:19,150
Save it.

19
00:01:19,150 --> 00:01:21,850
And now let's make the request again.

20
00:01:21,850 --> 00:01:27,670
I have my response and I have my middleware being called, which is awesome.

21
00:01:27,670 --> 00:01:31,630
So any of these ways we can create a merge word.

22
00:01:31,840 --> 00:01:35,720
So now what about chaining of middleware?

23
00:01:35,740 --> 00:01:37,420
So here we go.

24
00:01:37,600 --> 00:01:48,400
Now let me comment this one and let me give it a beautiful comment here says chaining matter where.

25
00:01:48,490 --> 00:01:49,510
So here we go.

26
00:01:49,510 --> 00:01:57,990
We make use of app dot use and inside we pass in a callback function which is our middleware.

27
00:01:58,000 --> 00:02:07,510
So the first one have access to request response and then next and then every function has that.

28
00:02:07,960 --> 00:02:11,470
So here I'll console.log the first middleware.

29
00:02:11,470 --> 00:02:15,130
I would say that first middle.

30
00:02:15,950 --> 00:02:16,760
Where.

31
00:02:16,910 --> 00:02:19,720
And let me call it next hearing.

32
00:02:19,730 --> 00:02:21,000
And here we go.

33
00:02:21,050 --> 00:02:30,350
And then on that, I can change by make use of dot and then use and I can add any callback function

34
00:02:30,350 --> 00:02:32,150
here, which is a murderer.

35
00:02:32,150 --> 00:02:38,600
So I can copy this one and now paste it here as that as my second murderer.

36
00:02:38,600 --> 00:02:42,290
And I'll change this one to second murderer.

37
00:02:42,290 --> 00:02:49,580
And to change more I'll make use of dot and use and then paste in my callback function as that and change

38
00:02:49,580 --> 00:02:54,560
this one to third middle where let me change this one to uppercase.

39
00:02:54,560 --> 00:02:59,810
And now I can change as many as I want in the pipeline.

40
00:03:00,050 --> 00:03:00,470
All right.

41
00:03:00,470 --> 00:03:03,800
So now when I save this one, I make a request.

42
00:03:03,800 --> 00:03:05,060
Now check it out.

43
00:03:05,090 --> 00:03:06,080
Let's see.

44
00:03:06,080 --> 00:03:10,310
First were just called, second one called and third also called.

45
00:03:10,670 --> 00:03:11,150
All right.

46
00:03:11,150 --> 00:03:12,740
But inside the first middle.

47
00:03:12,740 --> 00:03:19,790
Well, let's see that I refuse to break next what is going to happen, meaning that after this one being

48
00:03:19,790 --> 00:03:25,280
called the next middle way here will not be called because I'm not calling next year.

49
00:03:25,280 --> 00:03:26,720
So let's check it out.

50
00:03:26,750 --> 00:03:29,450
Moment of truth when I hit send.

51
00:03:29,450 --> 00:03:30,830
Now, let's see.

52
00:03:30,830 --> 00:03:35,900
You can see that I get the first middle way because that is the first one in the pipeline.

53
00:03:35,900 --> 00:03:41,960
And since I'm not calling, next express is still hanging waiting for the next to be called.

54
00:03:42,230 --> 00:03:48,020
But as soon as I provide next to this one as that.

55
00:03:50,700 --> 00:03:54,720
And when I say it now, let me remove this one here.

56
00:03:54,750 --> 00:03:56,100
So let's see the flow.

57
00:03:56,130 --> 00:04:01,980
The first one will console.log and then move to the next middleware, which is this one, and it will

58
00:04:01,980 --> 00:04:02,550
call.

59
00:04:02,550 --> 00:04:04,800
But after that there is no next.

60
00:04:04,800 --> 00:04:09,540
So it will stop here and the rest of the code down here will not run.

61
00:04:09,540 --> 00:04:11,120
So let's check it out.

62
00:04:11,130 --> 00:04:18,089
When I make the request and I check my terminal, you can see that I have first, second and third guys

63
00:04:18,089 --> 00:04:19,440
take note about this.

64
00:04:19,649 --> 00:04:23,660
The order of a middleware matters a lot.

65
00:04:23,700 --> 00:04:25,620
So here is the clue.

66
00:04:25,650 --> 00:04:33,180
Always put your middleware at the top before your route handles, because this is the first middleware

67
00:04:33,180 --> 00:04:33,960
here.

68
00:04:33,960 --> 00:04:36,230
And next is those down here.

69
00:04:36,240 --> 00:04:38,700
That is a route I also made reverse.

70
00:04:38,880 --> 00:04:45,810
So this one will call and then the next minute will be called by if I cut this one.

71
00:04:46,640 --> 00:04:50,690
Cut it and then let me place it below all my roots here.

72
00:04:51,590 --> 00:04:58,520
Even though let me remove the next one here and remove this one, meaning that by default the response

73
00:04:58,520 --> 00:04:59,660
will never be displayed.

74
00:04:59,660 --> 00:05:02,130
But let's check it out when I make the request.

75
00:05:02,150 --> 00:05:09,050
You can see that I get a response and when I check my terminal there is no matter where being called

76
00:05:09,080 --> 00:05:11,500
is because of the order.

77
00:05:11,510 --> 00:05:15,040
So pay attention about the order.

78
00:05:15,050 --> 00:05:18,740
So let me bring it back to the top here.

79
00:05:18,950 --> 00:05:24,110
So this is how we can change middleware and how we can arrange middleware.

80
00:05:24,290 --> 00:05:30,980
The next video, let's see how we can create a measure to check if a user is logged in.

