1
00:00:00,150 --> 00:00:01,330
Welcome back.

2
00:00:01,380 --> 00:00:03,060
So this video is going to be pretty quick.

3
00:00:03,180 --> 00:00:07,440
I'm going to show you something called modules on exports which is going to help us clean up our code

4
00:00:07,530 --> 00:00:08,870
and make it more modular.

5
00:00:09,120 --> 00:00:13,650
And then I'll show you how we can use module but exports to clean up the code that we just wrote which

6
00:00:13,650 --> 00:00:15,640
is the reference is genius.

7
00:00:15,650 --> 00:00:17,260
File this one here.

8
00:00:17,310 --> 00:00:21,520
Currently it's a long file that has our mongoose code up top.

9
00:00:21,630 --> 00:00:27,210
Then we have the post schema and model and then the user schema and model and then all of our subsequent

10
00:00:27,210 --> 00:00:32,030
code where we're using those models what we're going to do is break this out into separate files.

11
00:00:32,100 --> 00:00:37,380
So the post code here all the stuff to make the schema in the model will be its own file that we can

12
00:00:37,380 --> 00:00:40,710
require just like we are requiring mongoose here.

13
00:00:40,830 --> 00:00:42,100
We can do the same thing.

14
00:00:42,120 --> 00:00:43,450
It won't work just yet.

15
00:00:43,450 --> 00:00:46,100
But we can do something like var post equals require.

16
00:00:46,260 --> 00:00:48,140
And then we'll have post-up.

17
00:00:48,210 --> 00:00:53,610
Yes that's not exactly the syntax but it will be a require statement and we'll do the same thing for

18
00:00:53,700 --> 00:00:55,740
the user.

19
00:00:55,740 --> 00:00:56,880
So why would we do this.

20
00:00:56,880 --> 00:00:58,080
There's two answers.

21
00:00:58,080 --> 00:01:01,720
First one is that it will help us clean up our code and shorten this.

22
00:01:01,950 --> 00:01:05,420
So we'll get rid of all of this and move it to separate files.

23
00:01:05,850 --> 00:01:08,490
But also it helps us make our code more modular.

24
00:01:08,760 --> 00:01:13,740
And if I wanted to have another application that use the same user model or another file that used it

25
00:01:14,040 --> 00:01:15,850
I wouldn't have to duplicate the code.

26
00:01:16,050 --> 00:01:18,810
I could just require that code in that file as well.

27
00:01:18,810 --> 00:01:24,900
So two main goals we will be using when I'm about to show you to also modularize are actually yes instead

28
00:01:24,900 --> 00:01:28,150
of the Yelp camp application this is getting pretty long.

29
00:01:28,410 --> 00:01:29,720
You have a bunch of routes.

30
00:01:30,000 --> 00:01:34,680
We have all the model code up here and this will continue to grow as we get more and more models.

31
00:01:34,680 --> 00:01:37,640
So after this video I'm going to show you how we can clean this up.

32
00:01:37,800 --> 00:01:41,060
But to start we're going to work with this references dodgiest file.

33
00:01:41,400 --> 00:01:46,640
And the first thing I'm going to do is move all of this code that relates to creating the model.

34
00:01:46,850 --> 00:01:52,170
The post model and I'll move it to a new file and I'm going to make a new directory first called models

35
00:01:52,620 --> 00:01:55,060
and the file will be models.

36
00:01:55,110 --> 00:01:56,660
Slash post.

37
00:01:56,750 --> 00:01:57,830
Jay Yes.

38
00:01:57,840 --> 00:02:03,470
And then while I'm here I'll also do models slash user dodginess just like that.

39
00:02:03,730 --> 00:02:04,030
OK.

40
00:02:04,050 --> 00:02:05,760
And then I'll open both of those up.

41
00:02:05,760 --> 00:02:09,660
Let's start with post models slash post.

42
00:02:09,750 --> 00:02:10,390
Yes.

43
00:02:10,710 --> 00:02:11,100
OK.

44
00:02:11,100 --> 00:02:12,710
And we can paste that code in.

45
00:02:13,110 --> 00:02:15,680
And the first thing you notice is that we now have an error.

46
00:02:15,720 --> 00:02:18,110
It's telling us that Mongoose is not defined.

47
00:02:18,570 --> 00:02:25,300
So we can start by defining mongoose and this is something that you just have to do if you're modularize

48
00:02:25,310 --> 00:02:27,880
in your code if you're splitting it up into small pieces.

49
00:02:28,020 --> 00:02:30,460
You're going to have to require things a lot more often.

50
00:02:30,660 --> 00:02:35,740
So we require mongoose and there's one major change you need to make which is that we need to use modules

51
00:02:35,740 --> 00:02:40,950
led experts down here which I like to think of as a return value for a file.

52
00:02:41,160 --> 00:02:47,070
So just like with the return value in a function nothing is returned unless we explicitly tell javascript

53
00:02:47,130 --> 00:02:50,010
what we want to return out of the function.

54
00:02:50,010 --> 00:02:55,440
Same thing with the file if we just include the file but we don't export anything out of it.

55
00:02:55,440 --> 00:02:59,140
We'll actually be including Nothing will be requiring just an empty file.

56
00:02:59,220 --> 00:03:04,170
What we need to do is export something and what we want to export is the model.

57
00:03:04,170 --> 00:03:11,230
So we're going to copy that and say modular but exports equals mongoose up model post.

58
00:03:11,760 --> 00:03:15,410
So we're declaring the schema and then we're declaring the model.

59
00:03:15,450 --> 00:03:17,250
And that's what we're sending out.

60
00:03:17,250 --> 00:03:22,720
We could also have done this and then said module about exports equals posts.

