Okay, So now we're going to start to use environment variables.
Right now I just want to have one for the port number and also the node environment, which could be
either development or production.
So let's go ahead and stop the server for a second and we're going to install a package called dot Env
and this will allow us to basically have a file named dot env or period env and we can define our environment
variables in there.
Then when we deploy we'll actually set those environment variables on the server itself so we can install
this as a dev dependency.
So we'll do dash uppercase D again, make sure you're in your root, not your front end.
And if we look in our package.json you should now have dot env.
So what we'll do is in the root, not in the back end folder or the front end, but in the root we're
going to have a file called dot env.
And this is where we want to put any global environment variables.
So I'm going to add port and the convention is to use all uppercase and we're going to set the port
to let's do 8000 for now just to test it out.
But ultimately I do want it to be 5000, but it's completely up to you.
If you want something different, that's fine.
And then let's do the node environment.
Actually, I'm going to put that at the top.
So node underscore env and we're going to set that to development.
That way we can switch between between development and production because we might have some stuff that
will that will react different depending on the environment.
So we'll save that.
And then let's go into our server.js and in order for those to work we do have to import dot env.
So let's say import dot env from dot env and then we just have to call the config method and it's important
that you call that above where you use any of those environment variables.
So we're going to do it right under the import because I actually want to use one right here.
So we have 8000 as the environment variable.
So I'm going to set this port right here.
I'm going to set it to process dot env dot port.
Okay.
So whenever you want to access one of these variables, you just prefix it with process dot env dot
and then whatever you called it.
Now this should be 8000.
But I do want to have a fallback in case that in case this isn't found so we're going to say or 5000.
So if we run we can just do the server for now, let's say NPM run server and it should start on 8000
because that's what we have in our dot env.
All right.
But like I said, I do ultimately want this to just be 5000.
Now if I save it, it's not going to take effect unless I restart even though we're using Nodemon.
When you add or change anything in this file, you do have to restart the server.
So I'm actually going to run NPM run dev because I want to run both front end and back end.
But you should see real quick, there it is 5000, okay.
Because that's what I have in the dot env file.
All right.
So now we have our environment variables set up.
We have node on and concurrently.
So in the next video, we'll hop back into the front end so that we can actually fetch these products
and the single product from our API rather than just bringing them in from a JavaScript file.