1
00:00:00,030 --> 00:00:00,380
All right.

2
00:00:00,380 --> 00:00:07,060
And before we set up the controllers and our first router, I also want to add two more Middlewares,

3
00:00:07,070 --> 00:00:09,590
one for NotFound requests.

4
00:00:09,590 --> 00:00:15,140
And the second one is going to be the error middleware and then in the next video.

5
00:00:15,950 --> 00:00:21,740
I'll discuss the differences because this is a very common question in the course Q&A.

6
00:00:21,770 --> 00:00:24,920
So first of all, let's navigate to a server.

7
00:00:25,160 --> 00:00:27,830
And after all our requests.

8
00:00:27,830 --> 00:00:33,020
So again, right here where I have the port, I want to go with app dot use.

9
00:00:33,050 --> 00:00:40,520
And if we provide a star over here, essentially this is going to apply to all of the requests.

10
00:00:40,520 --> 00:00:43,660
So notice we're not using any kind of method.

11
00:00:43,670 --> 00:00:47,800
So in here I say app dot use, which means any method.

12
00:00:47,810 --> 00:00:54,380
And as far as the URL is going to be all the URLs because we have the star and then we need to provide

13
00:00:54,380 --> 00:00:55,010
here.

14
00:00:56,060 --> 00:00:57,050
Rack and raise.

15
00:00:57,050 --> 00:00:58,700
So that's our response.

16
00:00:58,990 --> 00:01:03,330
And essentially, I just want to go with resort status and 404.

17
00:01:03,350 --> 00:01:10,010
So pretty much every time the user is going to try to access some kind of resource that we don't have

18
00:01:10,010 --> 00:01:18,620
on our server, we're going to send back 404 and then Dot and Json and we're going to go over here with

19
00:01:18,620 --> 00:01:21,380
message not found.

20
00:01:21,740 --> 00:01:26,180
Now before we test out our middleware.

21
00:01:27,070 --> 00:01:32,110
I know it's a little bit confusing probably, but let me comment this one out and let me just showcase

22
00:01:32,110 --> 00:01:32,710
that.

23
00:01:32,710 --> 00:01:36,100
Express already sends back the error message.

24
00:01:36,100 --> 00:01:38,410
So let me go to all routes.

25
00:01:39,070 --> 00:01:41,620
Again, it doesn't really matter which request you're making.

26
00:01:41,620 --> 00:01:43,500
In my case, I'm going to go with get request.

27
00:01:43,510 --> 00:01:47,980
That's going to be the easiest one and just change something about this URL.

28
00:01:48,310 --> 00:01:51,820
Add an extra s and you'll notice this response.

29
00:01:51,820 --> 00:01:53,270
404 not found.

30
00:01:53,290 --> 00:01:56,920
So this is what the Express is sending back.

31
00:01:57,310 --> 00:02:00,880
And effectively in our case, we want to change that.

32
00:02:00,970 --> 00:02:02,920
We want to set up our own.

33
00:02:03,800 --> 00:02:06,200
Not found middleware again.

34
00:02:06,200 --> 00:02:08,389
It needs to be right after all of the routes.

35
00:02:08,389 --> 00:02:14,810
So basically, if none of these routes match because the way the Express reads, the routes, the order

36
00:02:14,810 --> 00:02:21,170
matters, and then if none of those routes match, then we simply provide this response.

37
00:02:21,170 --> 00:02:23,410
So I'm going to go back over here to get all jobs.

38
00:02:23,420 --> 00:02:24,170
Let's send it here.

39
00:02:24,170 --> 00:02:30,680
And now notice I'll have the same status code 404, but the response is going to be in the Json format,

40
00:02:30,680 --> 00:02:36,050
and I'm going to send back the message not found and also in Express.

41
00:02:36,900 --> 00:02:41,790
We can set up a error middleware, which is very useful when we want to handle errors.

42
00:02:41,790 --> 00:02:44,940
And like I mentioned before, when we worked on the front end.

43
00:02:45,540 --> 00:02:49,180
Handling errors is going to be big part of the application.

44
00:02:49,200 --> 00:02:53,370
Now, one rule this middleware has to be the last one.

45
00:02:53,370 --> 00:02:59,190
And yes, in the following video, I'll discuss the differences between the 404 and the error middleware,

46
00:02:59,520 --> 00:03:02,280
but make sure it's the last one.

47
00:03:02,280 --> 00:03:07,650
And as far as the controller, it's going to have access to four parameters.

48
00:03:07,650 --> 00:03:09,570
So how is that going to look like?

49
00:03:09,930 --> 00:03:12,000
We'll go with App.use.

50
00:03:12,030 --> 00:03:15,180
We're not going to provide any kind of URL.

51
00:03:15,420 --> 00:03:22,740
And in here, like I said, in the controller, there are four parameters and the first one is the error.

52
00:03:22,740 --> 00:03:25,300
So that's where we'll see the actual error.

53
00:03:25,320 --> 00:03:29,160
Then req res and also a next one.

54
00:03:29,890 --> 00:03:32,950
And as far as the next we'll talk about a little bit later.

55
00:03:32,950 --> 00:03:37,120
I mean, again, my expectation is that you are already familiar with these things.

56
00:03:37,120 --> 00:03:43,420
But if anything, we'll talk about this next one a little bit later when we have a different middleware.

57
00:03:43,420 --> 00:03:45,160
So now let's go with Log.

58
00:03:45,610 --> 00:03:48,190
We'll definitely log the error.

59
00:03:48,220 --> 00:03:53,620
That's something very useful as far as the response will go with dot status.

60
00:03:53,620 --> 00:03:58,960
And for now, let's just go with something generic Again, these things will change, but for now we'll

61
00:03:58,960 --> 00:04:04,990
go with the rest Dot status 500, which just means that there was a generic server error.

62
00:04:05,000 --> 00:04:10,530
And then as far as the message, we'll go with something went wrong.

63
00:04:10,540 --> 00:04:15,730
And then in the following video, like I said, I'll talk about the differences between these two,

64
00:04:15,820 --> 00:04:22,120
the 404 and error, and also we'll test out our error middleware.

