Welcome back.
I wanted to show you a quick video on an important concept called environmental variables and I've created
a simple survey here that we've done before using the http module that comes with node and we have a
simple server that listens to port 3000.
If I ran a node server dot js we get server is listening on port 3000. However this is not
really realistic.
Sometimes we don't want to hard code what port we are listening to.
For example when we're deploying something through another service like we will with Heroku we can't
really pick what port we're going to listen to.
We need something dynamic so that it can be changed.
How can we do that? Well this concept of environmental variables.
is the idea that there is variables just like we have a const A equals one const B equals two that are
variables in this environment.
There's an idea of an environmental variable which is an environment such as here, a computer that this
application is running on or the server is running on.
It may have its own variables and we're able to inject these variables into the specific location in
a file.
And this isn't just a node or server specific.
It is with all programming.
Everywhere you look there is going to be environmental variables that are being used.
It's a concept.
So how can we do this in node. Well in node we have access to - let's do a console log process dot env
and with process dot env,
if I run the server you see that it tells me the environment such as where I'm running this from
the home, tells us what terminal program I'm running with this with, which is iTerm, getting a lot of information.
Let's clear this. And with this
now we can set our own variables. If you want to set the port.
For example we can say process dot env dot port and now we change this to
process dot env dot port.
And just to make this cleaner we can just have PORT equal to process dot env dot PORT.
So now we can remove the same call.
Let's just console log here as well just to make sure that we see it.
If I run node server I get undefined. But that's because this port number is something that we didn't
have initially.
I just made this name up.
I could have said ABC. It doesn't exist. In order for us to access or create an environmental variable
within this computer.
We have to inject it.
I'm going to show you how to do with fish which is the shell that I'm using here which is a little
bit different than bash and most common you'll see Bash.
So I'm going to show you that way first. With bash we simply do PORT 3000 node server dot js
And look at that. Server is listening to port 3000. If I change this to
3050 server is listening to 3050.
And this isn't limited to port.
You'll see in the next couple of videos during production we can set up something like a database URL.
And this database URL can now be dynamic.
Maybe we won't know where the database URL will be. In that case.
I can simply say database
URL equals 123 node server dot js
And do we get ports is not defined.
Because we're not using it here so let's just revert this back to port three thousand and run that command
again.
And look at that server listening to port 1 2 3 because that's the database URL and we can set whatever
environmental variables we want.
And the importance of them won't really come into play until later on in these videos.
But they're really important for things that should be kept secret or dynamic.
For example port we might not know what port the server that's going to be running our API will be run
on.
So this way whoever is running it can just set the port without touching the codebase. But also for secrets
for example API keys that we might not want to have in our codebase.
We can just launch the app and give maybe API keys or even passwords to log into the database dynamically
through the environment so that it's more secure.
Let me show you how to do this in fish.
It's a little bit different and you don't need to worry too much about it because it's just specific
to fish it's kind of a weird kink but you can do environment database
URL Will say hello node server dot js and we have servers listening on port Hello.
Again this is just specific to fish but to demonstrate to you that there are environment variables
the environment decides the variables and because the environment that this is running on is a fish shell,
I have to use the specific fish way of declaring it.
But the goal is still the same.
We're trying to inject these properties and values into node so that it has access to it.
And that's why it's called process dot env for environmental variables and we'll use them in the next
couple of videos.
I'll see in the next one.
Bye-Bye.