1
00:00:02,290 --> 00:00:04,540
Now, refactoring the routes

2
00:00:04,540 --> 00:00:06,800
is only a start though.

3
00:00:06,800 --> 00:00:08,980
There's more we can refactor.

4
00:00:08,980 --> 00:00:12,890
And there is more we can refactor in those route files,

5
00:00:12,890 --> 00:00:14,180
but before we do that,

6
00:00:14,180 --> 00:00:17,440
I'll take a look at the app.js file.

7
00:00:17,440 --> 00:00:19,820
This file is definitely not bad.

8
00:00:19,820 --> 00:00:23,920
It's pretty lean, but there are a couple of things

9
00:00:23,920 --> 00:00:25,740
we could extract.

10
00:00:25,740 --> 00:00:29,940
For example, you could extract configuration settings

11
00:00:29,940 --> 00:00:32,890
like this object for the session

12
00:00:32,890 --> 00:00:35,560
into a separate config.file.

13
00:00:35,560 --> 00:00:37,410
It's not a must do.

14
00:00:37,410 --> 00:00:40,990
None of the things mentioned here are must dos.

15
00:00:40,990 --> 00:00:43,280
But it is something you could do,

16
00:00:43,280 --> 00:00:47,470
if you find yourself having a lot of configuration objects

17
00:00:47,470 --> 00:00:50,860
in that file, and you wanna split that up.

18
00:00:50,860 --> 00:00:53,540
And therefore, to show this to you, I will do it here.

19
00:00:53,540 --> 00:00:56,250
But again, keep in mind, it's optional.

20
00:00:56,250 --> 00:00:58,800
Splitting the routes was also optional,

21
00:00:58,800 --> 00:01:00,790
but strongly recommended.

22
00:01:00,790 --> 00:01:03,860
Extracting configuration objects is optional

23
00:01:03,860 --> 00:01:06,370
and not necessarily recommended.

24
00:01:06,370 --> 00:01:08,701
It's totally up to you.

25
00:01:08,701 --> 00:01:11,890
So we'll add a new folder and I'll name it config.

26
00:01:11,890 --> 00:01:14,100
Though, the name is up to you.

27
00:01:14,100 --> 00:01:16,900
This is no reserved name or anything like that.

28
00:01:16,900 --> 00:01:19,610
You can name that folder whatever you want.

29
00:01:19,610 --> 00:01:22,769
And here in this config folder,

30
00:01:22,769 --> 00:01:27,769
I will add a session.js file like this.

31
00:01:27,820 --> 00:01:30,100
And in there I'll now register

32
00:01:30,100 --> 00:01:35,010
all these session config objects that I use in app.js.

33
00:01:35,010 --> 00:01:37,660
Like this object for configuring the MongoDBStore

34
00:01:38,900 --> 00:01:42,343
and then this object for configuring the session itself.

35
00:01:43,560 --> 00:01:46,520
And actually, I'll take this entire sessionStore

36
00:01:46,520 --> 00:01:51,140
constant here from app.js and move that into session.js

37
00:01:51,140 --> 00:01:52,053
like this.

38
00:01:53,490 --> 00:01:57,260
And therefore, I'll also grab the MongoDBStore here

39
00:01:58,700 --> 00:02:03,110
and cut that from app.js and bring that into session.js

40
00:02:03,110 --> 00:02:06,343
because we're now using that MongoDBStore there.

41
00:02:07,190 --> 00:02:10,446
Now, I need that MongoDBStore function here.

42
00:02:10,446 --> 00:02:13,882
And for this in app.js, I'll extract

43
00:02:13,882 --> 00:02:18,784
this require statement here, where I require MongoDBStore.

44
00:02:18,784 --> 00:02:22,563
And I will cut that, and bring that here

45
00:02:22,563 --> 00:02:24,563
into session.js as well.

46
00:02:25,720 --> 00:02:29,943
And I will now wrap this sessionStore creation

47
00:02:29,943 --> 00:02:31,276
into a function,

48
00:02:32,284 --> 00:02:36,540
createSessionStore would be a fitting name

49
00:02:36,540 --> 00:02:40,180
and I'll move that into this function.

50
00:02:40,180 --> 00:02:44,230
I will also move this line here into this function

51
00:02:44,230 --> 00:02:45,730
because this is not an import.

52
00:02:45,730 --> 00:02:48,560
I'm just creating the MongoDBStore object.

53
00:02:48,560 --> 00:02:51,970
And since this needs that session middleware,

54
00:02:51,970 --> 00:02:53,170
which we'll be using,

55
00:02:53,170 --> 00:02:56,750
I will simply accept this here as a parameter value

56
00:02:56,750 --> 00:02:58,263
in my custom function.

57
00:02:59,183 --> 00:03:02,290
And I will then export this function

58
00:03:02,290 --> 00:03:04,803
with module.exports as you learned it.

59
00:03:05,940 --> 00:03:08,620
So I will export this function here.

60
00:03:08,620 --> 00:03:10,520
I'll name it createSessionStore

61
00:03:10,520 --> 00:03:12,886
and point at this createSessionStore function

62
00:03:12,886 --> 00:03:15,863
I just created like this.

63
00:03:17,600 --> 00:03:19,196
And now back in app.js,

64
00:03:19,196 --> 00:03:21,960
we can import from this config.file,

65
00:03:21,960 --> 00:03:24,195
call this function and pass the session to it,

66
00:03:24,195 --> 00:03:26,730
to have this logic outsourced.

