Restful APIs using Express – Environment Variable

1 min read

There are ways by which we can improve our project setup in Part 2 of this series. We consider some of the improvements discussed in this section to be the best practices when building complex production web services.

Using Nodemon in Development

Recall that we always go back to the terminal to stop and restart the server, whenever we make changes to the node application files. This is a tedious process, and perhaps, there is a better way of doing this. This is the function of the Nodemon package.

Nodemon is a monitor that watches a Node application. It update the server automatically whenever changes are made to any of the Node application files. In order to install the Nodemon package globally on our computer, we use the command below.

npm i -g nodemon

For Mac users, add sudo command, in case the system permission isn’t configured properly.

sudo npm i -g nodemon

Now that we have Nodemon on our computer, there is no need of manually using the node command for executing our application again. Instead, we will use the nodemon.

nodemon index.js
using nodemon to start express server automatically in resful API development

With nodemon command, our application will restart automatically, whenever there is any change to our files. Cool right?

Using Environment Variable

Recall that our application in Part 2 listen on port 3000. This port value is an hardcoded value. We were only lucky to have it work on our development machine. Moreover, the chances of it working in production environment is very low. This is because the hosting environment dynamically assign the port number, once we deploy the application to production. In order to solve this problem, we need to make of an environment variable.

Environment variable is a variable that can influence the way a process runs. It is the part of the environment in which the process runs. Its value is set outside of the application. In other to read the value of the port environment, we make use of a process object, as shown below.

process.env.PORT || 3000

This means that if the PORT environment is configured, we will use the process.env.PORT to read the port value, otherwise we will set the port value to 3000.

Now, let’s store it in a constant called port

const port = process.env.PORT || 3000;

With our environment variable now in place, we can re-write our code in Part 2 as follows.

app.listen(port, () => console.log(`Listening on port ${port}...`));

The use of back ticks in above code block, instead of the single quote allows us to make use of template strings. The code below is the complete source code with the environment variable configuration.

In order to test the environment variable in action, we will configure the environment variable with a port value of 5000, using the export command on Mac, and set on Windows.

MAC users: export PORT=5000
Windows users: set PORT=5000

With the above configuration, the app will now listen on PORT 5000, instead of the default port 3000 when we run the application.

environment variable configuration in express web server

HTTP Put and Delete Requests – Building Restful APIs…

In order to update or delete an information, we need have the object ID. Let's analyze the code block below for handling HTTP Put...
1 min read

Validating POST Request – Building Restful APIs using Express:…

It is important to have a validation mechanism that validates inputs when building web services. Read more as we use the joi package to...
2 min read

Building Restful APIs using Express: Part 6 – POST…

In this section, we will discuss how to use http POST request to create a new radio station. We will test endpoint using the...
2 min read

2 Replies to “Restful APIs using Express – Environment Variable”

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a tutorial from us, get weekly updates in your inbox.