Creating and Running a NestJS Project Locally on macOS Using ServBay
NestJS is a progressive Node.js framework for building efficient and scalable server-side applications. Written in TypeScript and inspired by Angular, it adopts a modular architecture and dependency injection (DI) pattern. NestJS offers powerful tools and features, enabling developers to build maintainable, testable, and highly decoupled complex applications with ease.
Key Features and Advantages of NestJS
- Modular Architecture: Organizes code using modules, making applications more structured, understandable, and maintainable.
- Dependency Injection: Provides a robust and easy-to-use DI system that greatly enhances code testability and maintainability.
- Built with TypeScript: Leverages TypeScript’s static type checking, interfaces, and modern JavaScript features to improve development efficiency and code quality while reducing runtime errors.
- Rich Decorators: Uses decorators to define controllers, services, modules, etc., resulting in concise, declarative, and purposeful code.
- Thriving Ecosystem: Backed by an active community and a wide array of third-party modules and plugins, easily integrates with TypeORM, Mongoose, GraphQL, WebSockets, caching, validation, and more.
- Standards-Based: Built atop mature HTTP server frameworks such as Express or Fastify, delivering excellent compatibility and performance.
With NestJS, developers can embrace best backend development practices (such as SOLID principles and design patterns) to rapidly build high-performance, enterprise-grade web applications, API services, microservices, and more.
Creating and Running a NestJS Project Using ServBay
ServBay delivers a robust, integrated local web development environment for macOS, bundling various versions of Node.js and common databases and web servers. In this guide, you will leverage ServBay’s Node.js environment and the “Websites” feature to create, run, and configure a NestJS project. ServBay's reverse proxy functionality enables you to access your local NestJS app via a custom domain name.
Prerequisites
Before you begin, ensure you have completed the following steps:
- Install ServBay: ServBay should already be installed on your macOS machine.
- Enable Node.js Package: In ServBay’s control panel under the “Packages” tab, ensure you have selected and installed the required version of Node.js. If Node.js is not yet enabled, refer to the ServBay Node.js Guide for setup instructions.
Creating a NestJS Project
Install NestJS CLI Globally
Open your terminal and install the NestJS Command Line Interface (CLI) globally using npm (npm is typically bundled with Node.js). The NestJS CLI is a powerful tool for initializing, developing, and maintaining NestJS applications.
npm install -g @nestjs/cli
1Initialize the Project
Switch to the ServBay recommended website root directory
/Applications/ServBay/www
. This is ServBay’s default location for website projects, making subsequent configuration easier. Use the NestJS CLI to create a new project:cd /Applications/ServBay/www nest new servbay-nest-app
1
2After executing this command, the NestJS CLI will guide you through project creation, including the choice of package manager (npm, yarn, or pnpm). It is recommended to select npm for consistency. The project will be created at
/Applications/ServBay/www/servbay-nest-app
.Install Project Dependencies
Enter the newly created project directory
servbay-nest-app
and install all required dependencies:cd servbay-nest-app npm install
1
2The
nest new
command usually runsnpm install
automatically, but running it again ensures all dependencies are correctly installed or updated.
Modifying the NestJS Output (Optional)
To verify that your project is running and can be accessed via ServBay, you may wish to update the default root route response.
Edit the
src/app.controller.ts
FileOpen the
src/app.controller.ts
file in your preferred code editor. Modify its content as follows to return "Hello ServBay!" when accessing the root (/
) path:typescriptimport { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; // Assuming you’re keeping AppService @Controller() export class AppController { constructor(private readonly appService: AppService) {} // If keeping AppService @Get() getHello(): string { // return this.appService.getHello(); // If using AppService return 'Hello ServBay!'; // Returns the string directly } }
1
2
3
4
5
6
7
8
9
10
11
12
13This code defines a simple controller that handles HTTP GET requests to the application's root path (
/
) and returns the specified string.
Running the NestJS Project in Development Mode and Accessing via ServBay
NestJS projects are typically run in development mode using a built-in server that listens on a specific port. You can then use ServBay’s “Websites” feature to set up a reverse proxy, pointing a custom domain to the port your NestJS process listens on.
Start the Development Server
In the project root directory
/Applications/ServBay/www/servbay-nest-app
, run the following command to start the NestJS development server. Set thePORT
environment variable to specify the listening port, for example,8585
.cd /Applications/ServBay/www/servbay-nest-app PORT=8585 npm run start:dev
1
2The
npm run start:dev
script is predefined in NestJS projects; it runs your TypeScript code usingts-node
and enables file watching for auto-restarts upon code changes. After starting, the server listens atlocalhost:8585
.Configure ServBay Website Reverse Proxy
Open the ServBay control panel and navigate to the “Websites” tab. Click the add button (usually a
+
) to create a new website configuration:- Name: Give your site an easily identifiable name, e.g.,
My first NestJS dev site
. - Domains: Enter the local domain for accessing your project, such as
servbay-nest-test.dev
. ServBay will resolve.dev
and similar top-level domains to localhost by default; no need to modify your hosts file. - Type: Select
Reverse Proxy
. - IP Address: Enter the Node.js app’s listening IP address—typically
127.0.0.1
(localhost). - Port: Enter the NestJS app’s listening port (
8585
).
Save your settings when done. ServBay automatically updates its web server configuration (Caddy or Nginx). For additional details about ServBay website configuration, refer to the ServBay Adding Website Documentation.
- Name: Give your site an easily identifiable name, e.g.,
Access Your Development Site
Open your web browser and navigate to your configured domain,
https://servbay-nest-test.dev
.Notes:
- ServBay provides free SSL certificates (issued by ServBay User CA) for all local sites configured through it, enabling secure
https
access. If you see a certificate warning, you may need to trust the ServBay User CA certificate. See Using SSL to Secure Your Website for more information. - Ensure your NestJS development server (
PORT=8585 npm run start:dev
) is running. If stopped, your domain will not be accessible.
- ServBay provides free SSL certificates (issued by ServBay User CA) for all local sites configured through it, enabling secure
Deploying and Accessing the Production Version via ServBay
Before deploying your NestJS application to production (even for a local simulated production environment), you’ll typically need to build the project.
Build the Production Version
In the project root
/Applications/ServBay/www/servbay-nest-app
, run:cd /Applications/ServBay/www/servbay-nest-app npm run build
1
2This command uses the TypeScript compiler to compile your source code into the
dist
directory.Run the Production Server
After building, start the production version of your NestJS app using the following command. Typically, you’ll use
node
to run the compiled JavaScript. Again, specify a port such as8586
and set theNODE_ENV=production
environment variable.cd /Applications/ServBay/www/servbay-nest-app PORT=8586 NODE_ENV=production node dist/main.js
1
2In production mode, file watching and hot reloading are usually disabled.
Configure ServBay Website Reverse Proxy
Return to the “Websites” tab in the ServBay control panel and add or update a website configuration for accessing your production version:
- Name: For example,
My first NestJS production site
. - Domains: For example,
servbay-nest-test.prod
. - Type:
Reverse Proxy
. - IP Address:
127.0.0.1
. - Port:
8586
(matching your production server port).
Save your changes.
- Name: For example,
Access Your Production Site
Open your browser and visit
https://servbay-nest-test.prod
to view the production build of your NestJS app. ServBay will also provide SSL encryption.
Connecting to Databases Provided by ServBay
ServBay comes with built-in support for various databases, including MariaDB (MySQL-compatible), PostgreSQL, MongoDB, and Redis. When developing NestJS applications, you can conveniently connect to and use these databases.
Important: Before attempting to connect to a database, make sure you have enabled the corresponding database packages in the “Packages” tab of the ServBay control panel. You can find default ports, usernames, and password info for databases within the ServBay control panel or related documentation. For production environments, it is highly recommended to change default credentials and create dedicated database users.
Below are sample configuration snippets for connecting to ServBay databases using commonly used ORMs/libraries in NestJS. These examples are usually placed in your app’s root module (AppModule
) or a dedicated database module.
Connecting to MongoDB
Use the
@nestjs/mongoose
module to connect to MongoDB. First, install the required packages:npm install @nestjs/mongoose mongoose
1Then, configure
MongooseModule
in your NestJS module:typescriptimport { Module } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; // ... other imports @Module({ imports: [ MongooseModule.forRoot('mongodb://localhost:27017/servbay-nest-app'), // Default port 27017 // ... other modules ], controllers: [], // ... providers: [], // ... }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13The default MongoDB connection string in ServBay is typically
mongodb://localhost:27017/
. You may need to use a MongoDB client (such as MongoDB Compass ormongosh
) to connect tolocalhost:27017
and create theservbay-nest-app
database.Connecting to Redis
Use the
@nestjs/redis
module to connect to Redis. First, install the required packages:npm install @nestjs/redis redis @types/redis
1Then, configure
RedisModule
in your NestJS module:typescriptimport { Module } from '@nestjs/common'; import { RedisModule } from '@nestjs/redis'; // ... other imports @Module({ imports: [ RedisModule.forRoot({ url: 'redis://localhost:6379', // Default port 6379 }), // ... other modules ], controllers: [], // ... providers: [], // ... }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15The default Redis server address in ServBay is
localhost:6379
.Connecting to MariaDB (MySQL-compatible)
Use the
@nestjs/typeorm
module to connect to MariaDB. ServBay supports both MariaDB and MySQL packages; both are compatible with the MySQL protocol. TypeORM’smysql
ormariadb
types can connect to the MariaDB/MySQL instance provided by ServBay. Install the required packages:npm install @nestjs/typeorm mysql2 typeorm
1Then, configure
TypeOrmModule
in your NestJS module:typescriptimport { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; // ... other imports @Module({ imports: [ TypeOrmModule.forRoot({ type: 'mariadb', // or 'mysql' host: 'localhost', port: 3306, // Default port 3306 username: 'root', // ServBay MariaDB/MySQL default root user password: 'password', // ServBay default root password database: 'servbay_nest_app', // Create this database first entities: [], // Your entities array synchronize: true, // True in development; use with caution in production }), // ... other modules ], controllers: [], // ... providers: [], // ... }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22Note: The default
root
user password for ServBay MariaDB/MySQL ispassword
. Use a database client (like TablePlus, DBeaver, or MySQL CLI) to connect tolocalhost:3306
, login asroot
/password
, and manually create theservbay_nest_app
database. For security, it is strongly recommended to change theroot
password and create a dedicated database user with minimal privileges for your application.Connecting to PostgreSQL
Use the
@nestjs/typeorm
module to connect to PostgreSQL. First, install the required packages:npm install @nestjs/typeorm pg typeorm
1Then, configure
TypeOrmModule
in your NestJS module:typescriptimport { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; // ... other imports @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, // Default port 5432 username: 'servbay', // Example default ServBay PostgreSQL user; verify actual credentials password: 'password', // Example default ServBay PostgreSQL password; verify actual credentials database: 'servbay_nest_app', // Create this database first entities: [], // Your entities array synchronize: true, // True in development; use with caution in production }), // ... other modules ], controllers: [], // ... providers: [], // ... }) export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22Note: The default username and password for ServBay PostgreSQL may vary depending on the version. Common defaults are
servbay
orpostgres
. Check the ServBay control panel or documentation for accurate credentials, and use a PostgreSQL client to connect tolocalhost:5432
to create theservbay_nest_app
database and any required users. For security, always change default credentials in production.
Summary
This guide has shown you how to quickly create, run, and access a NestJS project locally on macOS using ServBay. With ServBay’s bundled Node.js environment, convenient website configuration (especially reverse proxy support), and built-in support for multiple databases, you can streamline the local development and testing workflow for NestJS applications. Enjoy seamless switching between development and production modes, secure access via custom domains and HTTPS, and effortless connection to powerful database services provided by ServBay.