1
00:00:00,750 --> 00:00:05,880
Let's talk about environment variables in Node.js.

2
00:00:05,910 --> 00:00:07,140
What is it?

3
00:00:07,440 --> 00:00:11,130
Why do we need it and how to use it?

4
00:00:11,250 --> 00:00:15,450
The first question is what is an environment variable?

5
00:00:15,570 --> 00:00:17,100
Environment variable.

6
00:00:17,100 --> 00:00:29,310
In node, I use to store sensitive data such as API credentials or keys or passwords and any other information

7
00:00:29,310 --> 00:00:33,360
that shouldn't be directly written inside your code.

8
00:00:33,510 --> 00:00:35,580
So why do we need it?

9
00:00:35,610 --> 00:00:38,910
The key point is about security.

10
00:00:39,030 --> 00:00:47,340
So looking inside our project where we are not protecting our code is inside the config and that is

11
00:00:47,340 --> 00:00:56,460
our function to connect to MongoDB and the code that is more secured is our MongoDB connection string.

12
00:00:57,000 --> 00:01:05,820
Let's say that if we push our project to get help and any user can have access to our code and if that

13
00:01:05,820 --> 00:01:14,160
user have access to our MongoDB connection string, that user can perform some operations, such as

14
00:01:14,160 --> 00:01:22,710
dropping our entire database, deleting error code, updating and performing all the crowd operations

15
00:01:22,710 --> 00:01:25,620
of which we are not even aware of.

16
00:01:25,650 --> 00:01:28,630
That is why we need to protect it.

17
00:01:28,650 --> 00:01:30,750
So how can we use it?

18
00:01:31,170 --> 00:01:39,120
The first step is to create a file with extension called Dot E and V, and that is what we did in the

19
00:01:39,120 --> 00:01:40,290
previous video.

20
00:01:40,530 --> 00:01:48,660
Depending on the nature of your application, you might have multiple dot E and V or environment variable

21
00:01:48,900 --> 00:01:52,680
one for production and one for development.

22
00:01:52,710 --> 00:02:01,470
We can say that this one is for development purposes and we can have one for production or we can have

23
00:02:01,470 --> 00:02:11,400
one file called E and V, and inside the file we can create two MongoDB connection string one for development

24
00:02:11,400 --> 00:02:14,790
as dev and one for production.

25
00:02:14,790 --> 00:02:20,400
And we can use the connection string depending on the environment that we are on.

26
00:02:20,970 --> 00:02:23,970
So how can we make use of it?

27
00:02:24,000 --> 00:02:32,280
The main logic here is that we are putting this one inside the internals of Node.js and we can have

28
00:02:32,280 --> 00:02:37,400
access to this inside what is called process Dot E and V.

29
00:02:37,410 --> 00:02:39,960
So let's see if we're going to have that one.

30
00:02:39,990 --> 00:02:47,310
Let's get back to the connection function here and inside here let's console.log what is called process

31
00:02:47,310 --> 00:02:48,660
dot envy.

32
00:02:48,690 --> 00:02:52,130
This method was given to us by Node.js.

33
00:02:52,140 --> 00:02:55,830
So let me check the terminal and let's see what we have.

34
00:02:56,280 --> 00:03:01,230
And these are the environment being used inside our application.

35
00:03:01,230 --> 00:03:08,790
We have the shell, the program that we are using and other properties, but what we want is what we

36
00:03:08,790 --> 00:03:11,370
call our MongoDB connection string.

37
00:03:11,370 --> 00:03:13,960
And you can see that we don't have it here.

38
00:03:13,980 --> 00:03:16,710
What we are expecting is this variable here.

39
00:03:16,710 --> 00:03:24,290
What you put in here will be available on the process dot E and V, but we don't have it by default.

40
00:03:24,300 --> 00:03:33,330
Node.js will not populate or add this variable inside the process of E and V unless we install a different

41
00:03:33,330 --> 00:03:34,910
package to get it done.

