#48 Environment variable configuration file | Working with Express JS | A Complete NODE JS Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lecture we are going to create a configuration file and in that configuration file we are going to Define all the environment variables which we are going to use for our application this lecture is the continuation of my previous lecture so if you have not watched that lecture first then I will highly recommend you to go through that lecture first and then only come to this lecture here let's go ahead and let's create a new file and let's call this file config.env so it is a convention to use dot EnV extension for the files which is going to store the environment variables now let's go ahead and let's define some involvement variables inside this config.erv file so the first environment variable which I am going to Define here is node underscore EnV and I'm going to set it to development then let's also say we want to set some sensitive data as well using this environment configuration file for example here let's say we want to have a username environment variable and there I am going to set it to admin and we also want to have a password environment variable and let's specify some password for now so I'll say one two three four five six as you can see these environment variables we have defined all in uppercase now this is not mandatory but this is a convention that is followed while creating these environment variables and if you see inside this config.env file all the text is white even when we are assigning a number or a string value all these texts are white now in order to format this config.env file basically any dot EnV file what we can do is we can go to extensions and there we can search for DOT EnV extension okay so here we have this dot Envy extension let's go ahead and let's install this for that let's click on this install button okay so that has been installed and now let me close this and now when we go to this config.env file now you will see that now we have a better syntax highlighting in this config.env file so basically now the content of this config.env file that is more readable and it looks better all right now let's also go ahead and let's also Define one more environment variable called port so we are using the port in this server.js file here so here we have created this port variable and to that we are assigning 3000. so instead of creating a variable like this since this value is going to be constant throughout this application what I'm going to do is instead of creating a constant variable here I am going to create an environment variable and there I will assign this port with the value 3000. now the next question is how do we connect our EnV file to our node.js application here we have created this config.env file and in there we have created some environment variables but node.js process is not aware about these environment variables so somehow we need to tell node.js process that it also has to use these environment variables basically we need some way of reading these environment variables from this file and saving them as environment variables in node.js process because right now this is just like a text file and node.js has no way of knowing that we are trying to define environment variables here right so we need some way to tell node.js process that here we have defined some environment variables and we want the process to use these environment variables to do that we need another package from npm called dot EnV so here I will go to this terminal this CMD terminal and there let's try to install dot EnV from npm so for that let's say npm install dot EnV let's press enter so dot EnV has been installed now let's go to server.js and there let's require this dot EnV package so for that let's use this require function and there let's specify the package name so the package name is dot EnV and this is going to return as an object let's go ahead and let's store that object in a variable and let's call that variable dot EnV okay next on this dot EnV we need to call a method called config and to this config method we need to pass an object and in that object we need to specify a Path property and to that Path property we need to assign the path of the config.env file so it is in root directory and the file name is config dot EnV now remember that this line of code should always come before we require this app so I will cut it from here and after we have used this config method after that let's use this app so this is very important okay here first we are importing this dot EnV package and then we are using this config method on this dot EnV package so when we use this config method on this dot EnV and when we have specified the config.env file path what it will do is whatever variables we have defined inside this config.env those will be saved in the node.js environment variables so now let's go to this config.env let's save this file let's go to this node terminal where the process is running and here let's stop the server by pressing Ctrl C and let's restart the server by running npm start if I press enter so the server has restarted that means the process has started and now if I scroll up first of all you will see the username environment variable here which is set to Manoj but here we have set this username to admin that's because there is already an environment variable with this name username okay so we will change this username here or let's change it to Simply user instead of username and if I scroll up we should also have other environment variables which we have set for example password and Port so here we have this port which is set to 3000 that's what we have set it here we should have password so here we have this password and it is set to this value then we should also have node underscore EnV so let's scroll up and here we have the node underscore EnV and it is set to development so as you can see the environment variables which we have defined here inside this config.env file that has also been added to the node.js process that means now we can access these environment variables on the node.js process okay now here we have changed this username to user so what I will do is I will save the changes here I will stop the server by pressing Ctrl C and let me restart this server okay so along with username now we should also have this user environment variable which is set to admin finally let's now go ahead and let's see how we can use these environment variables which we have just created here so here I'm going to use this node EnV environment variable and this port environment variable let's just go ahead and let's use the port environment variable so here I am setting this port to 3000 but instead of setting it to 3000 I want to read it from the environment variable for that here I can say process dot EnV dot port okay so this process.env.port it is going to return us this port number this 3000 but let's say if this port is not set in that case we also want to use the default value as 3000 so here I will also use this or operator okay so if we have this port environment variable in that case we want to use the value of that Port environment variable otherwise we want to use this value 3000 as the port number and then in the app.js here we are using this Morgan middleware so this Morgan middleware is basically used to log the request details right so if I go to postman and let's see if I make some request and if I go back to vs code there you will see the request details has been logged here now I only want to log these request details if the environment is development environment in production I don't want to log these request details so for that here I am going to use an if statement inside that if statement I am going to use this process dot EnV Dot and on this environment variables we have created one environment variable called node underscore EnV if it is equal to development then only we want to run this Morgan middleware let me cut it from here and let me put it inside this if statement okay if I save the changes since currently this node underscore EnV it is development if I go ahead and if I make a request from The Postman and if I go back to visual studio there you will see that the request details has been logged here but if this node environment is production or something else in that case the request details will not be logged okay now here you might be wondering why we have access to this environment variable here in this file because we are reading the environment variables from this config.env file inside this server.js file we are doing it at this line then how do we have access to those environment variables inside this app.js file that's because as I have mentioned earlier when we are reading these environment variables from this config.env file those environment variables will be set on the node.js process for example they are set here okay and the process is going to be same no matter UI in which file whether you are in server.js file or you in app.js file or in some other file inside this project directory the process is going to be same so that's why we have access to these environment variables even in app.js file although we are reading those environment variables in the server.js file so keep in mind that reading of the variables from the EnV file that has to happen only once and after that those environment variables will be available in the process and the process is going to be same no matter in which file you are all right so in this lecture we learned how to create a configuration file for environment variables and then how to read these environment variables and set it on the node.js process and then how to use those environment variables from the node.js process for example here we are using this node EnV environment variable by accessing it on the node.js process and here we are using this port environment variable by accessing it on the node.js process this is all from this lecture if you have any questions then feel free to ask it thank you for listening and have a great day
Info
Channel: procademy
Views: 7,875
Rating: undefined out of 5
Keywords: environment variables, dotenv, node js, express js, node js tutorial, complete node js course
Id: PxshhOKNPpQ
Channel Id: undefined
Length: 11min 35sec (695 seconds)
Published: Wed Jan 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.