1
00:00:02,029 --> 00:00:03,210
Now, in the last lecture,

2
00:00:03,210 --> 00:00:06,370
I mentioned that all these database operations

3
00:00:06,370 --> 00:00:09,340
in these route files are another aspect

4
00:00:09,340 --> 00:00:14,340
that could probably be refactored or outsourced

5
00:00:14,550 --> 00:00:18,400
to reduce the amount of code in those route files

6
00:00:18,400 --> 00:00:21,280
and put that code somewhere else.

7
00:00:21,280 --> 00:00:23,830
And for that, we got a specific pattern,

8
00:00:23,830 --> 00:00:24,960
which is very common.

9
00:00:24,960 --> 00:00:27,350
And which we can use here as well.

10
00:00:27,350 --> 00:00:29,920
As everything, it's not a must use pattern,

11
00:00:29,920 --> 00:00:32,500
but it is, as I just said, a very common pattern.

12
00:00:32,500 --> 00:00:36,190
And you will see it in a lot of web projects, though,

13
00:00:36,190 --> 00:00:37,690
not just there.

14
00:00:37,690 --> 00:00:40,370
Instead, it's a general programming pattern,

15
00:00:40,370 --> 00:00:44,080
which you will often see if you're building applications

16
00:00:44,080 --> 00:00:47,650
or websites that have user-facing parts.

17
00:00:47,650 --> 00:00:50,320
So things that can be seen by the user.

18
00:00:50,320 --> 00:00:54,300
And then a lot of backend logic parts you could say.

19
00:00:54,300 --> 00:00:56,380
And that's the MVC pattern,

20
00:00:56,380 --> 00:00:59,683
which stands for model view controller.

21
00:01:00,660 --> 00:01:02,270
Now, what are these things

22
00:01:02,270 --> 00:01:05,743
and how do we apply this pattern to our code base?

23
00:01:06,680 --> 00:01:10,120
The idea is that you split your main code

24
00:01:10,120 --> 00:01:13,650
that is involved in handling incoming requests

25
00:01:13,650 --> 00:01:18,373
and sending back responses into these three main areas.

26
00:01:19,350 --> 00:01:22,210
The model area is about all the logic

27
00:01:22,210 --> 00:01:25,500
that's related to working with your data source,

28
00:01:25,500 --> 00:01:28,220
your database, for example.

29
00:01:28,220 --> 00:01:30,700
So all the logic that is involved there.

30
00:01:30,700 --> 00:01:33,350
So for example, storing blog posts,

31
00:01:33,350 --> 00:01:35,990
fetching blog posts, and so on.

32
00:01:35,990 --> 00:01:39,960
That all should typically go into your model files.

33
00:01:39,960 --> 00:01:41,990
And of course, you will see how we can create

34
00:01:41,990 --> 00:01:44,923
such model files throughout this section.

35
00:01:46,020 --> 00:01:49,180
Now, here, you would therefore defined functions

36
00:01:49,180 --> 00:01:53,030
or methods for storing data in databases

37
00:01:53,030 --> 00:01:55,490
or fetching data from a database.

38
00:01:55,490 --> 00:01:59,080
Though, the model concept really applies

39
00:01:59,080 --> 00:02:01,900
no matter which data source you might be using.

40
00:02:01,900 --> 00:02:04,110
If you are working with file storage,

41
00:02:04,110 --> 00:02:07,403
you could also implement that with that MVC pattern.

42
00:02:08,810 --> 00:02:13,010
Now, the view part is the part we already are using.

43
00:02:13,010 --> 00:02:14,620
We have our templates.

44
00:02:14,620 --> 00:02:16,980
And the templates are the building blocks

45
00:02:16,980 --> 00:02:19,050
that contain the logic for presenting

46
00:02:19,050 --> 00:02:21,743
the data or the content to the user.

47
00:02:22,700 --> 00:02:24,460
So we're already doing that.

48
00:02:24,460 --> 00:02:26,970
And in those templates, we are of course,

49
00:02:26,970 --> 00:02:30,940
using the EJS templating language with all its tags

50
00:02:30,940 --> 00:02:35,050
that it's offering to us to output repeated content

51
00:02:35,050 --> 00:02:36,810
or conditional content.

52
00:02:36,810 --> 00:02:40,473
And therefore, that logic already is in the right place.

53
00:02:41,940 --> 00:02:45,310
In case you're wondering where else it should be stored.

54
00:02:45,310 --> 00:02:47,790
Keep in mind that when we got started

55
00:02:47,790 --> 00:02:50,660
with diving into backend development,

56
00:02:50,660 --> 00:02:55,660
we started by defining the HTML code that should be returned

57
00:02:56,180 --> 00:03:00,440
directly in our request handler functions.

58
00:03:00,440 --> 00:03:03,720
We didn't use templates right from the start.

59
00:03:03,720 --> 00:03:05,100
And there for example,

60
00:03:05,100 --> 00:03:09,100
we therefore didn't have that separate view logic

61
00:03:09,100 --> 00:03:11,210
or that separate view part.

62
00:03:11,210 --> 00:03:14,030
Instead, we had everything in our JavaScript code

63
00:03:14,030 --> 00:03:16,720
and everything in the same file.

64
00:03:16,720 --> 00:03:19,990
So we're using this view logic already,

65
00:03:19,990 --> 00:03:22,560
but we didn't use it right from the start.

66
00:03:22,560 --> 00:03:26,020
And using it as we're currently doing it is recommended,

67
00:03:26,020 --> 00:03:29,960
especially when using the MVC pattern, but not only then.

68
00:03:29,960 --> 00:03:33,763
Instead, views should always have separate template files.

69
00:03:35,490 --> 00:03:37,500
Now, the last part is the controller.

70
00:03:37,500 --> 00:03:40,710
And that's in the end a code that contains

71
00:03:40,710 --> 00:03:44,810
the logic for connecting your models to your views.

72
00:03:44,810 --> 00:03:48,320
So that is the part where you work with your models

73
00:03:48,320 --> 00:03:51,800
to tell them to fetch data, for example.

74
00:03:51,800 --> 00:03:56,410
And where you then pass that fetched data into your views.

75
00:03:56,410 --> 00:04:00,230
And therefore, controllers are typically the functions

76
00:04:00,230 --> 00:04:04,340
that are executed when your routes trigger.

77
00:04:04,340 --> 00:04:06,260
So you could say that the code

78
00:04:06,260 --> 00:04:09,930
in your route files is your controller code.

79
00:04:09,930 --> 00:04:13,410
Though, at the moment, since we're not using models at all,

80
00:04:13,410 --> 00:04:17,200
we don't have correct controllers in our website

81
00:04:17,200 --> 00:04:19,470
because we're doing way too much work

82
00:04:19,470 --> 00:04:22,350
in those controllers at the moment.

83
00:04:22,350 --> 00:04:24,360
And therefore, that's the MVC pattern.

84
00:04:24,360 --> 00:04:26,260
And we're not using it yet.

85
00:04:26,260 --> 00:04:28,840
We're only using the view part.

86
00:04:28,840 --> 00:04:31,940
Therefore, in the next lectures, we're going to apply

87
00:04:31,940 --> 00:04:35,810
the MVC pattern on the website code we have here.

88
00:04:35,810 --> 00:04:37,470
And you will learn step by step

89
00:04:37,470 --> 00:04:40,670
how you implement it so that you can implement

90
00:04:40,670 --> 00:04:43,813
it in any website project you might be working on.

