Let's talk about environment variables in Node.js.
What is it?
Why do we need it and how to use it?
The first question is what is an environment variable?
Environment variable.
In node, I use to store sensitive data such as API credentials or keys or passwords and any other information
that shouldn't be directly written inside your code.
So why do we need it?
The key point is about security.
So looking inside our project where we are not protecting our code is inside the config and that is
our function to connect to MongoDB and the code that is more secured is our MongoDB connection string.
Let's say that if we push our project to get help and any user can have access to our code and if that
user have access to our MongoDB connection string, that user can perform some operations, such as
dropping our entire database, deleting error code, updating and performing all the crowd operations
of which we are not even aware of.
That is why we need to protect it.
So how can we use it?
The first step is to create a file with extension called Dot E and V, and that is what we did in the
previous video.
Depending on the nature of your application, you might have multiple dot E and V or environment variable
one for production and one for development.
We can say that this one is for development purposes and we can have one for production or we can have
one file called E and V, and inside the file we can create two MongoDB connection string one for development
as dev and one for production.
And we can use the connection string depending on the environment that we are on.
So how can we make use of it?
The main logic here is that we are putting this one inside the internals of Node.js and we can have
access to this inside what is called process Dot E and V.
So let's see if we're going to have that one.
Let's get back to the connection function here and inside here let's console.log what is called process
dot envy.
This method was given to us by Node.js.
So let me check the terminal and let's see what we have.
And these are the environment being used inside our application.
We have the shell, the program that we are using and other properties, but what we want is what we
call our MongoDB connection string.
And you can see that we don't have it here.
What we are expecting is this variable here.
What you put in here will be available on the process dot E and V, but we don't have it by default.
Node.js will not populate or add this variable inside the process of E and V unless we install a different
package to get it done.
So let me shut down the terminal and play as well.
And we are going to install a package called Dot Envy.
Again, the purpose for this package is it's going to help us to have this variable inside the process
of EMV.
So now after installing, let me clear and then restart the server.
After installing.
Let's make use of it.
Let's get back to the server JS file.
This is our entry point and you need to require the dot E and V before other packages because we may
need the environment variables before even our application run.
So here we to make use of cost.
Darts.
E and V is equal to require the package as dot E and V.
And on that we have a method called config.
So I'm going to make use of dot envy, dot config, save it and let's restart our server.
Make sure any time you make changes to the environment variable you need to restart the server.
So let me restart it manually.
And if you look through, you can see that there is no way to be found for the mongo you are.
What is the problem?
The problem is about the arrangement.
So looking at our code, we are calling the function to connect before calling the environment variable.
So by the time this function got run, there is no variable called mongo.
You are on the process.
That is why we don't have it here.
So if I bring this one before the function to connect.
Now let's check our terminal and here we go.
You can see that we have a variable called Mongo.
You are L and this is the value.
And for this one we have this short way of coordinates so I can remove this one.
And remove the variable here.
Remember, required is a function so I can change what is called config.
And that is it.
Save it.
And let's check our terminal.
And you can see that we have it as well.
So now we can make use of this one.
So now let's get back to our DB connection function here and I'm going to remove this one and make use
of process dot e and v dot mongo u are.
And now let's check our terminal.
And you can see DB connected successfully.
So as you move on, we are going to put any code, for example API, key cloud, binary and many more
inside this file.
All right, let's continue in the next video.