‫Welcome back.
‫In this video you will learn all about
‫environment variables.
‫So what they are, how we set them, and how we use them.
‫Now this is not exactly about Express,
‫it really has to do with node JS development in general,
‫but we really need this in order to move on
‫in the project and so this is a good time
‫to talk about this.
‫So node JS, or Express apps,
‫can run in different environments.
‫And the most important ones are the development environment
‫and the production environment.
‫That's because depending on the environment,
‫we might use different databases for example,
‫or we might turn login on or off,
‫or we might turn debugging on or off,
‫or really all kinds of different settings that might
‫change depending on the development that we're in.
‫So again the most important ones are the development
‫and the production environment.
‫But there are other environments
‫that bigger teams might use.
‫So this type of setting that I just mentioned,
‫like different databases or login turned on or off,
‫that will be based on environment variables.
‫Now by default, Express sets the environment to development
‫which makes a lot of sense because that's what we're doing
‫when we start a new project.
‫So let's just for the sake of demonstration
‫take a look at that variable,
‫and we're gonna do that here in the server.
‫So remember that everything that is not related
‫to Express we're gonna do it outside of the app.JS file.
‫So we only use this one here to configuring our application.
‫So we only use this one in order to configure everything
‫that has to do with the Express application.
‫But the environment variables are really outside
‫the scope of Express.
‫So let's quickly do a console.log here
‫of app.get
‫and then end.
‫Give it a save here and down here you can see development.
‫Let me clear the console here, save it again,
‫and so here again you see development.
‫So that is the environment that we're currently in.
‫And so you see that this app.getenv will get us
‫the env environment variable.
‫So in summary, environment variables are global variables
‫that are used to define the environment in which
‫a node app is running.
‫So this one is set by Express,
‫but node JS itself actually sets a lot of environments.
‫Now this env variable here is actually set by Express,
‫but node JS itself actually also sets a lot
‫of environment variables.
‫And so let's actually take a look at those as well.
‫So these ones are located at process
‫.env.
‫Give it a save and so here we have a bunch
‫of different variables okay and I will not go through
‫all of them but let's just see they really
‫are a bunch of them,
‫and node uses most of them internally.
‫For example a task to current working directory here
‫in this environment variable and really a bunch
‫of other stuff as you can see.
‫For example my home folder or my login name
‫you see the script that we use to start this process
‫so really a bunch of stuff that for some reason
‫node JS internally needs.
‫Now these variables, they come from the process core module
‫and we're set at the moment that the process started.
‫And as you see,
‫we didn't have to require the process module right.
‫It is simply available everywhere automatically.
‫Now in Express, many packages depend on a special variable
‫called node N.
‫So it's a variable that's kind of a convention
‫which should define whether we're in development
‫or in production mode okay.
‫However Express does not really define this variable,
‫and so we have to do that manually.
‫And there are multiple ways in which we can do it,
‫but let's start with the easiest one which is
‫to use the terminal.
‫So I'm gonna finish this process here, clear it,
‫and so when we last started this process
‫we did it using npm start.
‫And npm start in turn stands for where is it?
‫So it stands for nodemon server.
‫And so let's copy this actually here.
‫So we use nodemon server.js to start the process.
‫But if you want to set an environment variable
‫for this process, we need to pre-plan that variable
‫to this command.
‫So we say nodeenv,
‫which is that special variable that I just talked about
‫equals
‫development.
‫And if I start this process now, let's take a look.
‫And so right now we have here node N set to development.
‫And so this is the result of doing this console.log
‫off process.env.
‫And so that variable that we have here actually comes
‫from that command.
‫And we can actually define even more if we wanted.
‫So let's just say X is 23 just for testing purposes
‫start the process and now you see the X environment variable
‫is attached to this 23 string all right.
‫So again many packages on npm that we use
‫for Express development actually depend on this
‫environment variable.
‫And so when our project is ready and we are gonna deploy it,
‫we then should change the node N and variable to production.
‫And we will do that of course once we deploy the project
‫by the end of the course.
‫So we set node N and X as environment variables,
‫but we can do a lot more.
‫And that's because we usually use environment variables
‫like configuration settings for our applications.
‫So whenever our app needs some configuration for stuff
‫that might change based on the environment
‫that the app is running in, we use environment variables.
‫For example we might use different databases for development
‫and for testing until we could define one variable
‫for each and then activate the right database
‫according to the environment.
‫Also we could set sensitive data like passwords
‫and user name using environment variables.
‫Now it's not really practical to always define
‫all of these variables in the command where we start
‫the application.
‫So imagine we had 10 environmental variables
‫and it would be not really practical to having
‫to write them out all here inside of this command.
‫And so instead what we do is to create a configuration file.
‫So let me go ahead and create
‫config.env.
‫And so env is really the convention for defining a file
‫which is these environment variables.
‫And VS code actually recognizes that and put this
‫configuration icon right in the file.
‫So let's now define this variable here
‫and paste it in here.
‫And we can also use some of this sensitive data
‫that I just mentioned.
‫So let's say the user is Jonas in lowercase
‫and the password is one, two, three, four, five, six.
‫Now as you see, these variable names they're usually always
‫in upper case.
‫So that's kind of a convention that we use.
‫Now in your VS code probably all the text is just white
‫so you have no syntax highlighting.
‫And I have that because they use this .env extension here.
‫So if you want your .env files to look nice like this
‫please go ahead and install that one.
‫So we have user password, let's also define the port
‫on which our app should be running.
‫So that's also kind of a standard variable
‫that is usually in a .env file like this.
‫And let's set this one to 8,000.
‫Now how do we actually connect this .env file
‫with our node application?
‫So we need some way of reading these variables
‫from this file and then saving them
‫as environment variables.
‫Because right now this is just a text file and node JS
‫has no way of knowing that these variables are in here.
‫And so for that the standard is kind of using
‫a npm package called .env.
‫So npm install.env,
‫all right,
‫then let's go over to our server and actually require
‫that module.
‫So const.env
‫is equal to require
‫.env, so simple.
‫And down here we can simply use this dotenv variable
‫called config on it and then in there we just have
‫to pass an object to specify the path where our
‫configuration file is located.
‫So path,
‫config dotenv.
‫And so what this command will now do is to read
‫our variables from the file and save them
‫into node JS environment variables.
‫So let's now run npm start and that should then
‫block all our environment variables to the console
‫because of this line of code.
‫And so let's wait for it, and indeed here we have nodeenv,
‫we have the port, the passwords,
‫and of course the rest of the data should be somewhere
‫here inside this gigantic list.
‫It doesn't really matter where it is here.
‫So the user was actually Jonas.io already
‫and so we should call this here user name.
‫Then resave this here in order to rerun the server again,
‫and so now we have a user name down here
‫and so these are the four environment variables
‫that are defined in our file.
‫Great, and just to finish, let's now actually go ahead
‫and use this nodeenv variable and also the port variable.
‫And to do that we go into app.js and somewhere here
‫the port should be defined and somewhere here we have
‫oral logger middleware and what I wanna do now
‫is to only run that middleware so to only define it
‫when we are actually in development,
‫so that the login does not happen when the app
‫is in production.
‫So we have that right here,
‫and so let's go ahead and say if
‫process.env
‫.nodeenv is equal
‫to development well only then use Morgan.
‫Now you might be wondering why we actually have access
‫to this environment variable here when we didn't really
‫define them in this file but in server.js.
‫And the answer to that is that the reading of the variables
‫from the file which happens here to the node process
‫only needs to happen once.
‫It's then in the process and the process is of course
‫the same no matter in what file we are.
‫So we're always in the same process
‫and the environment variables are on the process.
‫And so the process that is running,
‫so where our application is running is always the same
‫and so this is available to us in every single file
‫in the project.
‫So this is how we use this variable,
‫now let's go back to the server and let's say that the port
‫should either be the one coming from the environment
‫variables or this 3,000.
‫So process.env
‫.port or 3,000.
‫Now something is wrong here, let's take a look,
‫and oh I see I'm already using 8,000
‫in some other applications that's running somewhere
‫on my computer.
‫And so let me actually go ahead and change that here
‫to 3,000 as well.
‫But it doesn't really matter, you can leave yours at 8,000
‫if that works for you.
‫Clear it here, give it another save, and now it works.
‫You can also get rid of this console.log here
‫because we don't need to see these variables all the time.
‫And let's now quickly do an http request to see
‫if our logger still works and I'm gonna do that here
‫postman again, so this one,
‫going back, and now our login is gone.
‫Well let's see why.
‫So we have it here in development which is correct.
‫And so this should actually work,
‫so this is also correct here right, development.
‫Well let's do a console.log and take a look
‫at a tier,
‫but we do get undefined down here.
‫So let's check what's going on here,
‫and so yeah I see the problem which is that we actually
‫require the app file before our environment variables
‫are read from the config file.
‫And so it needs to be the other way around.
‫So let me move that up here
‫and then only after that we want to run the code
‫that is in the up file.
‫And then only after that we want to run the code
‫that is in the up file.
‫So again we couldn't read the process variable
‫inside app.js because it wasn't yet configured.
‫So if we give it a save now,
‫then indeed you see development here.
‫And if we do our request now, let's send it,
‫then of course we get our logger back.
‫So that's working now.
‫And now finally as the last test,
‫let's add a new start script to our package.json.
‫So right now we have start nodemon server,
‫but I also want to add another one for production
‫just so that we can test what happens in that situation.
‫So start prod, and so let me call the other one dev,
‫and so in production we simply want to set
‫the nodeenv variable to production.
‫So nodenv equals
‫production and then the rest is the same.
‫So nodenv,
‫server.js.
‫So let's finish this one here
‫npm run start
‫production.
‫And so now you see that our nodeenv variable
‫is actually set to production.
‫So that is coming from this console.log here.
‫And if now do this request,
‫we will then not get our logger.
‫So it's not here as it was before.
‫And so this is basically how we run different code
‫depending whether we are in development or in production.
‫So let's get rid of this one here,
‫we can close this one as well,
‫and so let's go back to our development script of course,
‫so npm run start
‫dev and all right here we are.
‫Just one final test
‫and indeed here is our login result.
‫So that's it for environment variables,
‫let's move on to the final lecture in this section.