Hey, I'm Marco and welcome to my newsletter!
As a software engineer, I created this newsletter to share my first-hand knowledge of the development world. Each topic we will explore will provide valuable insights, with the goal of inspiring and helping all of you on your journey.
In this episode, I will show you how I deploy my Node.js applications on Heroku, showing you all the steps I take. I have been using this platform for five years and I think it is one of the most effective solutions to deploy projects without having to deal with complicated low-level problems.
In the last episode I showed you my backend template showing how it works and how to run the server on your own computer, next step is to deploy your service in the environment designated as "production." This involves exposing the service to actual traffic and allowing it to perform all the operations that were previously tested locally.
1) 🚀 Deployment
Deployment is the process of distributing and running an application or service in an operational environment, often referred to as "production." This step is crucial in the software development life cycle, involving the transfer of the application from the development and testing phase to an environment where it is accessible to end users.
Here are some key reasons why application deployment is necessary:
Public Accessibility: Deployment allows end users to access and use the application. In a production environment, the application is made available to the public or specific users it is intended for.
Realistic Testing Conditions: Despite the importance of testing in a development or testing environment, the production environment may present real-world challenges and variables that may not emerge during development. Deployment enables testing the application in more realistic conditions.
Performance and Scalability: The production environment can handle a significant workload compared to the development environment. Deployment provides an opportunity to optimize the application's performance and scalability to ensure efficient operation, even with a high number of users or data.
Integration with Other Services: In production, the application may interact with other services, databases, or system components. Deployment includes configuring these integrations to ensure proper functioning in the operational environment.
Updates and Fixes: Deployment is essential for releasing updates, new features, or bug fixes, allowing users to benefit from the latest changes made to the application.
There are various options available for deploying applications, ranging from renting individual machines where you install and configure all the software yourself, to platforms that abstract the underlying infrastructure but still operate at a relatively low level, like AWS. On the other end, there are high-level platforms that provide a more abstracted and streamlined deployment process, like Heroku.
1) Heroku
Among the high-level deployment options, Heroku is my preferred choice. It offers a streamlined and user-friendly platform, abstracting much of the underlying infrastructure complexity and providing a seamless deployment experience.
Additionally, it provides a range of configurable add-ons, including systems for log retention, databases, caching, and many more.
I prefer Heroku over other solutions because it provides an incredibly straightforward way to deploy applications, abstracting away all the technical details that would otherwise require in-depth study and specific configurations. Thanks to this user-friendly approach, transitioning from running the software on our machine to making it operational in a production environment becomes immediate and hassle-free.
2) 💻 How to deploy our application
In this section, I'll guide you through all the steps to deploy a Node.js app on Heroku, specifically using the backend template I showcased in the previous episode:
The steps we'll take include:
Creating a Heroku account and selecting the “Eco” plan
Create an app and connect it to your GitHub project repository
Procfile
Configuring Database and Log Add-ons
Configuring environment variables
Test deployed application
1) Creating a Heroku account and selecting the “Eco” plan
Go to https://www.heroku.com/ and follow the steps to create your account. Once completed, you'll be prompted to set up Two-Factor Authentication (2FA). In my case, I used Google Authenticator.
After completing the steps, you will find yourself on this screen:
Now, we need to add our payment method by subscribing to the Eco plan.
This $5 per month plan allows us to leverage 1000 hours of code execution for all the applications we deploy. It comes with some important limitations, such as servers being put to sleep after 30 minutes of inactivity and reactivated upon the next request. I personally use this plan even for applications with real users. When desired, you can choose to upgrade to machines with higher RAM and performance at an additional cost. For testing your application and for simpler apps, the Hobby plan works just fine.
2) Create an app and connect it to your GitHub project repository
Let's create a new Heroku application:
And let's link it to our repository on GitHub. This way, we can make all our changes locally, and once pushed to GitHub, we'll be able to deploy them on the Heroku app:
Another thing I do is activate the option to automatically trigger the application deployment with every push to GitHub:
It's not mandatory; alternatively, you can manually deploy the app whenever you want to update it using the Deploy Branch button.
3) Procfile
As you may have noticed, inside the Node.js project, there is a Procfile.
web: yarn serve:production
The Procfile is a configuration file for Heroku. It specifies the processes to be executed when the application is started, such as starting the web server or running background scripts. The structure is a text file in the project's root directory, with lines specifying processes and their associated commands. This file is crucial for the proper execution of the application in the hosting environment, as platforms like Heroku read the Procfile to initiate and manage the specified processes.
Once the deployment is completed, you will see this:
4) Configuring Database and Log Add-ons
Heroku add-ons are third-party services integrated into Heroku apps for added functionalities like databases, caching, monitoring, and logging. These services, available through the Heroku marketplace, simplify the inclusion of external tools, allowing developers to enhance their applications without managing these services independently. The add-ons are easily managed through the Heroku Dashboard or CLI, streamlining development and deployment processes.
The add-ons come with various plans; some offer a free tier, while others are available only with a paid subscription.
I use Heroku Postgres for the database. It's a PostgreSQL database that costs $5 per month and allows up to 10,000 rows with a maximum storage of 1GB. If you exceed these limits, you can upgrade to the next plan at $9, which extends the limits to 10 million rows and 10GB of storage. Upgrading the plan can be done directly through the Heroku interface without compromising the database content.
For logging, I use Papertrail. I utilize the free plan, which retains logs for the last 7 days with a maximum of 10MB per day.
After configuration, you will have this:
5) Configuring environment variables
In the previous post, I mentioned creating a .env file based on env-template file:
MODE=development
NODE_ENV=production
PORT=80
DATABASE_URL=
This file is used to configure environment variables for the development environment. It's not recommended to version this file on your GitHub repository, as it may contain sensitive information such as API keys, database connection URLs, etc, in my case I add it in .gitignore file to prevent versioning.
How do we configure environment variables on our Heroku app?
We can do this by going to the “Settings” tab, then navigating to the “Config Vars” section:
As you can see, some variables are already configured, in this case, by the installed add-ons. The remaining ones can be added manually:
6) Test deployed application
You can access your deployed app by clicking the “Open App” button in the top right corner of the Heroku dashboard.
In my case, I reach it https://node-backend-template-0a553f134efa.herokuapp.com/
You can test healthcheck route: https://node-backend-template-0a553f134efa.herokuapp.com/healthcheck
If you want to test the newsletters route, first connect to the database using the url in the DATABASE_URL environment variable. Then, create the schema using:
create table _newsletters_template
(
id bigserial primary key,
description varchar(255) null,
created_at timestamp default CURRENT_TIMESTAMP not null,
updated_at timestamp default CURRENT_TIMESTAMP not null
);
and finally go to: https://node-backend-template-0a553f134efa.herokuapp.com/newsletters/1
And that’s it for today! If you are finding this newsletter valuable, consider doing any of these:
🍻 Read with your friends — Implementing lives thanks to word of mouth. Share the article with someone who would like it.
📣 Provide your feedback - We welcome your thoughts! Please share your opinions or suggestions for improving the newsletter, your input helps us adapt the content to your tastes.
I wish you a great day! ☀️
Marco