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
Markdown
# 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