A robust, production-ready microservice for managing account operations built with Node.js and Express.
- Node.js - JavaScript runtime environment
- Express.js - Fast, unopinionated web framework for Node.js
- MongoDB - NoSQL document database
- Mongoose - MongoDB object modeling tool with schema validation
- Joi - Schema description language and validator for JavaScript objects
- dotenv - Environment variable management
- MVC (Model-View-Controller) - Clean separation of concerns
- RESTful API Design - Standardized API endpoints
- Microservice Architecture - Independent, focused service
accounts-microservice/
├── configs/ # Environment configuration files
├── src/
│ ├── app.js # Express application setup
│ ├── index.js # Server entry point
│ ├── config/
│ │ └── config.js # Configuration management with validation
│ ├── controllers/
│ │ └── account.js # Business logic handlers
│ ├── db/
│ │ └── index.js # Database connection management
│ ├── middlewares/
│ │ └── validate.js # Request validation middleware
│ ├── models/
│ │ └── account.js # Mongoose schema and model
│ ├── routes/
│ │ └── v1/ # API versioning
│ │ └── accounts/ # Account-specific routes
│ ├── services/
│ │ └── account.js # Business logic layer
│ └── validation/
│ └── account.js # Joi validation schemas
├── package.json
└── README.md
- Centralized configuration with environment variable validation
- Joi schema validation for configuration values
- Support for multiple environment files
- MongoDB connection with automatic retry mechanism
- Mongoose ODM with optimistic concurrency control
- Proper connection lifecycle management
- Request body and parameter validation using Joi
- MongoDB ObjectId validation
- Comprehensive validation schemas for all operations
- RESTful API endpoints
- API versioning (v1)
- Standardized error handling
- Create, read, update, and delete account operations
- Account types: root and sub accounts
- Account status management (new, active, inactive, blocked)
- Node.js (v16 or higher)
- MongoDB instance
- npm or yarn package manager
-
Clone the repository
git clone <repository-url> cd account-microservice
-
Install dependencies
npm install
-
Environment Setup Create a
.envfile in theconfigs/directory:PORT=3000 MONGODB_URL=mongodb://localhost:27017/accounts_db
-
Start the service
npm start
POST /v1/accounts- Create a new accountGET /v1/accounts/:id- Retrieve account by IDPUT /v1/accounts/:id- Update accountDELETE /v1/accounts/:id- Delete account
npm start- Start the applicationnpm test- Run tests (to be implemented)
- Consistent code structure following MVC pattern
- Input validation on all endpoints
- Proper error handling and logging
- Environment-based configuration
- Input validation and sanitization
- MongoDB injection protection through Mongoose
- Environment variable management
- Proper error handling without information leakage
- Server startup and shutdown logging
- Database connection status logging
- Unhandled error logging
- Graceful server shutdown handling
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the ISC License.
For support and questions, please open an issue in the repository or contact the development team.
Built with ❤️ using Node.js, Express, and MongoDB