1
00:00:05,790 --> 00:00:10,410
Let's get started with express Middle words.

2
00:00:10,920 --> 00:00:13,830
We have two types of middle words.

3
00:00:13,860 --> 00:00:16,770
One is custom materials.

4
00:00:16,800 --> 00:00:22,140
That is materials that we are going to create by ourselves.

5
00:00:22,530 --> 00:00:30,240
And we also have third party middleware where we can download from NPM registry.

6
00:00:30,720 --> 00:00:36,940
So in this video, we are going to work with custom media words.

7
00:00:36,960 --> 00:00:39,730
So back to Visual Studio Code.

8
00:00:39,750 --> 00:00:46,020
I have already created a simple express server with few lines of code.

9
00:00:46,350 --> 00:00:49,500
And I'll provide this file for you.

10
00:00:49,530 --> 00:00:55,230
But if you want to use your custom express server, you can go ahead and do so.

11
00:00:55,410 --> 00:00:58,710
So let's go through this code line by line.

12
00:00:58,800 --> 00:01:04,830
We have the first route, which is home route, which sends hello word to the user.

13
00:01:05,010 --> 00:01:07,590
The next route is a logging route.

14
00:01:07,590 --> 00:01:11,070
And this one send logging successful.

15
00:01:11,400 --> 00:01:15,020
And the next route is a route to create post.

16
00:01:15,030 --> 00:01:19,020
And all what is doing is just to send post created.

17
00:01:19,200 --> 00:01:24,300
And for the description we have row which is authenticated user.

18
00:01:24,330 --> 00:01:30,690
Meaning that this route you need to be logged in before you can use this route.

19
00:01:30,750 --> 00:01:37,650
Well, we have not talked about authentication and authorization yet, but let's try to mimic.

20
00:01:37,680 --> 00:01:38,240
All right.

21
00:01:38,250 --> 00:01:44,370
So it means that this route you need to log in before you can create a post.

22
00:01:44,700 --> 00:01:47,790
And the next route is to fetch all posts.

23
00:01:47,790 --> 00:01:54,780
And for description, it says that public meaning that whether you have logged in or not, you can still

24
00:01:54,780 --> 00:01:57,480
have access to this route.

25
00:01:57,660 --> 00:02:00,760
And the last route is to delete a post.

26
00:02:00,780 --> 00:02:06,840
So for this action, unless you are an admin before you can delete a post.

27
00:02:06,900 --> 00:02:09,960
So how can we achieve all this logic?

28
00:02:09,990 --> 00:02:14,640
Well, we can do so using what is called middleware.

29
00:02:14,670 --> 00:02:18,060
So how can we make use of middleware?

30
00:02:18,120 --> 00:02:20,780
The first step is we need to create it.

31
00:02:20,790 --> 00:02:22,500
So let's have a route map.

32
00:02:22,530 --> 00:02:25,950
The first step is what creates middleware.

33
00:02:26,130 --> 00:02:27,210
I start.

34
00:02:27,300 --> 00:02:28,920
So let's go over again.

35
00:02:28,920 --> 00:02:30,390
What is middleware?

36
00:02:30,420 --> 00:02:36,210
So middleware is a function that have access to the request and the response object.

37
00:02:36,270 --> 00:02:41,970
And if you have access to the request and response object, it means that we can do whatever we want

38
00:02:41,970 --> 00:02:46,350
with it before we send the response or before getting to the server.

39
00:02:46,380 --> 00:02:47,610
So here we go.

40
00:02:47,640 --> 00:02:53,730
Since middleware is a function, we can use normal function syntax to create middleware.

41
00:02:53,940 --> 00:03:00,900
So the middleware that we are going to create is called logger, which means that any time a user visit

42
00:03:00,900 --> 00:03:04,950
any of our root, we want to display a message to the console.

43
00:03:04,980 --> 00:03:07,260
So let's see how we can create that.

44
00:03:07,440 --> 00:03:09,990
For this one, I'm going to use a function.

45
00:03:09,990 --> 00:03:11,950
You can use normal functions syntax.

46
00:03:11,970 --> 00:03:13,200
So let's go.

47
00:03:13,410 --> 00:03:22,380
The first step is the name of the middleware, and I'll call it as logger and equal to my error function.

48
00:03:22,380 --> 00:03:25,230
And this is a normal function syntax.

49
00:03:25,260 --> 00:03:29,970
The next step is the argument that this function takes For many words.

50
00:03:29,970 --> 00:03:32,220
It takes in three arguments.

51
00:03:32,250 --> 00:03:34,320
The first one is a request.

52
00:03:34,350 --> 00:03:38,850
Second one is response, and the next one is called next.

53
00:03:39,060 --> 00:03:46,560
So these three arguments, the first one represents the request the user is making, and then the response

54
00:03:46,560 --> 00:03:49,820
is what is going to be sent back to the user?

55
00:03:49,830 --> 00:03:51,830
And the next is a function.

56
00:03:52,320 --> 00:03:57,000
Do you remember we've talked about the next function in the previous video.

57
00:03:57,120 --> 00:04:04,260
So the next function is a function that allows another murderer to proceed in the pipeline or in the

58
00:04:04,260 --> 00:04:04,790
chain.

59
00:04:04,800 --> 00:04:07,200
And I'll show you in just a second.

60
00:04:07,290 --> 00:04:13,740
So inside the function body here, we can make use of any logic here.

