A modern, secure, and production-ready Node.js microservice designed for learning containerization, Kubernetes deployment, and DevOps best practices.
- Modern Node.js: Built with ES6+ modules and latest Node.js features
- Security First: Implements security headers, CORS, and follows OWASP guidelines
- Cloud Native: Designed for containerization and Kubernetes deployment
- Health Checks: Built-in health monitoring and metrics endpoints
- Comprehensive Testing: Unit tests, integration tests, and performance tests
- CI/CD Ready: GitHub Actions and GitLab CI pipelines included
- Production Ready: Proper error handling, logging, and graceful shutdowns
- Node.js >= 18.0.0
- npm >= 9.0.0
- Docker (optional)
- Kubernetes cluster (for deployment)
-
Clone the repository
git clone https://github.com/dxas90/learn-node.git cd learn-node -
Install dependencies
npm install
-
Start the development server
npm run dev
-
Test the application
curl http://localhost:3000/healthz
-
Build the Docker image
npm run docker:build
-
Run the container
npm run docker:run
-
Deploy to Kubernetes
npm run k8s:deploy
-
Check deployment status
kubectl get pods -l app=learn-node
- Development:
http://localhost:3000 - Production:
https://your-domain.com
Welcome endpoint with API information.
Response:
{
"success": true,
"data": {
"message": "Hello World! Welcome to the Learn Node.js Microservice",
"application": {
"name": "learn-node",
"version": "1.0.0",
"environment": "production"
},
"endpoints": [...]
},
"timestamp": "2024-01-01T00:00:00.000Z"
}Simple ping-pong endpoint for basic connectivity testing.
Response:
pong
Health check endpoint for monitoring and load balancers.
Response:
{
"success": true,
"data": {
"status": "healthy",
"uptime": 123.456,
"timestamp": "2024-01-01T00:00:00.000Z",
"memory": {...},
"version": "1.0.0",
"environment": "production"
}
}Detailed application and system information.
Response:
{
"success": true,
"data": {
"application": {...},
"system": {
"platform": "linux",
"arch": "x64",
"nodeVersion": "v20.0.0",
"uptime": 123.456,
"memory": {...},
"cpuUsage": {...}
},
"environment": {...}
}
}All endpoints return standardized error responses:
{
"error": true,
"message": "Error description",
"statusCode": 404,
"timestamp": "2024-01-01T00:00:00.000Z"
}Create a .env file in the root directory:
NODE_ENV=development
PORT=3000
HOST=0.0.0.0
CORS_ORIGIN=*| Script | Description |
|---|---|
npm start |
Start production server |
npm run dev |
Start development server with hot reload |
npm test |
Run test suite |
npm run test:watch |
Run tests in watch mode |
npm run test:coverage |
Run tests with coverage report |
npm run lint |
Run ESLint |
npm run lint:fix |
Fix ESLint issues |
npm run format |
Format code with Prettier |
npm run docker:build |
Build Docker image |
npm run docker:run |
Run Docker container |
npm run k8s:deploy |
Deploy to Kubernetes |
This project follows strict code quality standards:
- ESLint: JavaScript linting
- Prettier: Code formatting
- Jest: Testing framework
- Security: Helmet.js for security headers
- CORS: Cross-origin resource sharing
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage- Unit Tests: Test individual functions and modules
- Integration Tests: Test API endpoints and workflows
- Performance Tests: Test response times and concurrent requests
- Security Tests: Test security headers and error handling
Minimum test coverage thresholds:
- Branches: 80%
- Functions: 80%
- Lines: 80%
- Statements: 80%
The application uses multi-stage Docker builds for optimization:
# Development
docker build --target development -t learn-node:dev .
# Production
docker build --target production -t learn-node:prod .-
Create namespace
kubectl create namespace learn-node
-
Deploy application
kubectl apply -f k8s/ -n learn-node
-
Check status
kubectl get all -n learn-node
oc new-app https://github.com/dxas90/learn-node.gitgit push heroku mainSee deployment guides in the docs/ directory.
The application provides several monitoring endpoints:
/healthz- Kubernetes health checks/ping- Load balancer health checks/info- Detailed system information
Key metrics to monitor:
- Response Time: < 100ms for health checks
- Memory Usage: Monitor for memory leaks
- CPU Usage: Track CPU utilization
- Error Rate: Monitor 4xx/5xx responses
Structured logging with timestamps:
[2024-01-01T00:00:00.000Z] GET /healthz - User-Agent: kube-probe/1.28
We welcome contributions! Please follow these guidelines:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Make your changes
- Run tests
npm test - Commit your changes
git commit -m 'Add amazing feature' - Push to the branch
git push origin feature/amazing-feature
- Open a Pull Request
- Follow ESLint configuration
- Write tests for new features
- Update documentation
- Use conventional commit messages
- Ensure CI/CD passes
- Security headers implemented via Helmet.js
- CORS properly configured
- Input validation and sanitization
- No hardcoded secrets
- Regular dependency updates
- Container security scanning
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Node.js - JavaScript runtime
- Docker - Containerization platform
- Kubernetes - Container orchestration
- Jest - Testing framework
- GitHub Actions - CI/CD platform
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: [email protected]
Happy Coding! 🎉