Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Production-Ready Node.js Backend

A production-ready Node.js + Express backend project using TypeScript with comprehensive tooling, monitoring, and code generation capabilities.

πŸš€ Features

  • 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

πŸ“‹ Prerequisites

  • Node.js 18+
  • npm or yarn
  • Docker and Docker Compose (for containerized development)
  • Git

πŸ› οΈ Setup

1. Clone and Install

git clone <repository-url>
cd production-nodejs-backend
npm install

2. Environment Configuration

cp .env.example .env

Edit .env with your configuration:

PORT=3000
NODE_ENV=development
MONGO_URI=mongodb://localhost:27017/production-backend
CORS_ORIGIN=*

3. Setup Git Hooks

npm run prepare

πŸƒβ€β™‚οΈ Running the Application

Development Mode

# Local development
npm run dev

# Docker development with hot reload
docker-compose up --build

Production Mode

# 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

πŸ§ͺ Testing

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Generate coverage report
npm run test:coverage

🎨 Code Generation

Generate new API components using Plop:

npm run plop

This will prompt you to:

  1. Enter a router name (e.g., user)
  2. Choose whether to define a schema
  3. If yes, define schema fields interactively

Generated Structure

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.ts to include the new router

πŸ“Š Monitoring

Prometheus Metrics

Available at http://localhost:3000/metrics

Custom Metrics:

  • http_requests_total - Total HTTP requests
  • http_request_duration_seconds - Request duration histogram
  • http_active_connections - Active connections gauge

Default Node.js Metrics:

  • Memory usage
  • CPU usage
  • Event loop lag
  • Garbage collection

Grafana Dashboard

When using Docker Compose:

  • Grafana: http://localhost:3001 (admin/admin)
  • Prometheus: http://localhost:9090

πŸ”§ Scripts

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

πŸ—οΈ Project Structure

β”œβ”€β”€ .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

πŸ“ API Response Format

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"
}

πŸ”’ Security Features

  • Helmet: Security headers
  • CORS: Cross-origin resource sharing
  • Rate Limiting: Prevent abuse
  • Input Validation: Zod schema validation
  • Environment Variables: Secure configuration

🎯 Commit Convention

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

Development

docker-compose up --build
  • Hot reload enabled
  • MongoDB included
  • Monitoring stack (Prometheus + Grafana)

Production

docker build -t nodejs-backend .
docker run -p 3000:3000 nodejs-backend

🚦 Health Checks

  • Application: GET /health
  • API Status: GET /api
  • Metrics: GET /metrics

πŸ“š Documentation

  • 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)

🀝 Contributing

  1. Follow the commit convention
  2. Run npm run lint and npm run format before committing
  3. Ensure tests pass with npm test
  4. Add tests for new features
  5. Update documentation as needed

πŸ“„ License

MIT License - see LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages