This project is a microservice-based address management system built using NestJS. It includes three microservices:
- User Service: Provides user data.
- Address Management Service: Manages addresses and communicates with the user & email service.
- Email Service: Sends email notifications.
The services communicate with each other using RabbitMQ as the message broker.
Follow these steps to set up and run the project:
- Node.js (v14 or later)
- Docker
- RabbitMQ
-
Clone the app with Submodules:
To clone the repository with all its submodules, use the following command:
git clone --recurse-submodules https://github.com/A-P-Kaviraj/nestjs-address-management-system.git
-
Install Dependencies:
Navigate to each microservice's folder and install the dependencies:
cd nestjs-address-management-system-address-service npm install
cd nestjs-address-management-system-user-service npm install
cd nestjs-address-management-system-email-service npm install
-
Set Up Email Credentials:
In the
email.service.ts
file of the email service, configure your email credentials directly:import * as nodemailer from 'nodemailer; @Injectable() export class EmailService { async sendEmail(email: string, message: string) { const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'your-password', }, }); await transporter.sendMail({ from: '"NestJS App" <[email protected]>', to: email, subject: 'Address Added', text: message, }); console.log(`Email sent to ${email}`); } }
-
Start RabbitMQ:
Use Docker to run RabbitMQ:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
-
Run the Microservices:
For each microservice, run:
npm run start
To add an address, send a POST request to:
POST http://localhost:3000/address/add
Content-Type: application/json
{
"userId": 1,
"address": "Give any address you want"
}
You can use the provided request.http
file to test the Address Management Service.
Create a request.http
file:
POST http://localhost:3000/address/add
Content-Type: application/json
{
"userId": 1,
"address": "Give any address you want"
}
- Install the REST Client extension in Visual Studio Code.
- Open the
request.http
file and click "Send Request" to execute it.
You can use Postman to send the POST request by configuring the URL, method, and body as specified above.
The following dependencies are used in this project:
- NestJS: Framework for building efficient, scalable Node.js server-side applications.
- RabbitMQ: Message broker for communication between microservices.
- Nodemailer: Email sending library for Node.js.
- dotenv: Module for loading environment variables from a
.env
file (if used).
- Make sure RabbitMQ is running and accessible at
localhost:5672
. - Ensure that email credentials are correctly configured in
email.service.ts
&user.service.ts
to enable email functionality.