Skip to content

Ruffiano/nx-nestjs-boilerplate-server

Repository files navigation

Nx-Nestjs-Boilerplate-Server

Initial Setup

1. Install Nx CLI

If you haven't installed the Nx CLI globally yet, run the following command:

npm install -g nx

2. Create a New Nx Workspace

If you don't already have a workspace, create one using:

npx create-nx-workspace@latest microservices-project
cd microservices-project

Open apps directory:

cd apps

Then, generate a new application:

nx generate @nx/node:application api-gateway --docker

You can check the structure of your new workspace using:

tree -I node_modules

3. Add NestJS Dependency Packages

To set up NestJS in your project, install the following dependencies:

dependencies

npm install @nestjs/common @nestjs/core @nestjs/microservices @nestjs/platform-express rxjs reflect-metadata

Install the development dependencies:

devDependencies

npm install --save-dev @nestjs/cli @nestjs/schematics @nestjs/testing @nx/nest

Adding New Services to the Project

4. Generate a New Service Workspace

To generate a new service like api-gateway:

nx generate @nx/node:application api-gateway --docker

5. Generate Modules, Controllers, and Services

Install the NestJS plugin for Nx:

npm install @nrwl/nest --save-dev

Then, navigate to your application directory:

cd apps/api-gateway

Generate a pool module:

nx g @nrwl/nest:module pool --project=api-gateway

Generate the pool controller:

nx g @nrwl/nest:controller pool --project=api-gateway

Generate the pool service:

nx g @nrwl/nest:service pool --project=api-gateway

Generate the app.module file

To unify all modules, generate an app module:

cd apps/api-gateway/src/app
nx g @nrwl/nest:module app

To make the library buildable, use the following command:

Generate a new library/packages:

nx g @nx/nest:lib libs/my-nest-lib --buildable

Running the Application

To start the development server, run:

npx nx serve api.gateway

Building for Production

To build the application for production, run:

npx nx build api.gateway

Running Nx Tasks

You can use Nx to run various tasks for your applications.

Run a Single Target:

npx nx <target> <project> <...options>

Run Multiple Targets:

npx nx run-many -t <target1> <target2>

You can also filter the projects using -p:

npx nx run-many -t <target1> <target2> -p <proj1> <proj2>

Docker Commands

Start Docker Containers

To start and build Docker containers:

make docker-up

Stop Docker Containers

To stop Docker containers:

make docker-down

Kill and Remove Docker Resources

To stop and remove all Docker containers, images, volumes, and networks:

make kill-dockers

Generate gRPC Code

To generate gRPC code from .proto files:

make grpc-build

PostgreSQL Database Commands

Drop the PostgreSQL Database:

make dropdb

Create the PostgreSQL Database:

make createdb

Managing Ports

List Running Processes on Specific Ports:

make list-ports

Kill Processes Running on Specific Ports:

make kill-ports

Kafka Listening

To listen to a Kafka topic:

make kcat TOPIC=<topic-name>

CI Setup

Nx Cloud

  • Set up Nx Cloud to share your workspace's cache and distribute tasks across multiple machines.

Nx Graph

To explore the project graph and task dependencies:

nx graph

Makefile Commands Overview

This project includes several Makefile commands to streamline common tasks.

Available Commands:

Command Description
make run-all Run all projects using the Nx CLI
make run PROJECT=<name> Run a single project using the Nx CLI
make grpc-build Generate gRPC code from .proto files
make docker-up Run Docker Compose up
make docker-down Run Docker Compose down
make kill-dockers Kill and remove all Docker resources
make nx-reset Reset Nx cache and daemon
make kill PORT=<number> Kill the process running on the specified port
make dropdb Drop the PostgreSQL database
make createdb Create the PostgreSQL database
make kcat TOPIC=<name> Listen to a Kafka topic using kcat
make tree Show the project structure
make generate-schema SCHEMA=<name> Generate and apply schema from DBML
make dbdocs-build SCHEMA=<name> Generate DBML documentation
make list-ports List processes running on the specified ports
make kill-ports Kill processes running on the specified ports

Finish your CI setup

Click here to finish setting up your workspace!

Run tasks

To run the dev server for your app, use:

npx nx serve api-gateway

To create a production bundle:

npx nx build api-gateway

To see all available targets to run for a project, run:

npx nx show project api-gateway

These targets are either inferred automatically or defined in the project.json or package.json files.

More about running tasks in the docs »

Add new projects

While you could add new projects to your workspace manually, you might want to leverage Nx plugins and their code generation feature.

Use the plugin's generator to create new projects.

To generate a new application, use:

npx nx g @nx/node:app demo

To generate a new library, use:

npx nx g @nx/node:lib mylib

You can use npx nx list to get a list of installed plugins. Then, run npx nx list <plugin-name> to learn about more specific capabilities of a particular plugin. Alternatively, install Nx Console to browse plugins and generators in your IDE.

Learn more about Nx plugins » | Browse the plugin registry »

Learn more about Nx on CI

Install Nx Console

Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ.

Install Nx Console »

This project is designed to run on Linux Ubuntu. Please give it a star ⭐ if you find it helpful!

About

Nestjs Microservice based on Nx

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published