1
00:00:02,340 --> 00:00:03,420
Now regarding the routes,

2
00:00:03,420 --> 00:00:07,340
I'll add a new folder to dos dot routes dot JS.

3
00:00:07,340 --> 00:00:09,920
And in this folder, as always,

4
00:00:09,920 --> 00:00:14,470
we start by importing express like this,

5
00:00:14,470 --> 00:00:16,620
and we get the router by calling

6
00:00:16,620 --> 00:00:19,220
express dot router like that.

7
00:00:19,220 --> 00:00:21,130
And we of course then export this

8
00:00:21,130 --> 00:00:23,253
to make it available outside of this file.

9
00:00:24,170 --> 00:00:27,970
And then with that done, we can register our routes.

10
00:00:27,970 --> 00:00:30,350
And here for the moment, I need two routes,

11
00:00:30,350 --> 00:00:33,530
a get and a post route because these are the two controller

12
00:00:33,530 --> 00:00:35,740
actions on which we work thus far.

13
00:00:35,740 --> 00:00:37,100
And speaking of that, of course,

14
00:00:37,100 --> 00:00:40,130
we also need the to do's controller here,

15
00:00:40,130 --> 00:00:43,813
which we get by requiring controllers, to do's controller.

16
00:00:45,260 --> 00:00:48,920
Now I do again expect that these routes are loaded such that

17
00:00:48,920 --> 00:00:52,728
a certain prefix, slash to dos to be precise,

18
00:00:52,728 --> 00:00:55,980
is added automatically in app JS.

19
00:00:55,980 --> 00:00:57,260
So therefore here I can handle

20
00:00:57,260 --> 00:01:01,720
just get slash and then point at to do's controller,

21
00:01:01,720 --> 00:01:02,813
get all to dos,

22
00:01:04,170 --> 00:01:06,530
and here also have slash,

23
00:01:06,530 --> 00:01:08,590
but with a different request method,

24
00:01:08,590 --> 00:01:09,700
and they offer as you learn,

25
00:01:09,700 --> 00:01:11,050
it's a totally different route

26
00:01:11,050 --> 00:01:12,773
and a totally different end point.

27
00:01:13,860 --> 00:01:17,543
And here I will then trigger to do's controller, add to do.

28
00:01:19,660 --> 00:01:22,120
Now we need to load these routes in app JS.

29
00:01:22,120 --> 00:01:23,108
And for this here,

30
00:01:23,108 --> 00:01:26,900
I'll add my to do's routes...

31
00:01:26,900 --> 00:01:28,760
...by requiring...

32
00:01:29,800 --> 00:01:32,243
dot slash routes to dos routes.

33
00:01:33,450 --> 00:01:37,610
And I want to register them here, where I will use app use.

34
00:01:37,610 --> 00:01:42,000
And now add to that prefix I was talking about slash to dos,

35
00:01:42,000 --> 00:01:45,340
which will automatically be added in front of all the paths

36
00:01:45,340 --> 00:01:47,950
we are handling here instead of to do's routes,

37
00:01:47,950 --> 00:01:49,044
but which will be stripped

38
00:01:49,044 --> 00:01:51,623
once a request reaches this file here.

39
00:01:52,850 --> 00:01:55,610
And then here, I want to point at to do's routes.

40
00:01:55,610 --> 00:01:58,859
So all requests where the path starts with slash to dos

41
00:01:58,859 --> 00:02:01,380
will be forwarded to my to do's routes,

42
00:02:01,380 --> 00:02:03,293
and then it will be handled there.

43
00:02:04,640 --> 00:02:06,310
Now there is one important middleware

44
00:02:06,310 --> 00:02:07,400
which we'll have to add,

45
00:02:07,400 --> 00:02:10,110
which we didn't add before in the last API.

46
00:02:10,110 --> 00:02:14,620
And that's adjacent body parser middleware because of course

47
00:02:14,620 --> 00:02:16,768
in to do's controller, when we add a to do,

48
00:02:16,768 --> 00:02:20,770
I'm extracting data from the incoming request body.

49
00:02:20,770 --> 00:02:22,190
And for this to succeed,

50
00:02:22,190 --> 00:02:25,250
we need a middleware that actually reads that incoming

51
00:02:25,250 --> 00:02:29,230
request body, and makes it available through JavaScript, on

52
00:02:29,230 --> 00:02:31,953
this body object, on the request object.

53
00:02:32,930 --> 00:02:33,763
And of course,

54
00:02:33,763 --> 00:02:36,320
this is also something we already solved before in the

55
00:02:36,320 --> 00:02:39,070
course, but I want to emphasize it again here.

56
00:02:39,070 --> 00:02:42,360
We now need to use a new middleware and that's the express

57
00:02:42,360 --> 00:02:45,500
dot J son middleware. Where to J son method,

58
00:02:45,500 --> 00:02:48,230
which will then yield the actual middleware,

59
00:02:48,230 --> 00:02:52,650
which will scan all incoming requests for their content

60
00:02:52,650 --> 00:02:55,780
type. And if it's J son, it will parse the request

61
00:02:55,780 --> 00:03:00,755
body as J son and expose it under this rec dot body field so

62
00:03:00,755 --> 00:03:04,160
that we can work with it in our other middleware functions,

63
00:03:04,160 --> 00:03:06,393
and in our controller action, stale four.

64
00:03:07,810 --> 00:03:11,273
And then with that, we should be able to get that data.

65
00:03:12,640 --> 00:03:15,980
Now with all that done, we added these two routes,

66
00:03:15,980 --> 00:03:19,109
we added the controllers, and the model, and the middleware.

67
00:03:19,109 --> 00:03:24,109
This should be working, but how can we now test this.

