1
00:00:00,360 --> 00:00:00,690
All right.

2
00:00:00,690 --> 00:00:02,110
Welcome back.

3
00:00:02,120 --> 00:00:07,050
So in this video we're going to address the problem that I mentioned in the last video which is that

4
00:00:07,320 --> 00:00:14,670
right now we're using one database between our local cloud nine development environment and our production

5
00:00:14,670 --> 00:00:16,410
environment on Heroku.

6
00:00:16,650 --> 00:00:25,260
So our deployed code is using the same database that our testing ground is which is really not a great

7
00:00:25,260 --> 00:00:26,480
idea.

8
00:00:26,490 --> 00:00:32,820
Things work fine it's not a problem in the sense that our code is broken or that we can't get by using

9
00:00:32,820 --> 00:00:40,410
this way but it's really not a good practice because we don't want to have one database where you know

10
00:00:40,470 --> 00:00:45,950
if we have a big application our main database with all of our users that's deployed.

11
00:00:46,020 --> 00:00:50,640
We don't want to be testing things and screwing around and adding new features accidentally deleting

12
00:00:51,030 --> 00:00:53,830
entries in the database or deleting the entire database.

13
00:00:53,940 --> 00:00:56,220
In our development environment.

14
00:00:56,340 --> 00:00:58,210
So you want to keep those separate.

15
00:00:58,590 --> 00:01:01,760
And just to demonstrate right now what we have going on.

16
00:01:01,860 --> 00:01:08,700
I have remember that what we did was we updated our code rather than using the local hosts.

17
00:01:08,730 --> 00:01:15,660
You can't feed 10 or whatever database you or you had there but the local version on cloud 9 we updated

18
00:01:15,660 --> 00:01:18,050
it to be Mangu lab.

19
00:01:18,300 --> 00:01:23,970
And this is our Mongo lab instance and it's the same one we have this running locally right now.

20
00:01:24,120 --> 00:01:27,900
And on Heroku so I'll just refresh the page on both.

21
00:01:27,930 --> 00:01:35,490
This is Heroku deploy as you can see we have one rodeo beach and we have rodeo beach again just to prove

22
00:01:35,520 --> 00:01:37,290
that they are connected of course.

23
00:01:37,290 --> 00:01:40,920
Let's add a new campground over here.

24
00:01:42,240 --> 00:01:46,200
The name for this one will be Kirby cove.

25
00:01:47,730 --> 00:01:53,930
I'll just use this image here which is one that I took a few nights ago totally shameless plug.

26
00:01:54,420 --> 00:01:55,870
Nothing you can do about it.

27
00:01:55,980 --> 00:02:03,750
So I'm going to copy that paste that here and just put blah blah blah auto complete.

28
00:02:03,900 --> 00:02:04,470
Thank you.

29
00:02:04,470 --> 00:02:07,920
Chrome click submit.

30
00:02:07,920 --> 00:02:10,870
All right so we have this here on Heroku.

31
00:02:11,010 --> 00:02:13,000
And I refresh.

32
00:02:14,280 --> 00:02:18,400
I have the same Kirby code created here locally.

33
00:02:18,780 --> 00:02:22,330
So it's not even about catastrophic events.

34
00:02:22,470 --> 00:02:25,950
When a developer could accidentally delete the entire database.

35
00:02:26,190 --> 00:02:27,000
Let's see that.

36
00:02:27,090 --> 00:02:32,310
Let's say I wanted to test a feature I wanted to know is delete working on my delete route's working

37
00:02:32,320 --> 00:02:32,870
.

38
00:02:33,420 --> 00:02:39,770
Well if I go in here and if I deleted something which I'm not signed in but if I did delete this I'm

39
00:02:39,780 --> 00:02:44,760
also deleting it from production which means that I'm deleting someone's actual data which is never

40
00:02:44,760 --> 00:02:45,400
good.

41
00:02:45,720 --> 00:02:49,000
So it's really a good idea to have separate databases.

42
00:02:49,050 --> 00:02:56,420
So one way of fixing this problem is just to go back to cloud 9 and just on cloud nine switch.

