DEV Community

Cover image for Continuous Deployment of a NestJS Application on Koyeb
Edouard Bonlieu for Koyeb

Posted on • Originally published at koyeb.com

Continuous Deployment of a NestJS Application on Koyeb

Introduction

NestJS is a Node.js framework to build efficient and scalable server-side applications. Nest uses and abstracts ExpressJS under the hood to ease development but keeps its API accessible to developers.
This allows you to compose and use all Express's compatible third-party modules and middlewares. In addition to Express, you can also configure Nest to use Fastify, another popular Node.js framework.
Nest is built with typescript and combines the use of object-oriented programming, functional programming, and functional reactive programming.

In this tutorial, we will create a minimalist NestJS application and showcase how to deploy the application with continuous deployment on Koyeb.
By deploying the Nest application on Koyeb using the git-driven deployment method, each time you push new changes to your GitHub repository, a new deployment will occur and be promoted once the built and health checks are completed.

Thanks to Koyeb, you will benefit from native global load-balancing across our edge network, autoscaling, automatic HTTPS (SSL), and auto-healing with zero configuration.

Requirements

To successfully follow and complete this guide, you need:

Steps

To successfully complete this tutorial and deploy the Nest application on Koyeb Serverless Platform, you need to follow these steps:

  1. Create and configure the Nest application
  2. Push the sources to GitHub
  3. Deploy the Nest app on Koyeb

Create and configure the Nest application

Installing the NestJS CLI

To get started, we need to install the Nest CLI. In your terminal run the following command:

npm i -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

Create a new Nest application

The Nest CLI installed, we can initialize a new Nest app running:

nest new nestjs-on-koyeb
Enter fullscreen mode Exit fullscreen mode

The command below created a directory nestjs-on-koyeb containing the node modules to properly run the Nest application and a few other boilerplate files.

.
├── README.md
├── nest-cli.json
├── node_modules
├── package-lock.json
├── package.json
├── src
│   ├── app.controller.spec.ts
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── test
│   ├── app.e2e-spec.ts
│   └── jest-e2e.json
├── tsconfig.build.json
└── tsconfig.json
Enter fullscreen mode Exit fullscreen mode

Configuring the Nest application

By default, the Nest listens on port 3000. There are many cases you will want the application to listen on a different port.
Replace the line below to allow setting up the port via the PORT environment variable or use the port 3000 if no environment variable is provided in the src/main.ts file.

main.ts is the entry file of the application which uses the core function NestFactory to create a Nest application instance.

PRISM_DELETED await app.listen(3000);
PRISM_INSERTED await app.listen(process.env.PORT || 3000);
Enter fullscreen mode Exit fullscreen mode

Then, open and edit the package.json file to specify the npm and node versions to use. Here we use the Node LTS version and the latest NPM version:

  ...
  "engines": {
    "node": "14.x",
    "npm": "7.x"
  },
  ...
Enter fullscreen mode Exit fullscreen mode

Push the sources to GitHub

Once the edits are performed, we can commit and push our application to a GitHub repository. When we previously ran the nest new nestjs-on-koyeb command, a git repository has been initialized so we don't need to run git init inside our application directory.

Add the Nest application sources:

git add .
git commit -m "Nest app initial commit"
Enter fullscreen mode Exit fullscreen mode

Add a new remote pointing to your GitHub repository:

git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_GITHUB_REPOSITORY>.git
Enter fullscreen mode Exit fullscreen mode

Rename the repository default branch to main running:

git branch -M main
Enter fullscreen mode Exit fullscreen mode

Push your changes to the GitHub repository:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Deploy the Nest app on Koyeb

On the Koyeb Control Panel, click the Create App button. You land on the App creation page.

  1. Select GitHub as the deployment method to use
  2. In the repositories selector, select the repository containing your Nest application sources
  3. Specify the branch to deploy, in my case I will deploy the main branch.
  4. To let Koyeb how to launch the Nest application, add npm run start:prod as the run command. This will launch the application in production mode.
  5. In the environment variables section, add a new entry with the name NODE_ENV and production as value.
  6. Then, give your App a name, i.e koyeb-nestjs-demo, and click Create App.

You land on the deployment page where you can follow the build of your Nest application. Once the build is completed, your application is being deployed and you will be able to access it via <APP_NAME>.<ORG_NAME>.koyeb.app.

If you want to learn about how Koyeb automatically builds your applications from git, make sure to read our how we build from git documentation.

In this guide, we showcased how Koyeb simplifies the deployment of a NestJS application. With minimal effort, you have a complete, production-ready environment with native global load-balancing, TLS encryption, autoscaling, autohealing, and more to run your Nest application.

Top comments (0)