67
00:03:26,730 --> 00:03:30,110
In this function, I now just also want to return

68
00:03:30,110 --> 00:03:32,940
my sessionStore here, which I'm creating.

69
00:03:32,940 --> 00:03:35,856
So that the logic for creating it is outsourced

70
00:03:35,856 --> 00:03:37,810
and these configuration items

71
00:03:37,810 --> 00:03:41,510
are, but I can still get my session store in the place

72
00:03:41,510 --> 00:03:44,450
where I need it, which is the app.js file.

73
00:03:44,450 --> 00:03:47,790
So here I'll get my sessionConfig now,

74
00:03:47,790 --> 00:03:52,790
let's say in app.js by requiring ./config/session.

75
00:03:54,380 --> 00:03:56,980
So now I'm requiring this object,

76
00:03:56,980 --> 00:04:00,660
which I'm exporting here in the session.js file.

77
00:04:00,660 --> 00:04:03,690
And in there, I'll have the createSessionStore function,

78
00:04:03,690 --> 00:04:05,240
which I can call.

79
00:04:05,240 --> 00:04:09,040
So therefore, I can now get my mongoDBSessionStore

80
00:04:10,620 --> 00:04:14,559
by executing sessionConfig.createSessionStore.

81
00:04:14,559 --> 00:04:18,000
And to this function, I need to pass session,

82
00:04:18,000 --> 00:04:20,320
this middleware, which I'm importing here

83
00:04:20,320 --> 00:04:22,793
because that is used by that function.

84
00:04:25,620 --> 00:04:28,700
And now I'll just also grab this

85
00:04:30,870 --> 00:04:35,090
session creation here or just that object.

86
00:04:35,090 --> 00:04:37,940
It is up to you what you wanna do.

87
00:04:37,940 --> 00:04:39,570
I'll grab just the object here

88
00:04:39,570 --> 00:04:41,970
to show you that you can do both.

89
00:04:41,970 --> 00:04:45,725
And I'll add this here in session.js as well.

90
00:04:45,725 --> 00:04:48,061
So here I'll add another function,

91
00:04:48,061 --> 00:04:52,183
createSession or a createSessionConfig.

92
00:04:53,370 --> 00:04:57,440
And there, I will simply return this object,

93
00:04:57,440 --> 00:04:59,410
which I just cut.

94
00:04:59,410 --> 00:05:02,950
There, I need my sessionStore, so I'll except this here

95
00:05:02,950 --> 00:05:04,553
as a parameter as well.

96
00:05:05,640 --> 00:05:07,756
And then I'll export this unction

97
00:05:07,756 --> 00:05:10,637
just as I'm exporting createSessionStore.

98
00:05:10,637 --> 00:05:14,830
So this createSessionConfig function

99
00:05:14,830 --> 00:05:17,043
will be exported as well.

100
00:05:18,140 --> 00:05:21,730
And now in app.js, we can also call that.

101
00:05:21,730 --> 00:05:24,010
So now here, where I need the config,

102
00:05:24,010 --> 00:05:26,490
I can use my sessionConfig

103
00:05:26,490 --> 00:05:31,460
and call createSessionConfig and pass my mongoDBSessionStore

104
00:05:31,460 --> 00:05:35,275
as an argument, as a parameter value to this function.

105
00:05:35,275 --> 00:05:38,790
So that's this mongoDBSessionsStore I created before

106
00:05:38,790 --> 00:05:40,435
with that other function that lives

107
00:05:40,435 --> 00:05:42,513
in the sessionConfig file.

108
00:05:44,090 --> 00:05:47,100
And again, I will emphasize, this is optional

109
00:05:47,100 --> 00:05:50,640
and this is all just one way of implementing this.

110
00:05:50,640 --> 00:05:54,079
We could do the entire session setup into session.js file

111
00:05:54,079 --> 00:05:57,932
and directly use the store which we are creating here

112
00:05:57,932 --> 00:06:01,100
in the config and then just return

113
00:06:01,100 --> 00:06:03,221
the initialized session middleware,

114
00:06:03,221 --> 00:06:05,850
so you can do whatever you want.

115
00:06:05,850 --> 00:06:08,250
This is just one possible approach

116
00:06:08,250 --> 00:06:10,433
to extract some parts that are involved

117
00:06:10,433 --> 00:06:14,670
with this configuration from the app.js file

118
00:06:14,670 --> 00:06:16,840
into a separate file.

119
00:06:16,840 --> 00:06:18,816
And we could do this for the configuration

120
00:06:18,816 --> 00:06:22,723
for other middleware as well, but I'll keep it like this.

121
00:06:22,723 --> 00:06:25,600
I'll just focus on the session at the moment

122
00:06:25,600 --> 00:06:29,183
and just saved this and see whether that still works.

123
00:06:30,100 --> 00:06:33,360
If I'm quickly logging in here,

124
00:06:33,360 --> 00:06:36,700
yeah, I can still move around here.

125
00:06:36,700 --> 00:06:39,680
So that all seems to work.

126
00:06:39,680 --> 00:06:42,410
Let's see if flashing error message

127
00:06:42,410 --> 00:06:45,010
has onto the session also still works

128
00:06:45,010 --> 00:06:49,113
if I try to create a user that already exists, yes it does.

129
00:06:50,635 --> 00:06:54,300
So that's how we could extract configuration

130
00:06:54,300 --> 00:06:56,053
if we want it to do that.