43
00:02:56,880 --> 00:03:05,100
Well we're using here so that we're now using the camp localhost version again to save restart the server

44
00:03:05,100 --> 00:03:11,190
here and now on cloud 9 5 refresh.

45
00:03:11,190 --> 00:03:16,860
You'll see that we're using the old version or what we were using before the local version and on Heroku

46
00:03:17,490 --> 00:03:22,510
we're still using the Monga lab instance because right now they're out of sync.

47
00:03:22,620 --> 00:03:26,460
We have changes that we made locally on cloud 9 that we haven't pushed to her OK.

48
00:03:26,790 --> 00:03:28,620
And this is fine.

49
00:03:28,670 --> 00:03:35,370
This will work it's valid but it's kind of annoying because if I do want to let's say added a new feature

50
00:03:35,670 --> 00:03:39,040
I change the C S S and I want to update that on Heroku.

51
00:03:39,270 --> 00:03:44,470
Well then I'm going to have to figure out what I'm going to do about our database you know.

52
00:03:45,120 --> 00:03:52,020
I'm going to have to comment this out comment that or recommit on uncomment that one save and command

53
00:03:52,020 --> 00:03:57,480
all of that push it to Heroku And then if I wanted to add a new feature locally again or test something

54
00:03:57,480 --> 00:03:58,030
out.

55
00:03:58,150 --> 00:04:03,650
I comment that and uncomment this and it's just it's really not a good solution at all.

56
00:04:04,230 --> 00:04:07,960
However there is another solution or another approach that we can take.

57
00:04:08,400 --> 00:04:17,380
And basically what we're going to do is write some code that says if we're in production run or set

58
00:04:17,400 --> 00:04:20,430
the database you are out to this.

59
00:04:20,430 --> 00:04:23,690
Otherwise set it to be this here.

60
00:04:24,210 --> 00:04:26,570
Now it's not actually going to be an if statement.

61
00:04:26,760 --> 00:04:31,990
And we're not technically checking if we're in production if we're in or if we're on Heroku.

62
00:04:32,190 --> 00:04:33,620
It's a little bit different.

63
00:04:33,900 --> 00:04:37,300
What we're going to do is make use of something called environment variables.

64
00:04:37,620 --> 00:04:39,940
So we've seen them before.

65
00:04:40,470 --> 00:04:47,530
This little line right here which reviews and every Express app process does envy port process that

66
00:04:47,540 --> 00:04:49,530
EMV IP.

67
00:04:49,590 --> 00:04:52,550
So what this means right here what this.

68
00:04:52,560 --> 00:04:56,640
And I went over this briefly but I think you'll make a little more sense now that you have some more

69
00:04:56,640 --> 00:04:57,680
experience under your belt.

70
00:04:57,750 --> 00:05:02,990
What this refers to is the environment where this code is being run.

71
00:05:04,010 --> 00:05:11,770
So process that you envied up port on cloud nine may maybe different than process that dot port on her

72
00:05:11,800 --> 00:05:15,910
Roku probably is IP definitely is.

73
00:05:16,100 --> 00:05:23,660
So these are ways of adding variables that are not hard coded things that change based off of the environment

74
00:05:23,660 --> 00:05:24,900
they're being run in.

75
00:05:24,950 --> 00:05:29,590
So we could have put something like 80 80 here.

76
00:05:29,840 --> 00:05:31,700
But of course that's hardcoded.

77
00:05:31,820 --> 00:05:33,010
That's not dynamic.

78
00:05:33,020 --> 00:05:39,980
This allows us to have variables that respond to the environment and in node in javascript will actually

79
00:05:39,980 --> 00:05:41,180
just a node.

80
00:05:41,360 --> 00:05:45,050
They always start with process D-NV that's how you access them.

81
00:05:45,050 --> 00:05:47,600
And then the name of the variable.

82
00:05:48,110 --> 00:05:55,910
So what we're going to do here is have an environment variable that will call database Yoro and on Heroku

83
00:05:55,910 --> 00:06:05,150
we're going to set database you are able to be this long thing here and then all will have to do is

