create-gs-express-ms
v1.0.13
Published
π Production-ready Node.js Express microservices boilerplate with MongoDB, Redis, RabbitMQ, and complete authentication
Downloads
1,455
Maintainers
Readme
Production-Ready Node.js Microservices Boilerplate
A complete, production-ready Node.js microservices architecture following 2025 best practices with advanced security, performance optimizations, and comprehensive DevOps support.
ποΈ Architecture
This boilerplate implements a microservices architecture with the following services:
- API Gateway (Port 3000) - Single entry point, routing, rate limiting
- Auth Service (Port 3001) - Authentication, authorization, JWT, OAuth
- User Service (Port 3002) - User management, CRUD operations
- Notification Service (Port 3003) - Email (SMTP) & Push notifications (FCM)
Infrastructure
- MongoDB - Primary database
- Redis - Caching, sessions, rate limiting
- RabbitMQ/BullMQ - Message queue for background jobs
β¨ Features
π Security
- β JWT Access + Refresh tokens with secure HTTP-only cookies
- β Role-Based Access Control (RBAC)
- β API Key authentication for internal services
- β Advanced security middlewares (helmet, xss-clean, hpp, cors)
- β Request sanitization (NoSQL injection, XSS)
- β Rate limiting with Redis
- β CSRF token support
- β Account locking after failed login attempts
π Performance
- β Redis caching
- β Response compression
- β PM2 cluster mode support
- β Database query optimization
- β Connection pooling
π§ Development
- β Clean MVC architecture
- β Comprehensive error handling
- β Request validation with Joi
- β Logging with Winston + Morgan
- β Hot reload with Nodemon
- β ESLint + Prettier
- β Environment-based configuration
π¬ Communication
- β Email service with Nodemailer
- β Email templates (welcome, verification, password reset)
- β Firebase Cloud Messaging (FCM) for push notifications
- β BullMQ for background job processing
- β Event-driven architecture
π§ͺ Testing
- β Jest + Supertest setup
- β Unit and integration tests
- β Test coverage reports
π Documentation
- β Swagger/OpenAPI documentation
- β Comprehensive README
- β API documentation for all routes
π³ DevOps
- β Docker + Docker Compose
- β Multi-stage Dockerfile
- β GitHub Actions CI/CD
- β PM2 ecosystem configuration
- β Health check endpoints
π Prerequisites
- Node.js >= 18.0.0
- MongoDB >= 6.0
- Redis >= 7.0
- Docker & Docker Compose (optional)
- npm or yarn
π Quick Start
Using Docker (Recommended)
# Clone the repository
git clone <repository-url>
cd nodejs-microservices-boilerplate
# Copy environment variables
cp auth-service/.env.example auth-service/.env
cp user-service/.env.example user-service/.env
cp notification-service/.env.example notification-service/.env
cp api-gateway/.env.example api-gateway/.env
# Start all services with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f
# Stop all services
docker-compose downUsing Makefile
# Install dependencies for all services
make install
# Start all services in development mode
make dev
# Run tests for all services
make test
# Lint all services
make lint
# Format code
make format
# Clean node_modules and logs
make cleanManual Setup
# Install dependencies for each service
cd shared && npm install
cd ../auth-service && npm install
cd ../user-service && npm install
cd ../notification-service && npm install
cd ../api-gateway && npm install
# Start MongoDB
mongod
# Start Redis
redis-server
# Start each service (in separate terminals)
cd auth-service && npm run dev
cd user-service && npm run dev
cd notification-service && npm run dev
cd api-gateway && npm run devπ§ Configuration
Environment Variables
Each service has its own .env file. Copy the .env.example file and update the values:
# Auth Service
cd auth-service
cp .env.example .env
# Edit .env with your configuration
# Repeat for other servicesKey Configuration
- JWT_SECRET: Change to a strong random string
- API_KEY_*: Generate unique API keys for each service
- MONGODB_URI: Your MongoDB connection string
- REDIS_HOST/PORT: Your Redis configuration
- SMTP_*: Your email provider credentials
- GOOGLE_CLIENT_ID/SECRET: For Google OAuth
- APPLE_*: For Apple OAuth
π API Documentation
Once the services are running, access the Swagger documentation:
- API Gateway: http://localhost:3000/api-docs
- Auth Service: http://localhost:3001/api-docs
- User Service: http://localhost:3002/api-docs
- Notification Service: http://localhost:3003/api-docs
π Authentication Flow
Register
POST /api/auth/register
{
"name": "John Doe",
"email": "[email protected]",
"password": "Password123"
}Login
POST /api/auth/login
{
"email": "[email protected]",
"password": "Password123"
}Refresh Token
POST /api/auth/refresh-tokens
{
"refreshToken": "your-refresh-token"
}Protected Routes
GET /api/auth/me
Headers: {
"Authorization": "Bearer your-access-token"
}π§ͺ Testing
# Run tests for all services
make test
# Run tests for a specific service
cd auth-service && npm test
# Run tests with coverage
npm test -- --coverage
# Run tests in watch mode
npm run test:watchπ¦ Project Structure
nodejs-microservices-boilerplate/
βββ api-gateway/ # API Gateway service
βββ auth-service/ # Authentication service
βββ user-service/ # User management service
βββ notification-service/ # Notification service
βββ shared/ # Shared utilities and middleware
βββ docker-compose.yml # Docker Compose configuration
βββ Makefile # Common commands
βββ README.md # This fileπ³ Docker Commands
# Build all services
docker-compose build
# Start services
docker-compose up -d
# View logs
docker-compose logs -f [service-name]
# Stop services
docker-compose down
# Remove volumes
docker-compose down -v
# Rebuild and restart
docker-compose up -d --buildπ PM2 Cluster Mode
# Start with PM2
pm2 start ecosystem.config.js
# Monitor
pm2 monit
# View logs
pm2 logs
# Restart
pm2 restart all
# Stop
pm2 stop allπ Monitoring & Logging
- Logs are stored in
logs/directory for each service - Winston logger with daily rotating files
- Morgan for HTTP request logging
- PM2 monitoring dashboard
π Security Checklist
- [x] Environment variables properly configured
- [x] Strong JWT secret
- [x] Unique API keys for each service
- [x] HTTPS enabled in production
- [x] CORS properly configured
- [x] Rate limiting enabled
- [x] Input validation on all endpoints
- [x] SQL/NoSQL injection protection
- [x] XSS protection
- [x] CSRF protection
- [x] Secure HTTP headers (helmet)
- [x] Password hashing with bcrypt
- [x] Account lockout mechanism
π Deployment
Production Checklist
- Update all
.envfiles with production values - Set
NODE_ENV=production - Use strong secrets and API keys
- Enable HTTPS
- Configure proper CORS origins
- Set up database backups
- Configure monitoring and alerting
- Review security settings
- Enable PM2 cluster mode
- Set up CI/CD pipeline
GitHub Actions
The repository includes a GitHub Actions workflow for CI/CD:
- Runs tests on push/PR
- Lints code
- Builds Docker images
- Deploys to production (configure as needed)
π€ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
π License
This project is licensed under the MIT License.
π€ Author
Gagan Saddal
π Acknowledgments
- Express.js team
- Mongoose team
- All open-source contributors
π Support
For issues and questions:
- Create an issue on GitHub
- Check the documentation
- Review existing issues
Made with β€οΈ for the Node.js community
