Environment Variables in Practice
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
- |
Lesson Transcript
[00:00 - 00:13] The purpose of this lesson is to demonstrate how to use environment variables by setting up a basic node server script. We're going to be starting off with an empty node script.
[00:14 - 00:30] To access environment variables, you use the object process.env. To start, the most important environment variable is node_end, which we access through process.env.node_end.
[00:31 - 00:39] This is used to hold the name of the configuration set you're currently using. Most frameworks interface with it in some way.
[00:40 - 01:04] Let's see what that looks like by console logging in. To do that, first we're going to add a start script, node_index.js, and then let's run that.
[01:05 - 01:12] We'll do npm run start, and we'll notice it's undefined. That's because it has to be set.
[01:13 - 01:21] So how do we set it? The way to set it is when you run the script, you have to enter it into the environment.
[01:22 - 01:43] In this case, we can use node_end equals something like development, and when we rerun that, it prints development. The first exercise we're going to do is to use node_end for a cores check, like that on a backend server.
[01:44 - 02:06] To those who don't know what a cores check is, it's a commonly used security feature on node.js backend servers that can make it difficult to do local testing if left on. It's not that important to the cores, so if you'd like to learn more, I'll leave a link in the description below about how it works.
[02:07 - 02:35] If we want to serve to all domains for some values of node_end and only to one domain for production, we can use a blurb like this where we exclusively serve to localhost when node_ end equals production, and to all domains every other time. Just to make sure this is working, let's show what happens when we print that.
[02:36 - 02:55] So in the npm run start, we have our value being development, and when we set it to production, it says exclusively serves to localhost. But what happens when we want to set which domain that we're going to be serving to?
[02:56 - 03:04] We have to use an environment variable to set the value here, instead of local host. We can set it to another environment variable.
[03:05 - 03:18] In this case, I'm going to call it process.env.server_url. But how can we set so many variables on the command line? It gets really long.
[03:19 - 03:31] I mean, yes, we can use the an_end character server_url, but this gets very difficult and confusing to use. So let's not do this.
[03:32 - 03:47] Instead, we're going to get our configuration from a file, and that file is usually called a .env file. So I just created a .env file.
[03:48 - 03:57] Notice that there's no extensions, there's nothing before the .env. That is the extension, it's a plain text file.
[03:58 - 04:17] And we can use this file to set the server_url value, server_url, equals, so let's keep it at localhost for now, and we'll change it in a little bit. In fact, to be more realistic, we'll make it a proper URL, port 3000.
[04:18 - 04:42] Unfortunately, Node can access this file out of the box. We have to use an npm library, which is .env. The way to use it is to require .env, and then we use the configuration function, config.
[04:43 - 04:53] And so when we run that function, npm run_start, notice it says exclusively serves to localhost 3000. Great.
[04:54 - 05:06] Before we continue, let's look at how .env files are structured. They're pretty flexible, but I like to use them like this because they're constants.
[05:07 - 05:23] Every variable in .env variables are constants. So the convention is to use uppercase with underscore, snake-cased, but it is case sensitive if you do want to change that up.
[05:24 - 05:32] The values can contain spaces after the equal signs, but I really discourage it . I don't use that.
[05:33 - 05:52] And the values are always strings. They're always strings all the time. There is a link plugin if you're wondering, and I've linked it in the description, as well as a few other rules that I advise you follow for how to use .env files.
[05:53 - 06:16] So wait, if everything in environment variables is a string, what if we have numbers or Boolean that we're trying to bring into our project? Well, for numbers, well, you're going to have to set it to an interim variable and parse it, but for Boolean values, it's a little bit harder.
[06:17 - 06:26] To demonstrate, I'm going to add another variable. We'll call it use cores. So again, yeah, we're doing the use cores example.
[06:27 - 06:54] And by default, we don't want to use it. We'll say equals false. And we'll say if process dot n dot use cores equals, well, in this case, we have to choose between true, like the string true and false, because the default value of an environment variable is actually undefined.
[06:55 - 07:14] So if no value is set, it's actually going to be undefined. And we have to use that thinking to determine which value we're going to check for. The thing is that we don't want to check for the default value because it could actually be undefined.
[07:15 - 07:40] So in this case, we'll check for true. So if the value is not set, or if it is false, then it will do the default action, which is served to all actions, the domains. And otherwise, only specifically if use course is set to true, will it exclusively serve to the server URL?
[07:41 - 07:56] So what happens if we want to put our dot n file in a custom folder, like a config folder? Well, let's try it. We'll put create a config folder, and we'll move the dot n file into the config folder.
[07:57 - 08:08] Now what we have to do is use the path library so that way we can access this. So first, we'll import the path library.
[08:09 - 08:25] And then, instead of having config empty, what we can do is we can specify the path in which the dot n file is loaded from. In this case, it's in the config folder and it's called dot and B.
[08:26 - 08:33] You can rename this, but you probably shouldn't. Well, we're going to talk about that in a later module.
[08:34 - 08:58] For now, I'm going to take it out of there because this isn't something that I recommend. I recommend that before you use a new framework, that you look at the various environment variables that are paired with that framework.
[08:59 - 09:10] One other piece of advice is that someone asked me what happens when dot n updates. Well, the format of dot n is a standard that's been around for a long time.
[09:11 - 09:21] It's a standard across multiple languages, operating systems. I don't expect this to change anytime soon.
[09:22 - 09:33] And the skills that you learn in this course are going to help you in other languages. Lastly, setting node n equals production.
[09:34 - 09:43] This is a Unix only thing. So if you do have a team that is cross OS, you may need to do use other techniques.
[09:44 - 09:58] Sometimes I just have a second function and a second script that's like node set node n equals production and and node n. So that's the first step.
[09:59 - 10:12] You can also use some libraries like cross end, but the cross end is very old and deprecated. So that's why I'm not showing it in this video.
[10:13 - 10:16] Anyways, that's all for now. See you in the next one.