Deploy a Node Express Application to Production7 min
Koyeb provides developers the fastest way to deploy full stack applications and APIs globally. Want do deploy your Node.js application globally in minutes? Sign up today and deploy 2 services for free forever!
In this guide, we will explain how to deploy a Node.js Express application to production. We will write a simple Node.js API using the Express framework, see how to Dockerize the application, version it on GitHub, and create a GitHub Actions workflow to perform the CI/CD and deploy the application on Koyeb each time you push changes on a specific branch.
By deploying the Node app on Koyeb, you benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.
To successfully follow and complete this guide, you need:
- Docker installed on your machine
- A Koyeb account to deploy and run the Node.js Express application
- The Koyeb CLI installed to interact with Koyeb from the command line
- Have configured Docker for use with the GitHub container registry
- A GitHub account with an empty repository we will use to version the application and perform the CI/CD with GitHub Actions
To deploy a Node Express Application to Production, you need to follow these steps:
- Create a simple Node.js API using the Express app or use an existing one
- Write the Dockerfile and build the Docker image
- Push the Docker image to the GitHub container registry
- Deploy the Dockerized Node.js app on Koyeb
- Automate deployment with GitHub Actions
Create a simple Node.js API using the Express framework application or use an existing one
If you already have an existing Node.js application you want to dockerize, you can jump to the next step.
Create a new directory to store our Node.js application:
The next step is to create and initialize the package.json file. The package.json contains various metadata and gives npm information to identify the project, handles the project's dependencies, etc.
In your terminal run the following command and complete as below:
As our application uses the Express framework, we need to add it as a dependency of our project. In your terminal, run:
The project environment ready, we can now start writing our application. In this guide, we create a basic application that will return the port and git commit id the application is using on requests received to the root URL
/. All other routes will respond with a 404 error.
Create and open a file named
app.js and copy the content below:
Launch the app running
node app.js and request the
/ endpoint running:
The Node app responds with the port the application is listening to "3000" and commit_id set at "unknown" for the moment.
Write the Dockerfile and build the Docker image
To Dockerize our Node.js app, you need to create a
Dockerfile in your project folder containing the content below.
To build and properly tag the Docker image execute the following command:
Once the build is over, you can run a container using the image locally to validate everything is working as expected running:
As in the previous step, you can perform a curl request to ensure the app is running as expected:
Push the Docker image to the GitHub container registry
With our image built, we can now push it to the GitHub container registry. We will then use this image to deploy the application on Koyeb.
Within a few minutes, you will see your Docker image available on the GitHub container registry:
Deploy the Dockerized Node.js app on Koyeb
To deploy our dockerized Node.js application on Koyeb, start by creating a Koyeb Secret to store your Github container registry configuration.
<REPLACE_ME_WITH_GH_USERNAME> with your GitHub username and
<REPLACE_ME_WITH_GH_TOKEN> with a valid GitHub token having registry read/write permissions and execute the command below.
We can now deploy the Node.js application on Koyeb Serverless Platform running:
Within a few minutes, your application will be live and accessible at
Automate deployment with GitHub Actions
In the previous steps, we discovered how to dockerize and deploy a Node.js application on Koyeb.
In this section, we will see how to automate the deployment of ou application each time a change is pushed to the branch
main of your repository using GitHub Actions.
In your git repository, create a folder to store our GitHub Actions workflow:
Create a new file named
workflow.yaml inside the directory we created in the previous step and paste the snippet below:
In your GitHub repository settings, click Secrets in the left-side menu and create new secrets:
GHCR_TOKENcontaining a valid GitHub token having registry read/write permissions to push the image to the registry.
KOYEB_TOKENcontaining a valid Koyeb token to redeploy the application.
Commit and push your GitHub actions workflow, your GitHub Actions workflow is being executed. Each time a change is pushed on the
main branch, a Docker image is built and push to the GitHub registry with the tag
prod. Once the image pushed,
a new deployment is triggered and deployed on the Koyeb Serverless platform.
In this guide, we explained how to containerize a Node.js application and deploy it on Koyeb. We created a GitHub Actions workflow to build and deploy the application each time a change occurs. By deploying on Koyeb, your application is secured with native TLS encryption and benefits from all the Koyeb Serverless features including autoscaling, auto-healing, and a high-performance edge network.
If you would like to read more Koyeb tutorials, checkout out our tutorials collection. Have an idea for a tutorial you'd like us to cover? Let us know by joining the conversation over on the Koyeb community platform!