This project demonstrates a microservices architecture using Moleculer.js, a fast and powerful microservices framework for Node.js. It showcases how to create and connect multiple services, implement API gateways, and interact with a SQLite database.
The project consists of two main microservices:
- Auth Service
- User Service
Both services are independent and communicate through the Moleculer broker.
Port: 3001
Base URL: http://localhost:3001/api/auth
-
Login
- Method: POST
- Path:
/login
- Action:
auth.login
-
Verify Token
- Method: POST
- Path:
/verify
- Action:
auth.verify
Port: 3000
Base URL: http://localhost:3000/api
-
Register User
- Method: POST
- Path:
/user
- Action:
user.registerUser
-
Get All Users
- Method: GET
- Path:
/user
- Action:
user.getAllUsers
-
Get User by ID
- Method: GET
- Path:
/user/:id
- Action:
user.getUser
-
Remove User
- Method: DELETE
- Path:
/user/:id
- Action:
user.removeUser
- Service Creation: Each service is created using
broker.createService()
. - Action Definition: Actions are defined within each service, representing the service's capabilities.
- API Gateway: Moleculer-web is used to create API gateways for each service.
- Inter-service Communication: The Auth service calls the User service to validate credentials.
- Param Validation: Action parameters are validated using Moleculer's built-in validator.
- SQLite3 is used as the database.
- Database operations are performed within service actions.
- SQL queries are separated into a
queries.js
file for better organization.
The application is started from index.js
, which initializes the Moleculer broker and starts all services.