Node.js is a powerful tool for building fast, scalable APIs. Heroku makes deployment easy, turning our local project into a live, running application in minutes. In this post, we will deploy a simple Node.js REST API to Heroku from an Ubuntu server.
And that will mean that our development environment and our production environment can be synchronized. Heroku has support for a bunch of different versions.
So if you want to use a specific version of Node.js, we can add the following to the package.json file:
And that will mean that our development environment and our production environment can be synchronized
A Procfile in Heroku is a plain text file that defines the processes to run in a Heroku application. It specifies how to start the app, typically using
If you need to do everything from the terminal then you can provide credentials with the command
That will prompt you for the username and password. If your account is protected by MFA it will fail. Use the API key of your account as your password.
Where:
If your default branch is
At the end of the deployment the output of the terminal will tell us the URL where the app has been deployed. Copy it, we’ll use it to test the app
If not, use the URL we copied in the previous step and use curl:
Your API is now live! 🎉
Prerequisites
Before we begin, ensure that:- Node.js and npm are installed.
- Git is installed.
- A Heroku account is created.
- The Heroku CLI is installed.
Create a REST API in Node.js
If you already have an API in Node.js skip this step. If you don’t have one, check out this post on How to create a REST API in NodeJS to have an API for this testDeploy to Heroku
We need to make a few changes before deploying to Heroku.One of the changes we need to make is, in the package.json file of our app, to tell Heroku exactly what we want it to run on, because in this case we're using Node.js and we can give it the version of Node that we want to run. So if you want to use a specific version of Node, you can put it in here.
And that will mean that our development environment and our production environment can be synchronized. Heroku has support for a bunch of different versions.
So if you want to use a specific version of Node.js, we can add the following to the package.json file:
{
...
"engines": "22.x"
}
Add Procfile
Ensure the app runs on Heroku by adding aProcfile
in the root directory:echo "web: node server.js" > Procfile
web: node server.js
for a Node.js app. The Procfile is essential because it tells Heroku how to launch the application and ensures the correct process runs in the dyno environment.The name web is important because it declares that this single process type attaches to Heroku’s HTTP routing stack and receives web traffic when deployed
Initialize Git
Run the following commands to set up Git:git init
git add .
git commit -m "Initial commit"
Login to Heroku
Login to your Heroku account with the commandheroku login
heroku login -i
Create a Heroku app
Create a new Heroku app:heroku create hello-nodejs-api --region eu
hello-nodejs-api
is the unique app name.--region eu
- Specifies the region for deployment (i.e. eu, us)
When we create a heroku app with this command, we also create a Git remote called heroku that is associated with our local Git repository.
Deploy the code
Push your git repository to heroku and heroku will deploy the code for usgit push heroku main
master
, use:git push heroku master
Open the app
If you have available the web browser you can directly open the app with the command:heroku open
curl https://[YOUR_APP_DNS_NAME]/hello
Tags
Heroku