84
00:06:05,570 --> 00:06:07,280
basically write process

85
00:06:09,840 --> 00:06:13,340
in the database you are.

86
00:06:13,850 --> 00:06:19,120
And that will refer to the correct you URL for our given environment.

87
00:06:20,030 --> 00:06:21,670
And then we'll do the same thing here.

88
00:06:21,680 --> 00:06:30,560
We're well set on cloud nine database you Arel to be Mongar D-B colon slash us localhost she can Campfield

89
00:06:30,570 --> 00:06:31,670
10.

90
00:06:31,670 --> 00:06:36,630
So again the idea is that we can create an environment variable which I haven't shown you how to do

91
00:06:36,630 --> 00:06:36,650
.

92
00:06:36,650 --> 00:06:38,630
This is just how you access them.

93
00:06:38,840 --> 00:06:42,950
But remember this is coming from outside of this application.

94
00:06:42,950 --> 00:06:47,810
This is coming from the actual application or the actual environment the server that we're running the

95
00:06:47,810 --> 00:06:49,060
code on.

96
00:06:49,820 --> 00:06:56,720
So to demonstrate how we make this work I'm going to start by going over here closing out and I'm going

97
00:06:56,720 --> 00:07:03,830
to create a new environment variable in the command line in the environment when a type export is a

98
00:07:03,830 --> 00:07:09,500
new command and then I'm going to write my environment variable which usually the convention has to

99
00:07:09,500 --> 00:07:10,870
do in all caps.

100
00:07:10,880 --> 00:07:13,450
As you can see here IP port.

101
00:07:13,590 --> 00:07:16,420
So we'll do data base.

102
00:07:16,970 --> 00:07:27,800
You are all equals and we're going to start resetting it equal to what we want to run on our cloud nine

103
00:07:27,800 --> 00:07:31,680
instance what we want the database you are all to be equal to.

104
00:07:32,240 --> 00:07:34,250
Just like that and hit enter

105
00:07:37,310 --> 00:07:39,860
to check if that were.

106
00:07:41,020 --> 00:07:46,000
Going to do this right here actually right here.

107
00:07:46,130 --> 00:07:49,170
Cancel that log process.

108
00:07:49,450 --> 00:07:50,260
Daddy envy.

109
00:07:50,390 --> 00:07:54,960
And this needs to match data base you know.

110
00:07:56,180 --> 00:08:03,460
And if we see this Mongar D-B Colon's blah blah blah local host of Canvey 10 spit back out or printed

111
00:08:03,500 --> 00:08:09,170
out then we know that we've successfully set up the development database you know.

112
00:08:09,860 --> 00:08:13,110
So let's see what happens node.

113
00:08:13,190 --> 00:08:17,600
Yes there we go see that it was printed out here.

114
00:08:17,690 --> 00:08:19,790
I never said print out localhost.

115
00:08:19,800 --> 00:08:20,690
You can pretend.

116
00:08:20,690 --> 00:08:24,670
I said the environment variable database you are up.

117
00:08:25,080 --> 00:08:31,440
So now all we need to do is say Mangeshkar connect to process NVI

118
00:08:33,860 --> 00:08:34,940
database.

119
00:08:35,010 --> 00:08:45,410
You IRA and then on Heroku we need to go update database you know to be this Mongo lab giant You are

120
00:08:45,410 --> 00:08:48,760
all string but we only want that to happen on Heroku.

121
00:08:48,860 --> 00:08:52,320
We want to leave this alone on cloud nine.

122
00:08:52,520 --> 00:08:56,770
So let's just make sure it works so vocalise a restart.

123
00:08:56,990 --> 00:08:57,760
Great.

124
00:08:58,220 --> 00:09:04,850
Let's go to our correct version here refresh and we're still getting the local code Heroku of course

125
00:09:04,850 --> 00:09:08,070
is unchanged because we haven't pushed any code to Heroku.

126
00:09:08,660 --> 00:09:13,670
So now if we save this and add and commit

127
00:09:18,050 --> 00:09:20,570
say update the you IRL

