A modern, production-ready backend template built with TypeScript, Hono, and Better Auth. Features clean architecture, comprehensive error handling, and built-in observability.
- π Fast & Lightweight - Built on Hono, one of the fastest web frameworks
- π Authentication - Better Auth with email/password and OAuth support
- π Observability - Prometheus metrics, Pino logging, health checks
- π‘οΈ Security - Rate limiting, CORS, secure headers, input validation
- ποΈ Clean Architecture - Separation of concerns, dependency injection
- π API Versioning - Built-in support for versioned APIs
- β Type Safety - End-to-end TypeScript with strict checks
- π§ͺ Production Ready - Graceful shutdown, error handling, validation
- Node.js 18+
- MongoDB 4.4+
- npm or yarn
npm installCreate a .env file in the root directory:
# Server Configuration
NODE_ENV=development
PORT=3000
LOG_LEVEL=info
# Database
DATABASE_URL=mongodb://localhost:27017/your-database
# Authentication
BETTER_AUTH_SECRET=your-super-secret-key-min-32-characters
# OAuth (Optional)
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=npm run devThe server will start at http://localhost:3000
GET /- API informationGET /health- Health check with database statusGET /health/liveness- Kubernetes liveness probeGET /health/readiness- Kubernetes readiness probeGET /metrics- Prometheus metrics
POST /api/v1/validate-schema- Test endpoint with validationPOST /api/v1/auth/*- Authentication endpoints (Better Auth)GET /api/v1/examples/*- Example error handling endpoints
src/
βββ index.ts # Application entry point
βββ app.ts # Application factory
βββ server.ts # Server lifecycle management
βββ config/ # Configuration layer
β βββ auth.config.ts # Authentication setup
β βββ database.config.ts # Database connection
β βββ environment.config.ts # Environment validation
β βββ index.ts
βββ core/ # Core application logic
β βββ middlewares/ # Middleware functions
β βββ types/ # Type definitions
βββ routes/ # Route definitions
β βββ v1/ # API version 1
β βββ health.route.ts
βββ schemas/ # Validation schemas
For detailed architecture documentation, see ARCHITECTURE.md
# Development
npm run dev # Start development server with hot reload
# Production
npm run build # Build for production
npm start # Start production server
# Code Quality
npm run lint # Run linter
npm run format # Format code- Rate Limiting - 100 requests per 15 minutes per IP/token
- CORS - Configurable cross-origin policies
- Secure Headers - Protection against common vulnerabilities
- Input Validation - Zod schema validation on all inputs
- Authentication - Session-based auth with Better Auth
- Error Sanitization - Sensitive data hidden in production
Prometheus metrics are available at /metrics:
- HTTP request duration
- Request count by method/status
- Active connections
- Custom business metrics
Structured JSON logging with Pino:
- Request/response logging
- Error logging with stack traces
- Database operation logging
- Configurable log levels
/health- Overall application health/health/liveness- Is the app running?/health/readiness- Is the app ready to serve traffic?
This template uses Better Auth with:
- Email/password authentication
- Google OAuth (optional)
- Facebook OAuth (optional)
- Session management
- OpenAPI documentation
POST /api/v1/auth/sign-up- Register new userPOST /api/v1/auth/sign-in- LoginPOST /api/v1/auth/sign-out- LogoutGET /api/v1/auth/session- Get current session
All errors follow a consistent format:
{
"success": false,
"error": {
"code": "ERROR_CODE",
"message": "Human-readable error message",
"details": {}
},
"timestamp": "2025-01-01T00:00:00.000Z"
}Error types handled:
- Validation Errors - Zod schema validation failures
- HTTP Exceptions - 4xx/5xx errors with proper status codes
- Database Errors - MongoDB-specific errors
- Generic Errors - Unexpected errors with stack traces (dev only)
Ensure all required environment variables are set:
NODE_ENV- Set toproductionDATABASE_URL- MongoDB connection stringBETTER_AUTH_SECRET- Secure random string (32+ characters)PORT- Server port (default: 3000)
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]Health check configuration:
livenessProbe:
httpGet:
path: /health/liveness
port: 3000
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /health/readiness
port: 3000
initialDelaySeconds: 5
periodSeconds: 5API documentation is auto-generated via Better Auth's OpenAPI plugin. Access it at:
- Development:
http://localhost:3000/api/v1/auth/openapi
- Fork the repository
- Create a feature branch
- Make your changes
- Run linter and tests
- Submit a pull request
MIT License - feel free to use this template for your projects!
Built with:
- Hono - Fast web framework
- Better Auth - Authentication
- Zod - Schema validation
- Pino - Logging
- Prometheus - Metrics
For issues and questions, please open a GitHub issue.
Happy coding! π