October 28, 2021
Édouard Bonlieu
@edouardb_
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.
To successfully follow and complete this guide, you need:
To successfully complete this tutorial and deploy the Nest application on Koyeb Serverless Platform, you need to follow these steps:
To get started, we need to install the Nest CLI. In your terminal run the following command:
npm i -g @nestjs/cli
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
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); + await app.listen(process.env.PORT || 3000);
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" }, ...
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
On the Koyeb Control Panel, click the Create App button. You land on the App creation page.
GitHub
as the deployment method to usemain
branch.npm run start:prod
as the run command. This will launch the application in production mode.NODE_ENV
and production
as value.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.
Koyeb is a developer-friendly serverless platform to deploy any apps globally.
Start for freeDeploy 2 services for free and enjoy our predictable pricing as you grow
Get up and running in 5 minutes