A production-ready Node.js + Express backend project using TypeScript with comprehensive tooling, monitoring, and code generation capabilities.
- TypeScript: Strict mode enabled with ES Modules
- Express.js: Fast, minimalist web framework
- MongoDB: Document database with Mongoose ODM
- Validation: Zod schema validation for all inputs
- Code Quality: ESLint + Prettier + Husky + Commitlint
- Testing: Jest with coverage reports
- Monitoring: Prometheus metrics + Grafana dashboards
- Code Generation: Plop.js for scaffolding components
- Docker: Hot reload enabled development environment
- Security: Helmet, CORS, rate limiting
- Node.js 18+
- npm or yarn
- Docker and Docker Compose (for containerized development)
- Git
git clone <repository-url>
cd production-nodejs-backend
npm installcp .env.example .envEdit .env with your configuration:
PORT=3000
NODE_ENV=development
MONGO_URI=mongodb://localhost:27017/production-backend
CORS_ORIGIN=*npm run prepare# Local development
npm run dev
# Docker development with hot reload
docker-compose up --build# Build the application
npm run build
# Start production server
npm start
# Or using Docker
docker build -t nodejs-backend .
docker run -p 3000:3000 nodejs-backend# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverageGenerate new API components using Plop:
npm run plopThis will prompt you to:
- Enter a router name (e.g.,
user) - Choose whether to define a schema
- If yes, define schema fields interactively
For a router named user, Plop generates:
src/components/user/
βββ routes/index.ts # Express router with validation
βββ controller/index.ts # Request handlers
βββ service/index.ts # Business logic
βββ model/index.ts # Mongoose model
βββ interface/index.ts # TypeScript interfaces
βββ validation/index.ts # Zod schemas
βββ tests/service.test.ts # Unit tests
Plus:
- Swagger documentation in
src/swagger-api-docs/user.yaml - Auto-updates
src/api.tsto include the new router
Available at http://localhost:3000/metrics
Custom Metrics:
http_requests_total- Total HTTP requestshttp_request_duration_seconds- Request duration histogramhttp_active_connections- Active connections gauge
Default Node.js Metrics:
- Memory usage
- CPU usage
- Event loop lag
- Garbage collection
When using Docker Compose:
- Grafana:
http://localhost:3001(admin/admin) - Prometheus:
http://localhost:9090
| Script | Description |
|---|---|
npm run dev |
Start development server with hot reload |
npm run build |
Build TypeScript to JavaScript |
npm start |
Start production server |
npm run lint |
Run ESLint |
npm run lint:fix |
Fix ESLint issues |
npm run format |
Format code with Prettier |
npm run format:check |
Check code formatting |
npm run type-check |
TypeScript type checking |
npm test |
Run tests |
npm run plop |
Generate new components |
βββ .github/ # GitHub templates
βββ .husky/ # Git hooks
βββ .vscode/ # VS Code settings
βββ monitoring/ # Prometheus & Grafana config
βββ plop-templates/ # Code generation templates
βββ src/
β βββ components/ # Feature modules
β β βββ <feature>/
β β βββ routes/ # Express routes
β β βββ controller/ # Request handlers
β β βββ service/ # Business logic
β β βββ model/ # Database models
β β βββ interface/ # TypeScript interfaces
β β βββ validation/ # Zod schemas
β β βββ tests/ # Unit tests
β β βββ utils/ # Feature utilities
β βββ core/ # Core utilities
β βββ middleware/ # Express middleware
β βββ utils/ # Shared utilities
β βββ db/ # Database configuration
β βββ swagger-api-docs/ # API documentation
β βββ api.ts # Route aggregation
β βββ app.ts # Express app setup
β βββ server.ts # Server entry point
βββ Dockerfile # Production container
βββ Dockerfile.dev # Development container
βββ docker-compose.yml # Development environment
βββ package.json
All API responses follow this format:
{
success: boolean;
data?: any;
status: number;
message: string;
}Success Response:
{
"success": true,
"data": { "id": "123", "name": "John" },
"status": 200,
"message": "User retrieved successfully"
}Error Response:
{
"success": false,
"status": 400,
"message": "Validation error: id is required"
}- Helmet: Security headers
- CORS: Cross-origin resource sharing
- Rate Limiting: Prevent abuse
- Input Validation: Zod schema validation
- Environment Variables: Secure configuration
This project uses Conventional Commits:
<type>(<scope>): <message>
Examples:
feat(user): add user registration endpoint
fix(auth): resolve token validation issue
docs(readme): update setup instructions
Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
docker-compose up --build- Hot reload enabled
- MongoDB included
- Monitoring stack (Prometheus + Grafana)
docker build -t nodejs-backend .
docker run -p 3000:3000 nodejs-backend- Application:
GET /health - API Status:
GET /api - Metrics:
GET /metrics
- API documentation is auto-generated in
src/swagger-api-docs/ - Each component includes OpenAPI 3.0 spec
- Access Swagger UI at
/api-docs(when implemented)
- Follow the commit convention
- Run
npm run lintandnpm run formatbefore committing - Ensure tests pass with
npm test - Add tests for new features
- Update documentation as needed
MIT License - see LICENSE file for details.