Oct 28, 2021
5 min read

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

Create a new Nest application

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

nest new nestjs-on-koyeb

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

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.

await app.listen(3000) // [!code --]
await app.listen(process.env.PORT || 3000) // [!code ++]

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"
  },
  ...

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"

Add a new remote pointing to your GitHub repository:

git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_GITHUB_REPOSITORY>.git

Rename the repository default branch to main running:

git branch -M main

Push your changes to the GitHub repository:

git push -u origin main

Deploy the Nest app on Koyeb

On the Overview tab of the Koyeb control panel, click the Create Web Service button to begin:

  1. Choose GitHub as the deployment method.
  2. In the repositories selector, select the repository containing your Nest application sources.
  3. In the Builder section, click the Override toggle associated with the Run command and enter npm run start:prod in the field.
  4. In the Environment variables section, add a new entry with the name NODE_ENV and production as value.
  5. Choose a name for your App and Service, for example koyeb-nestjs-demo, and click Deploy.

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.

Conclusion

Want more NestJS on Koyeb? Check out our NestJS documentation to learn how to deploy a NestJS application on Koyeb.


Deploy AI apps to production in minutes

Koyeb is a developer-friendly serverless platform to deploy apps globally. No-ops, servers, or infrastructure management.
All systems operational
© Koyeb