Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,140 --> 00:00:02,520
Welcome back.
2
00:00:02,520 --> 00:00:04,540
In this video you will learn all about
3
00:00:04,540 --> 00:00:06,200
environment variables.
4
00:00:06,200 --> 00:00:10,220
So what they are, how we set them, and how we use them.
5
00:00:10,220 --> 00:00:13,190
Now this is not exactly about Express,
6
00:00:13,190 --> 00:00:16,700
it really has to do with node JS development in general,
7
00:00:16,700 --> 00:00:18,710
but we really need this in order to move on
8
00:00:18,710 --> 00:00:21,400
in the project and so this is a good time
9
00:00:21,400 --> 00:00:22,833
to talk about this.
10
00:00:24,440 --> 00:00:27,170
So node JS, or Express apps,
11
00:00:27,170 --> 00:00:29,270
can run in different environments.
12
00:00:29,270 --> 00:00:32,659
And the most important ones are the development environment
13
00:00:32,659 --> 00:00:34,440
and the production environment.
14
00:00:34,440 --> 00:00:37,180
That's because depending on the environment,
15
00:00:37,180 --> 00:00:39,690
we might use different databases for example,
16
00:00:39,690 --> 00:00:42,260
or we might turn login on or off,
17
00:00:42,260 --> 00:00:44,730
or we might turn debugging on or off,
18
00:00:44,730 --> 00:00:47,410
or really all kinds of different settings that might
19
00:00:47,410 --> 00:00:50,450
change depending on the development that we're in.
20
00:00:50,450 --> 00:00:53,360
So again the most important ones are the development
21
00:00:53,360 --> 00:00:55,240
and the production environment.
22
00:00:55,240 --> 00:00:56,890
But there are other environments
23
00:00:56,890 --> 00:00:58,980
that bigger teams might use.
24
00:00:58,980 --> 00:01:01,190
So this type of setting that I just mentioned,
25
00:01:01,190 --> 00:01:04,569
like different databases or login turned on or off,
26
00:01:04,569 --> 00:01:07,890
that will be based on environment variables.
27
00:01:07,890 --> 00:01:12,020
Now by default, Express sets the environment to development
28
00:01:12,020 --> 00:01:15,360
which makes a lot of sense because that's what we're doing
29
00:01:15,360 --> 00:01:16,943
when we start a new project.
30
00:01:18,110 --> 00:01:19,800
So let's just for the sake of demonstration
31
00:01:19,800 --> 00:01:21,550
take a look at that variable,
32
00:01:21,550 --> 00:01:23,700
and we're gonna do that here in the server.
33
00:01:24,680 --> 00:01:27,350
So remember that everything that is not related
34
00:01:27,350 --> 00:01:32,350
to Express we're gonna do it outside of the app.JS file.
35
00:01:33,260 --> 00:01:36,660
So we only use this one here to configuring our application.
36
00:01:36,660 --> 00:01:39,530
So we only use this one in order to configure everything
37
00:01:39,530 --> 00:01:42,423
that has to do with the Express application.
38
00:01:43,370 --> 00:01:45,530
But the environment variables are really outside
39
00:01:45,530 --> 00:01:47,003
the scope of Express.
40
00:01:51,467 --> 00:01:53,367
So let's quickly do a console.log here
41
00:01:55,225 --> 00:01:57,400
of app.get
42
00:01:57,400 --> 00:01:58,690
and then end.
43
00:01:58,690 --> 00:02:02,250
Give it a save here and down here you can see development.
44
00:02:02,250 --> 00:02:05,260
Let me clear the console here, save it again,
45
00:02:05,260 --> 00:02:07,700
and so here again you see development.
46
00:02:07,700 --> 00:02:10,350
So that is the environment that we're currently in.
47
00:02:10,350 --> 00:02:14,620
And so you see that this app.getenv will get us
48
00:02:14,620 --> 00:02:17,100
the env environment variable.
49
00:02:17,100 --> 00:02:21,240
So in summary, environment variables are global variables
50
00:02:21,240 --> 00:02:24,150
that are used to define the environment in which
51
00:02:24,150 --> 00:02:25,640
a node app is running.
52
00:02:25,640 --> 00:02:28,080
So this one is set by Express,
53
00:02:28,080 --> 00:02:32,023
but node JS itself actually sets a lot of environments.
54
00:02:33,358 --> 00:02:36,950
Now this env variable here is actually set by Express,
55
00:02:36,950 --> 00:02:40,040
but node JS itself actually also sets a lot
56
00:02:40,040 --> 00:02:42,170
of environment variables.
57
00:02:42,170 --> 00:02:45,510
And so let's actually take a look at those as well.
58
00:02:45,510 --> 00:02:48,410
So these ones are located at process
59
00:02:52,035 --> 00:02:53,270
.env.
60
00:02:53,270 --> 00:02:56,450
Give it a save and so here we have a bunch
61
00:02:56,450 --> 00:03:01,170
of different variables okay and I will not go through
62
00:03:01,170 --> 00:03:04,330
all of them but let's just see they really
63
00:03:04,330 --> 00:03:05,450
are a bunch of them,
64
00:03:05,450 --> 00:03:08,180
and node uses most of them internally.
65
00:03:08,180 --> 00:03:11,060
For example a task to current working directory here
66
00:03:11,060 --> 00:03:14,630
in this environment variable and really a bunch
67
00:03:14,630 --> 00:03:16,483
of other stuff as you can see.
68
00:03:17,580 --> 00:03:22,580
For example my home folder or my login name
69
00:03:23,030 --> 00:03:26,740
you see the script that we use to start this process
70
00:03:28,024 --> 00:03:30,210
so really a bunch of stuff that for some reason
71
00:03:30,210 --> 00:03:32,640
node JS internally needs.
72
00:03:32,640 --> 00:03:36,590
Now these variables, they come from the process core module
73
00:03:36,590 --> 00:03:39,960
and we're set at the moment that the process started.
74
00:03:39,960 --> 00:03:40,970
And as you see,
75
00:03:40,970 --> 00:03:44,490
we didn't have to require the process module right.
76
00:03:44,490 --> 00:03:47,480
It is simply available everywhere automatically.
77
00:03:47,480 --> 00:03:51,420
Now in Express, many packages depend on a special variable
78
00:03:51,420 --> 00:03:53,120
called node N.
79
00:03:53,120 --> 00:03:55,930
So it's a variable that's kind of a convention
80
00:03:55,930 --> 00:03:58,920
which should define whether we're in development
81
00:03:58,920 --> 00:04:00,963
or in production mode okay.
82
00:04:01,995 --> 00:04:04,680
However Express does not really define this variable,
83
00:04:04,680 --> 00:04:07,160
and so we have to do that manually.
84
00:04:07,160 --> 00:04:09,540
And there are multiple ways in which we can do it,
85
00:04:09,540 --> 00:04:11,770
but let's start with the easiest one which is
86
00:04:11,770 --> 00:04:12,980
to use the terminal.
87
00:04:12,980 --> 00:04:16,570
So I'm gonna finish this process here, clear it,
88
00:04:16,570 --> 00:04:19,269
and so when we last started this process
89
00:04:19,269 --> 00:04:21,339
we did it using npm start.
90
00:04:21,339 --> 00:04:26,320
And npm start in turn stands for where is it?
91
00:04:26,320 --> 00:04:28,563
So it stands for nodemon server.
92
00:04:29,500 --> 00:04:31,993
And so let's copy this actually here.
93
00:04:34,090 --> 00:04:38,100
So we use nodemon server.js to start the process.
94
00:04:38,100 --> 00:04:41,010
But if you want to set an environment variable
95
00:04:41,010 --> 00:04:44,010
for this process, we need to pre-plan that variable
96
00:04:44,010 --> 00:04:44,843
to this command.
97
00:04:48,171 --> 00:04:50,296
So we say nodeenv,
98
00:04:50,296 --> 00:04:52,990
which is that special variable that I just talked about
99
00:04:52,990 --> 00:04:53,823
equals
100
00:04:55,590 --> 00:04:56,460
development.
101
00:04:56,460 --> 00:05:00,640
And if I start this process now, let's take a look.
102
00:05:00,640 --> 00:05:05,620
And so right now we have here node N set to development.
103
00:05:05,620 --> 00:05:09,400
And so this is the result of doing this console.log
104
00:05:09,400 --> 00:05:10,503
off process.env.
105
00:05:11,770 --> 00:05:14,500
And so that variable that we have here actually comes
106
00:05:14,500 --> 00:05:15,573
from that command.
107
00:05:16,770 --> 00:05:20,070
And we can actually define even more if we wanted.
108
00:05:20,070 --> 00:05:24,350
So let's just say X is 23 just for testing purposes
109
00:05:24,350 --> 00:05:28,680
start the process and now you see the X environment variable
110
00:05:28,680 --> 00:05:31,723
is attached to this 23 string all right.
111
00:05:33,098 --> 00:05:35,940
So again many packages on npm that we use
112
00:05:35,940 --> 00:05:39,290
for Express development actually depend on this
113
00:05:39,290 --> 00:05:40,560
environment variable.
114
00:05:40,560 --> 00:05:43,920
And so when our project is ready and we are gonna deploy it,
115
00:05:43,920 --> 00:05:47,440
we then should change the node N and variable to production.
116
00:05:47,440 --> 00:05:50,440
And we will do that of course once we deploy the project
117
00:05:50,440 --> 00:05:51,853
by the end of the course.
118
00:05:53,043 --> 00:05:56,350
So we set node N and X as environment variables,
119
00:05:56,350 --> 00:05:58,290
but we can do a lot more.
120
00:05:58,290 --> 00:06:01,660
And that's because we usually use environment variables
121
00:06:01,660 --> 00:06:04,950
like configuration settings for our applications.
122
00:06:04,950 --> 00:06:08,640
So whenever our app needs some configuration for stuff
123
00:06:08,640 --> 00:06:10,860
that might change based on the environment
124
00:06:10,860 --> 00:06:14,330
that the app is running in, we use environment variables.
125
00:06:14,330 --> 00:06:17,550
For example we might use different databases for development
126
00:06:17,550 --> 00:06:20,470
and for testing until we could define one variable
127
00:06:20,470 --> 00:06:23,640
for each and then activate the right database
128
00:06:23,640 --> 00:06:25,410
according to the environment.
129
00:06:25,410 --> 00:06:28,590
Also we could set sensitive data like passwords
130
00:06:28,590 --> 00:06:31,493
and user name using environment variables.
131
00:06:32,570 --> 00:06:35,670
Now it's not really practical to always define
132
00:06:35,670 --> 00:06:38,550
all of these variables in the command where we start
133
00:06:38,550 --> 00:06:39,610
the application.
134
00:06:39,610 --> 00:06:43,000
So imagine we had 10 environmental variables
135
00:06:43,000 --> 00:06:46,000
and it would be not really practical to having
136
00:06:46,000 --> 00:06:49,750
to write them out all here inside of this command.
137
00:06:49,750 --> 00:06:53,563
And so instead what we do is to create a configuration file.
138
00:06:55,408 --> 00:06:57,810
So let me go ahead and create
139
00:07:01,426 --> 00:07:04,200
config.env.
140
00:07:04,200 --> 00:07:08,000
And so env is really the convention for defining a file
141
00:07:08,000 --> 00:07:10,220
which is these environment variables.
142
00:07:10,220 --> 00:07:14,060
And VS code actually recognizes that and put this
143
00:07:14,060 --> 00:07:16,793
configuration icon right in the file.
144
00:07:18,311 --> 00:07:20,430
So let's now define this variable here
145
00:07:23,023 --> 00:07:24,050
and paste it in here.
146
00:07:24,050 --> 00:07:28,060
And we can also use some of this sensitive data
147
00:07:28,060 --> 00:07:29,260
that I just mentioned.
148
00:07:29,260 --> 00:07:34,110
So let's say the user is Jonas in lowercase
149
00:07:35,400 --> 00:07:39,523
and the password is one, two, three, four, five, six.
150
00:07:41,403 --> 00:07:44,070
Now as you see, these variable names they're usually always
151
00:07:44,070 --> 00:07:44,993
in upper case.
152
00:07:45,862 --> 00:07:48,223
So that's kind of a convention that we use.
153
00:07:49,320 --> 00:07:53,990
Now in your VS code probably all the text is just white
154
00:07:53,990 --> 00:07:56,020
so you have no syntax highlighting.
155
00:07:56,020 --> 00:08:00,810
And I have that because they use this .env extension here.
156
00:08:00,810 --> 00:08:04,800
So if you want your .env files to look nice like this
157
00:08:04,800 --> 00:08:07,233
please go ahead and install that one.
158
00:08:08,784 --> 00:08:12,959
So we have user password, let's also define the port
159
00:08:12,959 --> 00:08:14,190
on which our app should be running.
160
00:08:14,190 --> 00:08:16,810
So that's also kind of a standard variable
161
00:08:16,810 --> 00:08:19,523
that is usually in a .env file like this.
162
00:08:20,520 --> 00:08:23,410
And let's set this one to 8,000.
163
00:08:23,410 --> 00:08:27,332
Now how do we actually connect this .env file
164
00:08:27,332 --> 00:08:28,550
with our node application?
165
00:08:28,550 --> 00:08:31,240
So we need some way of reading these variables
166
00:08:31,240 --> 00:08:33,299
from this file and then saving them
167
00:08:33,299 --> 00:08:35,003
as environment variables.
168
00:08:35,870 --> 00:08:39,590
Because right now this is just a text file and node JS
169
00:08:39,590 --> 00:08:44,403
has no way of knowing that these variables are in here.
170
00:08:45,450 --> 00:08:48,490
And so for that the standard is kind of using
171
00:08:49,870 --> 00:08:51,683
a npm package called .env.
172
00:08:54,156 --> 00:08:55,663
So npm install.env,
173
00:08:57,850 --> 00:08:58,823
all right,
174
00:09:00,688 --> 00:09:03,980
then let's go over to our server and actually require
175
00:09:03,980 --> 00:09:04,853
that module.
176
00:09:07,978 --> 00:09:08,978
So const.env
177
00:09:11,907 --> 00:09:12,857
is equal to require
178
00:09:15,739 --> 00:09:17,270
.env, so simple.
179
00:09:17,270 --> 00:09:20,660
And down here we can simply use this dotenv variable
180
00:09:22,313 --> 00:09:24,810
called config on it and then in there we just have
181
00:09:24,810 --> 00:09:28,300
to pass an object to specify the path where our
182
00:09:28,300 --> 00:09:30,083
configuration file is located.
183
00:09:31,333 --> 00:09:32,166
So path,
184
00:09:37,011 --> 00:09:37,844
config dotenv.
185
00:09:37,844 --> 00:09:40,880
And so what this command will now do is to read
186
00:09:40,880 --> 00:09:43,540
our variables from the file and save them
187
00:09:43,540 --> 00:09:45,823
into node JS environment variables.
188
00:09:46,900 --> 00:09:50,850
So let's now run npm start and that should then
189
00:09:50,850 --> 00:09:53,600
block all our environment variables to the console
190
00:09:53,600 --> 00:09:55,373
because of this line of code.
191
00:09:57,340 --> 00:10:01,680
And so let's wait for it, and indeed here we have nodeenv,
192
00:10:01,680 --> 00:10:04,120
we have the port, the passwords,
193
00:10:04,120 --> 00:10:07,730
and of course the rest of the data should be somewhere
194
00:10:07,730 --> 00:10:09,883
here inside this gigantic list.
195
00:10:11,860 --> 00:10:14,193
It doesn't really matter where it is here.
196
00:10:15,090 --> 00:10:18,100
So the user was actually Jonas.io already
197
00:10:18,100 --> 00:10:21,843
and so we should call this here user name.
198
00:10:28,524 --> 00:10:32,100
Then resave this here in order to rerun the server again,
199
00:10:36,200 --> 00:10:38,330
and so now we have a user name down here
200
00:10:38,330 --> 00:10:41,060
and so these are the four environment variables
201
00:10:41,060 --> 00:10:43,100
that are defined in our file.
202
00:10:43,100 --> 00:10:46,170
Great, and just to finish, let's now actually go ahead
203
00:10:46,170 --> 00:10:51,170
and use this nodeenv variable and also the port variable.
204
00:10:51,590 --> 00:10:55,930
And to do that we go into app.js and somewhere here
205
00:10:55,930 --> 00:10:59,210
the port should be defined and somewhere here we have
206
00:10:59,210 --> 00:11:01,920
oral logger middleware and what I wanna do now
207
00:11:02,762 --> 00:11:06,090
is to only run that middleware so to only define it
208
00:11:06,090 --> 00:11:08,140
when we are actually in development,
209
00:11:08,140 --> 00:11:10,670
so that the login does not happen when the app
210
00:11:10,670 --> 00:11:12,080
is in production.
211
00:11:12,080 --> 00:11:13,773
So we have that right here,
212
00:11:16,029 --> 00:11:18,080
and so let's go ahead and say if
213
00:11:21,355 --> 00:11:22,272
process.env
214
00:11:24,616 --> 00:11:26,840
.nodeenv is equal
215
00:11:30,413 --> 00:11:33,893
to development well only then use Morgan.
216
00:11:36,014 --> 00:11:38,690
Now you might be wondering why we actually have access
217
00:11:38,690 --> 00:11:41,910
to this environment variable here when we didn't really
218
00:11:41,910 --> 00:11:45,960
define them in this file but in server.js.
219
00:11:45,960 --> 00:11:49,450
And the answer to that is that the reading of the variables
220
00:11:49,450 --> 00:11:53,050
from the file which happens here to the node process
221
00:11:53,050 --> 00:11:54,610
only needs to happen once.
222
00:11:54,610 --> 00:11:57,630
It's then in the process and the process is of course
223
00:11:57,630 --> 00:12:00,193
the same no matter in what file we are.
224
00:12:01,740 --> 00:12:03,690
So we're always in the same process
225
00:12:03,690 --> 00:12:06,610
and the environment variables are on the process.
226
00:12:06,610 --> 00:12:08,290
And so the process that is running,
227
00:12:08,290 --> 00:12:11,350
so where our application is running is always the same
228
00:12:11,350 --> 00:12:14,180
and so this is available to us in every single file
229
00:12:14,180 --> 00:12:15,550
in the project.
230
00:12:15,550 --> 00:12:17,960
So this is how we use this variable,
231
00:12:17,960 --> 00:12:22,800
now let's go back to the server and let's say that the port
232
00:12:22,800 --> 00:12:25,300
should either be the one coming from the environment
233
00:12:25,300 --> 00:12:27,333
variables or this 3,000.
234
00:12:30,889 --> 00:12:33,340
So process.env
235
00:12:35,270 --> 00:12:36,163
.port or 3,000.
236
00:12:37,350 --> 00:12:40,970
Now something is wrong here, let's take a look,
237
00:12:40,970 --> 00:12:43,920
and oh I see I'm already using 8,000
238
00:12:43,920 --> 00:12:46,520
in some other applications that's running somewhere
239
00:12:46,520 --> 00:12:47,860
on my computer.
240
00:12:47,860 --> 00:12:50,460
And so let me actually go ahead and change that here
241
00:12:51,386 --> 00:12:53,720
to 3,000 as well.
242
00:12:53,720 --> 00:12:56,750
But it doesn't really matter, you can leave yours at 8,000
243
00:12:56,750 --> 00:12:58,740
if that works for you.
244
00:12:58,740 --> 00:13:02,200
Clear it here, give it another save, and now it works.
245
00:13:02,200 --> 00:13:05,420
You can also get rid of this console.log here
246
00:13:05,420 --> 00:13:08,333
because we don't need to see these variables all the time.
247
00:13:09,310 --> 00:13:14,310
And let's now quickly do an http request to see
248
00:13:14,395 --> 00:13:17,970
if our logger still works and I'm gonna do that here
249
00:13:17,970 --> 00:13:20,853
postman again, so this one,
250
00:13:23,832 --> 00:13:26,403
going back, and now our login is gone.
251
00:13:28,027 --> 00:13:28,977
Well let's see why.
252
00:13:29,834 --> 00:13:32,883
So we have it here in development which is correct.
253
00:13:34,640 --> 00:13:37,040
And so this should actually work,
254
00:13:37,040 --> 00:13:39,613
so this is also correct here right, development.
255
00:13:41,370 --> 00:13:43,220
Well let's do a console.log and take a look
256
00:13:43,220 --> 00:13:44,083
at a tier,
257
00:13:48,740 --> 00:13:51,633
but we do get undefined down here.
258
00:13:54,589 --> 00:13:56,389
So let's check what's going on here,
259
00:13:58,068 --> 00:14:01,380
and so yeah I see the problem which is that we actually
260
00:14:01,380 --> 00:14:05,240
require the app file before our environment variables
261
00:14:05,240 --> 00:14:07,953
are read from the config file.
262
00:14:07,953 --> 00:14:10,110
And so it needs to be the other way around.
263
00:14:10,110 --> 00:14:12,070
So let me move that up here
264
00:14:12,070 --> 00:14:14,940
and then only after that we want to run the code
265
00:14:14,940 --> 00:14:16,550
that is in the up file.
266
00:14:16,550 --> 00:14:18,970
And then only after that we want to run the code
267
00:14:18,970 --> 00:14:20,503
that is in the up file.
268
00:14:21,700 --> 00:14:24,480
So again we couldn't read the process variable
269
00:14:24,480 --> 00:14:28,153
inside app.js because it wasn't yet configured.
270
00:14:29,020 --> 00:14:30,530
So if we give it a save now,
271
00:14:30,530 --> 00:14:32,640
then indeed you see development here.
272
00:14:32,640 --> 00:14:36,640
And if we do our request now, let's send it,
273
00:14:36,640 --> 00:14:39,143
then of course we get our logger back.
274
00:14:41,264 --> 00:14:43,100
So that's working now.
275
00:14:43,100 --> 00:14:45,370
And now finally as the last test,
276
00:14:45,370 --> 00:14:49,000
let's add a new start script to our package.json.
277
00:14:50,050 --> 00:14:53,050
So right now we have start nodemon server,
278
00:14:53,050 --> 00:14:55,870
but I also want to add another one for production
279
00:14:55,870 --> 00:14:59,013
just so that we can test what happens in that situation.
280
00:15:02,869 --> 00:15:06,693
So start prod, and so let me call the other one dev,
281
00:15:10,331 --> 00:15:12,410
and so in production we simply want to set
282
00:15:12,410 --> 00:15:14,693
the nodeenv variable to production.
283
00:15:18,617 --> 00:15:19,550
So nodenv equals
284
00:15:21,904 --> 00:15:24,236
production and then the rest is the same.
285
00:15:24,236 --> 00:15:25,069
So nodenv,
286
00:15:27,288 --> 00:15:28,121
server.js.
287
00:15:29,685 --> 00:15:31,135
So let's finish this one here
288
00:15:34,713 --> 00:15:35,600
npm run start
289
00:15:37,300 --> 00:15:38,303
production.
290
00:15:39,330 --> 00:15:42,030
And so now you see that our nodeenv variable
291
00:15:42,030 --> 00:15:44,340
is actually set to production.
292
00:15:44,340 --> 00:15:47,670
So that is coming from this console.log here.
293
00:15:47,670 --> 00:15:49,900
And if now do this request,
294
00:15:49,900 --> 00:15:52,323
we will then not get our logger.
295
00:15:53,505 --> 00:15:55,283
So it's not here as it was before.
296
00:15:56,580 --> 00:15:59,550
And so this is basically how we run different code
297
00:15:59,550 --> 00:16:02,653
depending whether we are in development or in production.
298
00:16:03,530 --> 00:16:06,193
So let's get rid of this one here,
299
00:16:07,650 --> 00:16:09,150
we can close this one as well,
300
00:16:11,398 --> 00:16:14,120
and so let's go back to our development script of course,
301
00:16:14,120 --> 00:16:16,420
so npm run start
302
00:16:19,598 --> 00:16:21,113
dev and all right here we are.
303
00:16:21,990 --> 00:16:23,650
Just one final test
304
00:16:24,810 --> 00:16:28,270
and indeed here is our login result.
305
00:16:28,270 --> 00:16:30,580
So that's it for environment variables,
306
00:16:30,580 --> 00:16:33,823
let's move on to the final lecture in this section.
24169
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.