1
00:00:00,270 --> 00:00:06,630
So like I mentioned in the previous video, I've been getting some questions why this order is important,

2
00:00:07,260 --> 00:00:12,930
so essentially why replace or for beforo around one?

3
00:00:13,680 --> 00:00:15,510
And let me show you with an example.

4
00:00:16,170 --> 00:00:19,700
So at the moment, I only have the homepage route correct.

5
00:00:19,710 --> 00:00:23,130
So we have to get something for for Slashdot, our home page.

6
00:00:23,580 --> 00:00:31,470
So pretty much if I'm going to look for for its lush apples, I should get the four or four correct

7
00:00:31,470 --> 00:00:33,060
because Root does not exist.

8
00:00:33,510 --> 00:00:39,480
So let me navigate back to your browser window and I'm going to be looking for localhost 5000.

9
00:00:40,020 --> 00:00:42,080
And let's just type here Apple's.

10
00:00:42,840 --> 00:00:47,260
And once I hit the root, we should see Root does not exist.

11
00:00:47,280 --> 00:00:47,730
Correct.

12
00:00:48,120 --> 00:00:50,310
Now let's think about why is it happening?

13
00:00:50,460 --> 00:00:57,690
Well, once the user gets their old brand, express is like, OK, let me check all the root.

14
00:00:57,860 --> 00:01:02,670
So first we run through all the Midwest, which in this case, it doesn't really matter because we don't

15
00:01:02,670 --> 00:01:05,129
have the JSON data to begin with.

16
00:01:05,340 --> 00:01:12,090
But regardless, we still go through this middleware first, then Express tries to hit all the roots.

17
00:01:12,120 --> 00:01:15,690
Now, in this case, we only have one, but the same thing would work.

18
00:01:16,170 --> 00:01:17,860
Let's go back to.

19
00:01:18,090 --> 00:01:18,790
I don't know which one.

20
00:01:18,790 --> 00:01:22,320
When I wore out, I think it was the jobs one.

21
00:01:22,740 --> 00:01:25,200
Sorry for the detour, but this is very, very important.

22
00:01:25,650 --> 00:01:27,180
Well, we have the Midwest, correct?

23
00:01:27,490 --> 00:01:29,210
And then we have roots.

24
00:01:29,490 --> 00:01:35,370
In this case, it's set up with a router, so this will still work the same way regardless.

25
00:01:35,670 --> 00:01:39,620
In this case, we just have one root, and that's why it's pretty easy to show you.

26
00:01:40,220 --> 00:01:46,590
So Express tries to hit all the roots in this case only homepage, and if it doesn't find the root,

27
00:01:46,920 --> 00:01:49,710
you automatically end up over here.

28
00:01:50,160 --> 00:01:51,090
That's very important.

29
00:01:51,090 --> 00:01:52,200
That's the 404.

30
00:01:52,500 --> 00:01:56,820
That's why if I go to forward slash apples, I'll get the 404.

31
00:01:57,180 --> 00:02:00,660
And also notice something interesting where in the forum for.

32
00:02:01,630 --> 00:02:03,430
We are not calling next.

33
00:02:04,150 --> 00:02:06,280
Once we hit the four or four, that's it.

34
00:02:06,490 --> 00:02:07,180
We are done.

35
00:02:07,690 --> 00:02:11,920
We're saying, Hey, rule does not exist in this project.

36
00:02:12,160 --> 00:02:13,210
Hopefully, that is clear.

37
00:02:13,570 --> 00:02:18,220
So therefore, we place this for 04 after all the droughts.

38
00:02:18,220 --> 00:02:24,820
And now the next question is, well, why don't we place the air handler one before the four or four?

39
00:02:25,270 --> 00:02:30,640
Well, because by express rules, we need to set this one as the last one.

40
00:02:30,970 --> 00:02:38,800
And when it comes to invoking this one, well, this middleware, actually, we invoke it from the successful

41
00:02:39,040 --> 00:02:39,820
request.

42
00:02:40,060 --> 00:02:41,440
Now what am I mean by that?

43
00:02:41,770 --> 00:02:44,590
Well, if I go back here to the home page?

44
00:02:45,630 --> 00:02:53,940
And in the controller, if I'm just going to go throw new error and I'll just say there, hello there,

45
00:02:54,780 --> 00:03:01,710
and if I'll say it and if we'll go back to localhost 5000 basically a whole world, you'll see this

46
00:03:01,710 --> 00:03:02,140
message.

47
00:03:02,160 --> 00:03:02,640
Hello there.

48
00:03:02,670 --> 00:03:03,120
Why?

49
00:03:03,570 --> 00:03:09,990
Well, because of this one error handler one is invoked in the actual existing route.

50
00:03:10,530 --> 00:03:16,990
So there's no way for you to get this error handler one unless you actually hit the existing route.

51
00:03:17,010 --> 00:03:21,780
If there is no route, she's going to check all that out and is going to say, All right, so here it

52
00:03:21,780 --> 00:03:21,990
is.

53
00:03:22,110 --> 00:03:22,590
Here it is.

54
00:03:22,590 --> 00:03:23,220
Four or four.

55
00:03:23,490 --> 00:03:28,140
Now, this one on purpose per express rules, is set as a last one.

56
00:03:28,440 --> 00:03:32,610
Because of this one, we only hit from the existing around.

57
00:03:33,060 --> 00:03:36,240
So you won't hear it from the random route and just get the error.

58
00:03:36,270 --> 00:03:37,320
That's not how it works.

59
00:03:37,770 --> 00:03:44,220
Only if in one of the existing routes, you throw an arrow like we do with our custom ones, only then

60
00:03:44,490 --> 00:03:46,290
is going to be handled over here.

61
00:03:46,620 --> 00:03:50,820
Hopefully, I made myself clear and hopefully answered all the questions.

