Deploy a NestJS Application
Nest is a progressive Node.js framework tailored for building efficient, reliable, and scalable server-side applications. By default, it leverages the Express framework but also seamlessly integrates with Fastify for improved performance and flexibility.
This guide walks you through deploying a NestJS application to CloudStation.
Let’s get started with creating a Nest application!
Set Up a NestJS Application
Note: If you already have an existing Nest application, either locally or on GitHub, skip ahead to Deploy the Nest Application to CloudStation.
To set up a new Nest application, ensure that both Node.js and NestJS CLI are installed on your system.
Use the following terminal command to create a new application:
nest new my-app
This will generate a fresh Nest application in a directory named my-app
.
Running the Application Locally
To run the application locally, execute the following command:
npm run start
Visit http://localhost:3000
in your browser to access the application.
To run the application on a different port, set the desired port with the command PORT=4000 npm run start
. The application will then be available at http://localhost:4000
.
Configure a Database
Note: This example uses PostgreSQL. If it is not installed, you can download PostgreSQL or use a different Node.js-compatible database.
-
Create a new database named
myapp_dev
. -
Install the required database packages:
npm install @nestjs/typeorm typeorm pg
typeorm
: A TypeScript and JavaScript ORM library.pg
: PostgreSQL driver for Node.js.
- Update the
src/app.module.ts
file as shown below:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'your_username',
password: 'your_password',
database: 'myapp_dev',
entities: [],
synchronize: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Run the application with npm run start:dev
. If there are any errors connecting to the database, verify the credentials and database configuration.
- Modify the
src/app.service.ts
file to return a custom message:
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Welcome to CloudStation with NestJS!';
}
}
Restart the application to see the updated message.
Prepare the Application for Deployment
Update the src/app.module.ts
file to use environment variables for database credentials:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [],
synchronize: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Using environment variables allows CloudStation to provide the necessary configuration dynamically during deployment.
Deploy the Nest App to CloudStation
This guide will walk you through deploying a Next.js application on CloudStation step-by-step.
Prerequisites
Before you start, ensure you have the following:
-
CloudStation Account: An active account on CloudStation.
-
Node.js and npm: Installed on your local machine.
-
Next.js Application Code: Your application ready for deployment.
-
Git: Installed and set up for version control.
Deploy from the CLI
Steps to Deploy
1. Log in and Create a New Project
Log into CloudStation, go to Dashboard > New Project, and Add New Git Repo. Select your Svelte project and import it.
2. Set Build and Start Commands
Define the commands that CloudStation should use to build and start your application during deployment. These commands are essential for ensuring your project compiles correctly and runs seamlessly on the server environment.
Build Command:
The command used to compile or prepare your application for production. For example,npm run build
or yarn build
.
Start Command:
The command that launches your server or application. For instance, or npm start
.
Ensure these commands are properly defined in your package.json file to avoid deployment errors.
3. Deploy the Application
Click Deploy to start the process. CloudStation handles resource allocation, installation, and app start-up.
4.Verify the Deployment
- Once the deployment completes,check if the server is running successfully.
Note: CloudStation supports also deployment from public and private Docker images.
This guide covers the main deployment options on CloudStation. Choose the approach that suits your setup, and start deploying your Nest apps seamlessly!
Conclusion
Congratulations! Your Next.js application is now successfully deployed on CloudStation. You can manage and scale your application effortlessly from the CloudStation dashboard.
Next Steps
Explore these resources to learn how you can maximize your experience with CloudStation:
Edit this file on GitHub