This project is a NestJS-based microservice example.
This project implements a microservice architecture using NestJS. It provides core functionalities including user management, authentication and authorization, project management, and an API gateway to orchestrate communication between services. This example serves as a robust foundation for building scalable and maintainable distributed applications.
Make sure you have the following installed on your system:
- Node.js: A recent LTS version (e.g., v18.x or v20.x). You can download it from nodejs.org.
- npm: Node Package Manager (comes with Node.js).
- Docker: (Optional, but recommended if using containerized services). Download from docker.com.
- Clone the repository:
git clone https://github.com/mahdi-vajdi/nestjs-microservice-example.git
- Navigate to the project directory:
cd nestjs-microservice-example
- Install dependencies:
npm install
In the project directory, you can run the following commands:
npm run prebuild
: Removes thedist
folder.npm run build
: Builds the application.npm run format
: Formats the code using Prettier.npm run start
: Starts the application.npm run start:dev
: Starts the application in watch mode.npm run start:debug
: Starts the application in debug mode with watch.npm run start:prod
: Starts the production build (specificallydist/apps/dashboard-ms/main
).npm run lint
: Lints the codebase using ESLint and auto-fixes issues.npm run test
: Runs Jest tests.npm run test:watch
: Runs Jest tests in watch mode.npm run test:cov
: Runs Jest tests and generates a coverage report.npm run test:debug
: Runs Jest tests in debug mode.npm run test:e2e
: Runs end-to-end tests for thedashboard-ms
application.
This project uses a monorepo structure managed with npm workspaces:
apps/
: Contains the different microservice applications.apps/auth
: Authentication and authorization service.apps/gateway
: API Gateway to route requests to appropriate microservices.apps/project
: Project management service.apps/user
: User management service.
libs/
: Contains shared libraries used across applications.libs/common
: Common utilities and modules.libs/infrastructure
: Infrastructure-related modules (e.g., database, messaging).
This project consists of the following microservices:
- Auth Service: Handles user authentication, including registration, login, and token generation (e.g., JWT). It is also responsible for authorization, managing roles and permissions to control access to various resources and services.
- Gateway Service: Acts as the single entry point for all incoming client requests. It routes requests to the
appropriate downstream microservices (
user
,project
, etc.), and can also handle cross-cutting concerns like request aggregation, SSL termination, rate limiting, and authentication/authorization checks. - Project Service: Manages all aspects of projects, including creation, retrieval, updates, and deletion of project data. It may also handle project-specific settings, tasks, or collaborations.
- User Service: Responsible for managing user-specific information, such as profiles, preferences, and other user-related data, distinct from authentication details which are handled by the Auth service.
API documentation can be generated and viewed using Swagger (OpenAPI).
Once the application (specifically the Gateway service) is running, you can typically access the Swagger UI at
http://<HOST>:<PORT>/api
.
- Building Docker images for each service.
- Using
docker-compose
for local/development deployment. - Instructions for deploying to a cloud provider (e.g., AWS, Azure, GCP) or a Kubernetes cluster.
- Environment variable configuration.]
A basic approach using Docker might involve:
- Ensure Docker is installed and running.
- Navigate to the
docker
directory (if it contains relevantDockerfile
s or adocker-compose.yml
). - Build the images:
docker-compose build
(or individualdocker build
commands). - Run the services:
docker-compose up
.
Contributions are welcome! If you'd like to contribute to this project, please follow these general guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix (
git checkout -b feature/your-feature-name
orbugfix/issue-number
). - Make your changes and commit them with clear, descriptive messages.
- Push your changes to your forked repository.
- Open a Pull Request (PR) against the main repository's
develop
ormain
branch. - Ensure your PR includes a clear description of the changes and any relevant issue numbers.
This project is released under the MIT License