61
00:04:13,890 --> 00:04:17,339
For the meantime, we are just console.log in a message.

62
00:04:17,339 --> 00:04:21,690
Says that a logger middle.

63
00:04:22,670 --> 00:04:23,410
Where.

64
00:04:23,420 --> 00:04:26,640
So now we have created our first middleware.

65
00:04:26,660 --> 00:04:28,220
How can we use it?

66
00:04:28,310 --> 00:04:29,620
So let's have a comment.

67
00:04:29,630 --> 00:04:32,120
Says Use middleware.

68
00:04:32,510 --> 00:04:38,590
So how can we use it and express make it pretty simple to plug in modules.

69
00:04:38,600 --> 00:04:39,770
And here you go.

70
00:04:39,920 --> 00:04:42,140
We make use of app on that.

71
00:04:42,140 --> 00:04:48,730
We have what is used and the app is an instance of express and we have a method called use.

72
00:04:48,740 --> 00:04:55,970
So you simply means that we are plugging in or we are using another function, which is what middleware

73
00:04:55,970 --> 00:04:59,390
to enhance the functionality of our server.

74
00:04:59,480 --> 00:05:06,170
So with upload use, we can plug in any function where we can add our own logic into it.

75
00:05:06,170 --> 00:05:08,690
And the first one is called the Major world.

76
00:05:08,840 --> 00:05:16,850
So we pass in the logger middleware and don't call it as this, but instead what use is going to call

77
00:05:16,850 --> 00:05:20,150
the logger for us, and that is our murderer.

78
00:05:20,210 --> 00:05:25,520
So now when I save it, make sure that your server is up and running.

79
00:05:25,520 --> 00:05:31,100
And let's get back to Postman and I have created a collection called Middleware.

80
00:05:31,100 --> 00:05:34,730
And inside I have one request to fetch our post.

81
00:05:34,730 --> 00:05:39,550
So I'm trying to hit this route called Fetch or Post.

82
00:05:39,560 --> 00:05:44,920
So let's scroll down here and this is a route I want to hit.

83
00:05:44,930 --> 00:05:48,680
So now have a look when I hit send.

84
00:05:48,710 --> 00:05:51,200
Now you can see something going on here.

85
00:05:51,230 --> 00:05:53,360
Two things are going on here.

86
00:05:53,390 --> 00:06:00,290
First one is we are not getting the response from the root and we see what is called sending requests.

87
00:06:00,410 --> 00:06:08,210
The next step is if you check our terminal, we have a message that I'm a logger, which means that

88
00:06:08,210 --> 00:06:14,630
our middleware has been called, but we are not getting the response from the root.

89
00:06:14,630 --> 00:06:16,430
So why is it so?

90
00:06:16,820 --> 00:06:24,350
Well, the first point is that any time you make a request to any of our root, this middleware function

91
00:06:24,350 --> 00:06:25,160
will be called.

92
00:06:25,490 --> 00:06:32,960
The next step is that unless we manually tell, express that please proceed to the next network.

93
00:06:32,990 --> 00:06:39,230
That is why the next function is very important here, or it shines here.

94
00:06:39,290 --> 00:06:47,150
So here to be able to allow Express to proceed on to the next middleware we are going to call next.

95
00:06:47,150 --> 00:06:53,260
And this function is given to us by express automatically and we can name it whatever you want.

96
00:06:53,270 --> 00:06:54,650
It can be anything.

97
00:06:54,650 --> 00:06:58,010
But by convention we make use of next.

98
00:06:58,190 --> 00:07:03,800
So when I save it now, when I cancel the request, now you see that our server has hang out.

99
00:07:03,830 --> 00:07:06,170
Now let's make the request again.

100
00:07:06,170 --> 00:07:12,410
And you can say that now I get my response and I also get my middleware message.

101
00:07:12,470 --> 00:07:16,430
So now you can see how important the next function is.

102
00:07:16,730 --> 00:07:21,020
So the next function allows experts to move on to the next middleware.

103
00:07:21,140 --> 00:07:24,370
So we be asking, we have only one middleware.

104
00:07:24,380 --> 00:07:26,390
So what is the next middleware?

105
00:07:26,660 --> 00:07:32,190
Remember, the root handle is and this is our root handler.

106
00:07:32,210 --> 00:07:40,760
Let me show you after our path here, we have the callback function and this is our request handler

107
00:07:40,760 --> 00:07:43,610
and is a middleware also.

108
00:07:43,910 --> 00:07:52,850
So the first step is that as soon as we hit that root, first is going to call the middleware, this

109
00:07:52,850 --> 00:07:53,870
middleware here.

110
00:07:54,320 --> 00:08:02,540
And then if it is next here, then Express will push on to the next middleware, which is this root

111
00:08:02,540 --> 00:08:07,460
that you called, which is this root that we call this one.

112
00:08:07,910 --> 00:08:09,770
So this also a middleware.

113
00:08:09,920 --> 00:08:10,340
All right.

114
00:08:10,340 --> 00:08:12,800
So now this is how you create a murderer.

115
00:08:13,100 --> 00:08:20,120
So in the next video, let's go ahead and create a mature where it can display a message that please

116
00:08:20,120 --> 00:08:22,250
log in before you can access it.

117
00:08:22,280 --> 00:08:24,650
Let's get into that in the next video.