42
00:03:34,920 --> 00:03:38,910
So let me shut down the terminal and play as well.

43
00:03:38,910 --> 00:03:43,800
And we are going to install a package called Dot Envy.

44
00:03:45,180 --> 00:03:52,530
Again, the purpose for this package is it's going to help us to have this variable inside the process

45
00:03:52,590 --> 00:03:53,690
of EMV.

46
00:03:53,700 --> 00:03:58,470
So now after installing, let me clear and then restart the server.

47
00:04:00,260 --> 00:04:01,520
After installing.

48
00:04:01,550 --> 00:04:02,870
Let's make use of it.

49
00:04:02,900 --> 00:04:06,020
Let's get back to the server JS file.

50
00:04:06,050 --> 00:04:14,360
This is our entry point and you need to require the dot E and V before other packages because we may

51
00:04:14,360 --> 00:04:18,709
need the environment variables before even our application run.

52
00:04:18,709 --> 00:04:21,769
So here we to make use of cost.

53
00:04:22,800 --> 00:04:23,370
Darts.

54
00:04:23,370 --> 00:04:30,270
E and V is equal to require the package as dot E and V.

55
00:04:34,240 --> 00:04:38,180
And on that we have a method called config.

56
00:04:38,200 --> 00:04:47,380
So I'm going to make use of dot envy, dot config, save it and let's restart our server.

57
00:04:47,650 --> 00:04:53,140
Make sure any time you make changes to the environment variable you need to restart the server.

58
00:04:53,140 --> 00:04:56,560
So let me restart it manually.

59
00:04:57,590 --> 00:05:04,970
And if you look through, you can see that there is no way to be found for the mongo you are.

60
00:05:05,480 --> 00:05:07,130
What is the problem?

61
00:05:07,280 --> 00:05:10,450
The problem is about the arrangement.

62
00:05:10,460 --> 00:05:19,100
So looking at our code, we are calling the function to connect before calling the environment variable.

63
00:05:19,190 --> 00:05:24,250
So by the time this function got run, there is no variable called mongo.

64
00:05:24,260 --> 00:05:26,200
You are on the process.

65
00:05:26,210 --> 00:05:28,580
That is why we don't have it here.

66
00:05:28,730 --> 00:05:33,560
So if I bring this one before the function to connect.

67
00:05:33,560 --> 00:05:36,470
Now let's check our terminal and here we go.

68
00:05:36,470 --> 00:05:39,910
You can see that we have a variable called Mongo.

69
00:05:39,920 --> 00:05:43,040
You are L and this is the value.

70
00:05:43,250 --> 00:05:50,270
And for this one we have this short way of coordinates so I can remove this one.

71
00:05:51,740 --> 00:05:54,560
And remove the variable here.

72
00:05:54,590 --> 00:06:00,500
Remember, required is a function so I can change what is called config.

73
00:06:01,250 --> 00:06:02,450
And that is it.

74
00:06:02,480 --> 00:06:03,080
Save it.

75
00:06:03,080 --> 00:06:04,580
And let's check our terminal.

76
00:06:04,580 --> 00:06:07,670
And you can see that we have it as well.

77
00:06:07,670 --> 00:06:10,430
So now we can make use of this one.

78
00:06:10,430 --> 00:06:18,140
So now let's get back to our DB connection function here and I'm going to remove this one and make use

79
00:06:18,140 --> 00:06:22,910
of process dot e and v dot mongo u are.

80
00:06:22,910 --> 00:06:24,760
And now let's check our terminal.

81
00:06:27,320 --> 00:06:31,140
And you can see DB connected successfully.

82
00:06:31,160 --> 00:06:38,450
So as you move on, we are going to put any code, for example API, key cloud, binary and many more

83
00:06:38,450 --> 00:06:40,310
inside this file.

84
00:06:40,730 --> 00:06:43,070
All right, let's continue in the next video.