128
00:09:23,570 --> 00:09:27,170
get push Heroku M..

129
00:09:29,150 --> 00:09:35,510
If we try and run the Heroku version right now if I as soon as this finishes at least if I try and refresh

130
00:09:35,510 --> 00:09:41,930
this page I'm going to have an error because Heroku are the servers that would be able to find process

131
00:09:41,930 --> 00:09:46,120
study and vidette database you know because it's not set up.

132
00:09:46,550 --> 00:09:56,930
So just to show you that refresh might take a bit and they should get application error and while that's

133
00:09:56,930 --> 00:09:58,820
going out there we go.

134
00:09:58,850 --> 00:10:03,060
Application error again if you really want to know exactly what's happening.

135
00:10:03,070 --> 00:10:04,250
It's Heroku logs

136
00:10:06,800 --> 00:10:13,140
and you can see here I don't know how useful the error message that we get will be honestly.

137
00:10:13,250 --> 00:10:15,450
Let's see.

138
00:10:15,470 --> 00:10:21,770
This might be one of those messages that doesn't tell you exactly what you had hoped but it's telling

139
00:10:21,770 --> 00:10:24,560
us Mongar D-B there's an error.

140
00:10:26,820 --> 00:10:34,080
Yesso doesn't it doesn't explicitly mention that this database you are is a problem because what's actually

141
00:10:34,090 --> 00:10:39,580
happening is that it's trying to connect to an empty string most likely which technically isn't a problem

142
00:10:40,020 --> 00:10:45,310
except for the fact that there is no database located at the you are an empty string.

143
00:10:45,490 --> 00:10:49,020
So it's sort of a unexpected error message.

144
00:10:49,600 --> 00:10:55,990
So I am telling you though that the problem is that we haven't set up a process to or that we haven't

145
00:10:55,990 --> 00:10:58,770
set up the environment variable on Heroku.

146
00:10:59,530 --> 00:11:05,070
So to do that there's a few different ways we can do it if we go to Heroku itself.

147
00:11:05,200 --> 00:11:09,640
I'll probably need to log back in here I refresh the page.

148
00:11:10,750 --> 00:11:12,170
Nope I didn't need to lie back in.

149
00:11:12,220 --> 00:11:13,910
Here's our app.

150
00:11:14,010 --> 00:11:22,530
Well if we go in and we go into settings I believe it is.

151
00:11:23,500 --> 00:11:30,420
And then keep scrolling down or go to config variables.

152
00:11:31,140 --> 00:11:33,230
We can add in a variable here.

153
00:11:33,460 --> 00:11:39,170
So we want database you Earl to be that giant string.

154
00:11:39,190 --> 00:11:43,880
I copied it and click add.

155
00:11:45,510 --> 00:11:48,930
And if we go back now try refreshing the page.

156
00:11:49,540 --> 00:11:52,230
It works perfectly OK.

157
00:11:52,260 --> 00:11:53,920
So that's one way of doing it.

158
00:11:53,980 --> 00:12:01,420
I showed you how to go on to Heroku and add in a key value pair database you Erlass and the key or the

159
00:12:01,420 --> 00:12:05,050
name of the variable the environment variable and then a value.

160
00:12:05,400 --> 00:12:11,170
That's one way of doing it the other way of doing it is through the command line you can run a command

161
00:12:11,170 --> 00:12:11,930
.

162
00:12:12,150 --> 00:12:13,330
We can do this right here.

163
00:12:13,360 --> 00:12:14,760
I have the No.

164
00:12:14,760 --> 00:12:24,760
J.S. support docs open on Heroku this line Heroku config colon set and then key equals value.

165
00:12:25,420 --> 00:12:29,720
So I'll just copy this first bit if we want to I'm not going to actually do that.

166
00:12:29,730 --> 00:12:36,340
The I'm not going to hit enter but I just want to show you it would be Heroku config Kolin set and then

167
00:12:36,340 --> 00:12:42,210
database you are l equals and then we had paste that you are up.

168
00:12:42,940 --> 00:12:44,710
But am I going to do that.

