UNPKG

node-backend-generator

Version:

šŸš€ CLI tool to generate professional Node.js backend templates with best practices, multiple databases, authentication, and production-ready features

260 lines (190 loc) • 5.59 kB
# my-backend A microservices-based Node.js backend application ## šŸ—ļø Architecture This project follows a microservices architecture with the following components: ### Core Services - **API Gateway** - Single entry point for all client requests - **Service Discovery** - Dynamic service registration and discovery - **Shared Library** - Common utilities, middleware, and constants ### Business Services - **Auth Service** - User authentication and authorization - **User Service** - User management and profiles - **Notification Service** - Email and push notifications - **File Service** - File upload and management - **Payment Service** - Payment processing with Stripe - **Analytics Service** - Event tracking and analytics ### Infrastructure - **MongoDB** - Primary database - **Redis** - Caching and message queue - **Docker** - Containerization and orchestration ## šŸš€ Quick Start ### Prerequisites - Node.js 18.0.0 or higher - Docker and Docker Compose - MongoDB (if running locally) - Redis (if running locally) ### Development 1. **Clone and install dependencies:** ```bash git clone <repository-url> cd my-backend npm install ``` 2. **Start all services with Docker:** ```bash npm run start:dev ``` 3. **Or start services individually:** ```bash npm run dev ``` ### Production 1. **Build and start production services:** ```bash npm run build npm run start:prod ``` ## šŸ“ Project Structure ``` my-backend/ ā”œā”€ā”€ api-gateway/ # API Gateway service ā”œā”€ā”€ service-discovery/ # Service Discovery service ā”œā”€ā”€ services/ # Business microservices │ ā”œā”€ā”€ auth-service/ │ ā”œā”€ā”€ user-service/ │ ā”œā”€ā”€ notification-service/ │ ā”œā”€ā”€ file-service/ │ ā”œā”€ā”€ payment-service/ │ └── analytics-service/ ā”œā”€ā”€ shared/ # Shared utilities and middleware ā”œā”€ā”€ docker/ # Docker configuration │ ā”œā”€ā”€ docker-compose.yml │ ā”œā”€ā”€ docker-compose.dev.yml │ ā”œā”€ā”€ docker-compose.prod.yml │ └── scripts/ ā”œā”€ā”€ kubernetes/ # Kubernetes manifests (optional) └── docs/ # Documentation ``` ## šŸ”§ Configuration ### Environment Variables Copy the example environment files and update with your values: ```bash cp docker/.env.example docker/.env cp docker/.env.development docker/.env.dev ``` Key environment variables: - `JWT_ACCESS_SECRET` - JWT token secret - `MONGODB_URI` - MongoDB connection string - `REDIS_URL` - Redis connection string - `STRIPE_SECRET_KEY` - Stripe API key ### Service Ports | Service | Port | Description | |---------|------|-------------| | API Gateway | 3000 | Main entry point | | Service Discovery | 8500 | Service registry | | Auth Service | 3001 | Authentication | | User Service | 3002 | User management | | Notification Service | 3003 | Notifications | | File Service | 3004 | File handling | | Payment Service | 3005 | Payments | | Analytics Service | 3006 | Analytics | ## šŸ› ļø Development ### Running Tests ```bash # Run all tests npm test # Run tests for specific service cd services/auth-service && npm test ``` ### Code Quality ```bash # Lint code npm run lint # Format code npm run format ``` ### Database Management ```bash # Reset databases npm run db:reset # Access MongoDB (via Docker) docker exec -it my-backend-mongodb-1 mongosh # Access Redis (via Docker) docker exec -it my-backend-redis-1 redis-cli ``` ## šŸ“Š Monitoring ### Health Checks ```bash # Check service health npm run health-check # View logs npm run logs ``` ### Web Interfaces - **Redis Commander**: http://localhost:8081 - **Mongo Express**: http://localhost:8082 ## šŸ” API Documentation ### Authentication Most endpoints require JWT authentication. Include the token in the Authorization header: ```http Authorization: Bearer <your-jwt-token> ``` ### Example API Calls ```bash # Register user curl -X POST http://localhost:3000/api/auth/register \ -H "Content-Type: application/json" \ -d '{ "name": "John Doe", "email": "john@example.com", "password": "securepassword" }' # Login curl -X POST http://localhost:3000/api/auth/login \ -H "Content-Type: application/json" \ -d '{ "email": "john@example.com", "password": "securepassword" }' ``` ## 🐳 Docker ### Development ```bash # Start development environment docker-compose -f docker/docker-compose.dev.yml up -d # View development logs docker-compose -f docker/docker-compose.dev.yml logs -f ``` ### Production ```bash # Build and start production docker-compose -f docker/docker-compose.prod.yml up -d --build # Scale services docker-compose -f docker/docker-compose.prod.yml up -d --scale auth-service=3 --scale user-service=2 ``` ## 🚢 Deployment ### Docker Swarm ```bash # Initialize swarm docker swarm init # Deploy stack docker stack deploy -c docker/docker-compose.prod.yml my-backend ``` ### Kubernetes See `kubernetes/` directory for manifests. ## šŸ¤ Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Add tests 5. Submit a pull request ## šŸ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## šŸ†˜ Support If you encounter any issues: 1. Check the service logs: `npm run logs` 2. Verify environment variables 3. Check Docker container status 4. Open an issue on GitHub --- Built with ā¤ļø using Node.js and Microservices Architecture