61
00:03:22,860 --> 00:03:23,950
Either one would work.

62
00:03:24,210 --> 00:03:26,680
But I'm going to do it just in one swoop here.

63
00:03:26,850 --> 00:03:30,520
So we'll get rid of this and add this back down here.

64
00:03:30,980 --> 00:03:35,470
OK so now let's see how we've required this file inside references.

65
00:03:35,550 --> 00:03:39,350
Yes I'm going to comment out pretty much everything else for now.

66
00:03:39,480 --> 00:03:44,790
Well keep the user schema there but I'm going to get rid of all this just so that we're not running

67
00:03:44,790 --> 00:03:45,540
anything.

68
00:03:45,780 --> 00:03:48,950
All I want to do is make sure that we can import this code.

69
00:03:49,200 --> 00:03:53,780
So it's going to be var post require.

70
00:03:54,240 --> 00:03:56,110
And the file is post-up.

71
00:03:56,150 --> 00:04:01,050
Yes we can just refer to it as post but it's inside the model directory.

72
00:04:01,050 --> 00:04:07,910
So models slash post and save and try running this file and see what happens.

73
00:04:07,920 --> 00:04:15,600
So node references dot J us and we get a small error and it tells us cannot find module models.

74
00:04:15,630 --> 00:04:16,770
Slash post.

75
00:04:16,770 --> 00:04:22,080
And that's because we're missing one key thing when we're referencing file paths and directories in

76
00:04:22,080 --> 00:04:27,990
node to reference the current directory We need a dot slash and that will reference where we currently

77
00:04:27,990 --> 00:04:30,520
are which is associations.

78
00:04:30,690 --> 00:04:35,850
And then we want to access the models directory and then the post file and site of that.

79
00:04:35,850 --> 00:04:37,770
Now let's clear the terminal.

80
00:04:37,770 --> 00:04:39,250
Try that again.

81
00:04:40,200 --> 00:04:42,150
This time it looks like it worked.

82
00:04:42,150 --> 00:04:44,520
We don't see anything which is a good sign.

83
00:04:44,520 --> 00:04:46,850
We shouldn't see anything with that code that we have here.

84
00:04:47,220 --> 00:04:47,690
OK.

85
00:04:47,790 --> 00:04:52,760
So let's close out of this and now will do the exact same thing for the user code.

86
00:04:52,860 --> 00:04:59,160
So we're going to take all of this cut it out and then open up the user J S file.

87
00:04:59,190 --> 00:05:05,370
So that's going to be C9 models slash user data.

88
00:05:05,430 --> 00:05:06,210
Yes.

89
00:05:06,330 --> 00:05:07,920
And not put this code in here.

90
00:05:08,130 --> 00:05:11,390
Once again we need to import mongoose.

91
00:05:11,760 --> 00:05:21,600
So our mongoose equals require mongoose and save that and then we're going to do a module that exports

92
00:05:22,020 --> 00:05:25,600
equals the mongoose top model that we're creating from user.

93
00:05:26,040 --> 00:05:32,670
So this will send out the model that when we require this file this is the one thing that we're returning

94
00:05:32,670 --> 00:05:34,950
out that we're exporting out to use.

95
00:05:34,950 --> 00:05:38,070
And this doesn't always have to be a single thing like this.

96
00:05:38,070 --> 00:05:39,870
We can return multiple pieces.

97
00:05:39,870 --> 00:05:43,590
We can make an object here and add a bunch of different bits of data.

98
00:05:43,890 --> 00:05:48,230
But this is an object remember mongoose top model generates an object for us.

99
00:05:48,480 --> 00:05:50,550
So that's what we're turning will save.

100
00:05:50,670 --> 00:05:59,910
Now go back to references and we're going to run var user he calls require and this should be dot slash

101
00:06:00,360 --> 00:06:08,110
models slash user and let's save and make sure both of those are requiring correctly.

102
00:06:08,340 --> 00:06:09,710
There we go.

103
00:06:09,780 --> 00:06:11,270
And now let's try using it.

104
00:06:11,550 --> 00:06:16,680
So what we're going to do is just uncomment some of this code will do this right here which is going

105
00:06:16,680 --> 00:06:22,210
to make us another post with how to cook the best burger and we'll do part four.

106
00:06:22,470 --> 00:06:23,430
And some jibberish.

107
00:06:23,430 --> 00:06:30,840
Let's change that up a little bit and then it's going to find that user with email Babuji com and everything

108
00:06:30,840 --> 00:06:35,040
else should be the same it's going to push in that post and canceled out logs and data.

109
00:06:35,310 --> 00:06:37,260
So if everything worked out just fine.

110
00:06:37,530 --> 00:06:42,270
This should work and we should see a user with some posts printed out.

111
00:06:42,270 --> 00:06:45,720
So let's give it a shot.

112
00:06:45,750 --> 00:06:46,620
There we go.

113
00:06:46,740 --> 00:06:53,080
We have our user Bob Belcher Bob at gmail dot com and then we have a bunch of post IDs inside the post

114
00:06:53,080 --> 00:06:55,360
Saray great.

115
00:06:55,380 --> 00:07:01,060
So let's recap everything Monserrat exports allows us to break things into files.

116
00:07:01,080 --> 00:07:05,030
And the reason we'd want to do that is to clean up our code first and foremost.

117
00:07:05,070 --> 00:07:10,980
This makes it a lot cleaner appear but it also makes things modular and makes it reusable so I can have

118
00:07:10,980 --> 00:07:16,620
another file where I require the post or the user model and I don't have to duplicate any code.

119
00:07:16,620 --> 00:07:22,050
Aside from the single line in the next video I'm going to show you how we can clean up camp using the

120
00:07:22,050 --> 00:07:23,470
exact same concepts.