169
00:12:44,710 --> 00:12:50,200
So another thing I haven't mentioned yet is that there's another good reason for using environment variables

170
00:12:50,350 --> 00:12:58,270
which is that there are hidden there they're not exposed to whatever it is developers if you put this

171
00:12:58,270 --> 00:12:59,550
code up on good hub.

172
00:12:59,710 --> 00:13:05,920
We don't want to have our database you rl right here of course or if you already know no my database

173
00:13:05,910 --> 00:13:06,340
you are.

174
00:13:06,360 --> 00:13:09,370
But this isn't obviously a real app.

175
00:13:09,490 --> 00:13:16,870
But if this was a real application even if it's an open source application or parts of my code or open

176
00:13:16,870 --> 00:13:23,320
source I don't want to be exposing my data or my database you are out to everybody who looks at the

177
00:13:23,320 --> 00:13:24,110
code.

178
00:13:24,580 --> 00:13:32,230
So by having this line here Mongar start connect process that the NV dot database you arow that's no

179
00:13:32,230 --> 00:13:33,250
longer an issue.

180
00:13:33,790 --> 00:13:40,930
So that this is all that a developer would see or anyone browsing the code on get home and they would

181
00:13:40,930 --> 00:13:43,230
have to set up their own database.

182
00:13:43,240 --> 00:13:46,490
You are all in using environment variables.

183
00:13:47,050 --> 00:13:47,770
OK.

184
00:13:48,190 --> 00:13:49,850
So let's recap what we did.

185
00:13:49,990 --> 00:13:56,890
We started by having the issue the problem of one database for development and production a shared database

186
00:13:57,700 --> 00:13:58,870
to fix that.

187
00:13:58,890 --> 00:14:07,300
We set up our database URL environment variables locally on cloud nine and on two different values for

188
00:14:07,300 --> 00:14:08,910
the same variable.

189
00:14:09,190 --> 00:14:15,300
And then we accessed that variable the environment variable using process that ENVI database you IRL

190
00:14:16,210 --> 00:14:17,360
and we connect to that.

191
00:14:17,380 --> 00:14:19,120
So we have two outcomes.

192
00:14:19,480 --> 00:14:25,050
One thing that I will mention is that it is worth setting up a default value.

193
00:14:25,500 --> 00:14:31,640
So let's say that I somehow screwed my app up if I restart my computer.

194
00:14:31,650 --> 00:14:38,590
I don't know I reset this workspace in my environment variable is gone or I'm sending this to someone

195
00:14:38,620 --> 00:14:44,940
and I want them to have I don't know a default database that they could use just to make it so that

196
00:14:45,220 --> 00:14:49,790
the code doesn't completely break if I haven't explicitly set this up.

197
00:14:49,890 --> 00:14:55,480
What I can do is add in a little backup so we'll make a variable and we'll just call it.

198
00:14:55,650 --> 00:15:03,710
You are all equals and we're going to do process started in the database you are real or.

199
00:15:04,690 --> 00:15:14,550
And then we'll put some you are out there so let's do what was are you are ELLYARD already lost it.

200
00:15:15,230 --> 00:15:16,850
There you are.

201
00:15:17,830 --> 00:15:20,770
So this will just be a backup.

202
00:15:20,800 --> 00:15:28,660
So again what's happening here is it's not mandatory doesn't change the functionality but if for some

203
00:15:28,650 --> 00:15:33,220
reason my environment variable got screwed up or it didn't exist.

204
00:15:33,220 --> 00:15:39,100
Rather than breaking the code what will happen is that this you are all variable it's going to be equal

205
00:15:39,100 --> 00:15:42,620
to the environment variable if it exists.

206
00:15:43,300 --> 00:15:49,270
If it's an empty string though or if it doesn't exist then it's going to be equal to this which is just

207
00:15:49,270 --> 00:15:50,410
our backup.

208
00:15:50,400 --> 00:15:54,470
In this case on cloud nine there are the exact same thing.

209
00:15:54,610 --> 00:15:56,000
It's really not mandatory again.

