Skip to main content

Nest

Nest is an enterprise-grade backend framework based on TypeScript/Node.js, featuring a modular design that combines OOP (Object-Oriented Programming), FP (Functional Programming), and microservices architecture. It provides underlying support for Express/Fastify and offers features such as dependency injection, decorator-based routing, and GraphQL integration. Well-suited for building efficient and maintainable server-side applications, it aligns particularly well with full-stack TypeScript development.

This guide describes how to deploy the sample Nest application on Tencent CloudBase through multiple approaches:

Create a Nest Application

Note: If you already have a Nest app locally, you can skip this step.

To create a Nest application, ensure that Node and NestJS are installed on your machine.

Run the following command in the terminal to create a new Nest application:

nest new cloudrun-nest

A new Nest application Nest will be created for you in the directory.

Run the Nest Application Locally

Start the application locally by running the following command:

npm run start

Start the browser and access http://localhost:3000 to view the returned results.

Configuring Dockerfile

Create a Dockerfile file in the root directory of the Nest application with the following content:

FROM alpine:latest

# Install dependencies. If you need additional dependencies, search in the alpine package management (https://pkgs.alpinelinux.org/packages?name=php8*imagick*&branch=v3.13).
# Use a domestic mirror source to improve download speed.
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories \
&& apk add --update --no-cache nodejs npm

# The container's default time zone is UTC. To use Shanghai time, enable the following time zone setting command.
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates

## Specify the Working Directory
WORKDIR /app

# Copy the package management file
COPY package*.json /app/

# Use a domestic npm mirror to improve download speed.
RUN npm config set registry https://mirrors.cloud.tencent.com/npm/
# RUN npm config set registry https://registry.npm.taobao.org/

# npm install dependencies
RUN npm install

# Copy all files from the current directory (where the dockerfile is located) to the working directory (excluding files specified in .dockerignore)
COPY . /app

# Execute the startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["npm", "start"]

With the above changes, your nestjs application will be deployable to Tencent Cloud Hosting!

Deployment to Cloud Hosting

Cloud Hosting provides multiple deployment methods to deploy your application:

Deploy via Console

Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> choose the cloudrun-nest directory for upload -> enter 3000 for the port -> click Create and wait for the creation to complete.

Deploying via cli

If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy -p 3000

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.

Besides manual deployment, you can also install the aforementioned applications with one click:

One-Click Deployment from Template

One-Click Deployment from github