Create and Run a NestJS Project
What is NestJS?
NestJS is a framework for building efficient and scalable Node.js server-side applications. It is written in TypeScript, inspired by Angular, adopting a modular architecture and dependency injection (DI) pattern. NestJS provides powerful tools and features that allow developers to easily build complex applications.
Key Features and Benefits of NestJS
- Modular Architecture: Organizes code through modules, making the application more structured and maintainable.
- Dependency Injection: Offers a robust DI system, enhancing code testability and maintainability.
- Uses TypeScript: Leverages TypeScript's static type checking and modern JavaScript features, improving development efficiency and code quality.
- Rich Decorators: Uses decorators to define controllers, services, modules, etc., making the code more concise and intuitive.
- Strong Ecosystem: NestJS has extensive support for third-party modules and plugins, making development more convenient.
Using NestJS can help developers quickly build high-performance web applications and APIs.
Create and Run a NestJS Project with ServBay
In this article, we will use the Node.js environment provided by ServBay to create and run a NestJS project. We will use ServBay's 'Hosting' feature to set up a web server and access the project through reverse proxy.
Create NestJS Project
Initialize Project
First, ensure that you have installed the Node.js environment provided by ServBay. Then, globally install the NestJS CLI using the following command:
bashnpm install -g @nestjs/cli
1Create a new NestJS project in the recommended root directory
/Applications/ServBay/www
:bashcd /Applications/ServBay/www nest new servbay-nest-app
1
2Follow the prompts to enter the project name (recommended as
servbay-nest-app
) and select other options as needed.Install Dependencies
Enter the project directory and install dependencies:
bashcd servbay-nest-app npm install
1
2
Modify the Output Content of the NestJS Project
Modify the
src/app.controller.ts
FileOpen the
src/app.controller.ts
file and change its content to make the webpage display "Hello ServBay!":typescriptimport { Controller, Get } from '@nestjs/common'; @Controller() export class AppController { @Get() getHello(): string { return 'Hello ServBay!'; } }
1
2
3
4
5
6
7
8
9
Enter Development Mode
Run Development Server
Start the development server and specify the port (e.g., 8585):
bashPORT=8585 npm run start
1This will start a local development server and expose port 8585.
Configure ServBay Host Reverse Proxy
Use ServBay's 'Hosting' feature and access the development server through reverse proxy. In ServBay's 'Hosting' settings, add a new reverse proxy:
- Name:
My first NestJS dev site
- Domain:
servbay-nest-test.dev
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8585
Refer to the detailed setup steps in adding a Nodejs development website.
- Name:
Access Development Mode
Open the browser and visit
https://servbay-nest-test.dev
to see the project in real-time. With ServBay's support for custom domains and free SSL certificates, you will enjoy higher security.
Deploy Production Version
Prepare Production Environment
Ensure that your project can run normally in a production environment. Generally, NestJS projects do not require special build steps, but you may need to set some environment variables or perform other configurations.
Run Production Server
Start the production server and specify the port (e.g., 8586):
bashPORT=8586 NODE_ENV=production npm run start:prod
1Configure ServBay Host Reverse Proxy
Use ServBay's 'Hosting' feature and access the production server through reverse proxy. In ServBay's 'Hosting' settings, add a new reverse proxy:
- Name:
My first NestJS production site
- Domain:
servbay-nest-test.prod
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8586
- Name:
Access Production Mode
Open the browser and visit
https://servbay-nest-test.prod
to view the production version. With ServBay's custom domain and free SSL certificates, your website will have higher security and credibility.
Database Connection
ServBay provides support for Redis, MariaDB, PostgreSQL, and MongoDB databases. Below are examples of how to connect to these databases.
Connect to MongoDB
Install
@nestjs/mongoose
andmongoose
:bashnpm install @nestjs/mongoose mongoose
1Then import and connect in the project:
typescriptimport { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ MongooseModule.forRoot('mongodb://localhost/servbay-nest-app'), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13Connect to Redis
Install
@nestjs/redis
andredis
:bashnpm install @nestjs/redis redis
1Then import and connect in the project:
typescriptimport { Module } from '@nestjs/common'; import { RedisModule } from '@nestjs/redis'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ RedisModule.forRoot({ url: 'redis://localhost:6379', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Connect to MariaDB
Install
@nestjs/typeorm
andmysql2
:bashnpm install @nestjs/typeorm mysql2
1Then import and connect in the project:
typescriptimport { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mariadb', host: 'localhost', port: 3306, username: 'root', password: 'password', database: 'servbay_nest_app', entities: [], synchronize: true, }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22Connect to PostgreSQL
Install
@nestjs/typeorm
andpg
:bashnpm install @nestjs/typeorm pg
1Then import and connect in the project:
typescriptimport { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'password', database: 'servbay_nest_app', entities: [], synchronize: true, }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
By following these steps, you have successfully created and run a NestJS project and used ServBay's features to manage and access your project while connecting to multiple databases.