210
00:15:56,020 --> 00:16:00,410
It's just kind of a good practice to have in case something went wrong.

211
00:16:00,790 --> 00:16:04,600
But really I'm just showing it to you because it's pretty common to see this as well.

212
00:16:04,950 --> 00:16:05,550
OK.

213
00:16:05,800 --> 00:16:12,150
So kind of long winded or no circuitous way of approaching this but it all boils down to the fact that

214
00:16:12,370 --> 00:16:19,170
we want different databases process that in vidette database you IRL is how we access environment variables

215
00:16:19,170 --> 00:16:20,530
from a node app.

216
00:16:20,660 --> 00:16:26,770
You want to make it clear though environment variables are not a node thing or a javascript thing.

217
00:16:26,770 --> 00:16:33,580
Ruby applications rails applications Python apps they all can access environment variables and in all

218
00:16:33,660 --> 00:16:39,760
they look relatively simple similar to this it just happens that in node you write process in vi in

219
00:16:39,760 --> 00:16:41,850
other languages like Ruby.

220
00:16:41,880 --> 00:16:47,040
You'll often see something like ENV database you know.

221
00:16:47,710 --> 00:16:54,850
So it is a universal concept it's not a node specific thing and it's universal across all sorts of applications

222
00:16:54,850 --> 00:16:55,930
as well.

223
00:16:55,920 --> 00:17:01,200
You don't just use this for database URLs use it anytime you you want to.

224
00:17:01,310 --> 00:17:03,770
It looks like a typo here.

225
00:17:03,790 --> 00:17:06,730
Either way it should be localhost.

226
00:17:06,820 --> 00:17:14,290
Use this any time you want to have different information between your different environments development

227
00:17:14,290 --> 00:17:15,380
or production.

228
00:17:15,390 --> 00:17:22,300
You also use this when you want to hide or obscure information from someone and get help for instance

229
00:17:22,300 --> 00:17:22,530
.

230
00:17:22,540 --> 00:17:31,510
So if you had your paypal or your Strype credit card processing API information inside of your application

231
00:17:31,510 --> 00:17:33,350
because you needed it for the code.

232
00:17:33,570 --> 00:17:37,700
Well if you want to share your current get how you obviously don't want to be pushing that up.

233
00:17:37,810 --> 00:17:43,760
So what you can do is create an environment variable and do something like process.

234
00:17:43,820 --> 00:17:53,740
Ian VI died super sensitive information and that's a way to get around that problem of wanting to share

235
00:17:53,740 --> 00:18:00,100
your code make it public but also not wanting to share your super sensitive information where someone

236
00:18:00,100 --> 00:18:06,090
could take your your API key and run up a bill of thousands and thousands of dollars which actually

237
00:18:06,100 --> 00:18:11,050
happens some of the bigger companies are much better about policing that.

238
00:18:11,050 --> 00:18:19,980
So Amazon for instance if you have your Amazon API key or your AWOS information your Amazon Web Services

239
00:18:20,200 --> 00:18:26,140
web services information on get hobe they actually will know and they'll send you a message saying you

240
00:18:26,130 --> 00:18:28,960
need to take this down immediately or are going to disable it.

241
00:18:28,990 --> 00:18:32,170
Someone could charge thousands and thousands of dollars to this.

242
00:18:32,290 --> 00:18:38,110
They could use you or basically your account to run off a huge bill that they don't have to pay.

243
00:18:38,430 --> 00:18:40,810
Happens relatively often.

244
00:18:40,810 --> 00:18:46,550
I've heard of some students having that problem and of mine and promise but definitely I've heard of

245
00:18:46,600 --> 00:18:48,640
that before.

246
00:18:48,630 --> 00:18:49,270
All right.

247
00:18:49,270 --> 00:18:52,300
So process the envy is how we access environment variables.

248
00:18:52,330 --> 00:18:58,000
We have two databases set up camp has now deployed two different databases.

249
00:18:57,990 --> 00:19:01,290
They don't share any information which is how we want it.

250
00:19:01,620 --> 00:19:01,930
Awesome
