@tjoc/trivia-api
v1.0.1
Published
Trivia game API backend with Express and TypeScript
Maintainers
Readme
Trivia API Backend
A robust Express.js API backend for the TJOC Trivia application, built with TypeScript and following enterprise-grade patterns.
🚀 Quick Start
Prerequisites
- Node.js 18+
- PostgreSQL 14+
- Redis 6+
- pnpm package manager
Installation
- Install dependencies:
pnpm install- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration- Start the development server:
pnpm devThe API will be available at http://localhost:3001
📁 Project Structure
src/
├── config/ # Configuration files
│ ├── index.ts # Environment validation
│ └── database.ts # Database connections
├── middleware/ # Express middleware
│ ├── errorHandler.ts
│ ├── notFoundHandler.ts
│ ├── requestLogger.ts
│ └── validateRequest.ts
├── routes/ # API route handlers
├── services/ # Business logic
├── models/ # Database models
├── utils/ # Utility functions
│ └── logger.ts # Logging utility
├── types/ # TypeScript type definitions
├── scripts/ # Database and utility scripts
├── app.ts # Express app configuration
├── server.ts # Server startup
└── index.ts # Entry point🔧 Available Scripts
pnpm dev- Start development server with hot reloadpnpm build- Build for productionpnpm start- Start production serverpnpm test- Run testspnpm test:watch- Run tests in watch modepnpm lint- Run ESLintpnpm lint:fix- Fix ESLint issuespnpm format- Format code with Prettier
🌐 API Endpoints
Health Check
GET /health- Server health statusGET /api/v1- API welcome message
Authentication (Coming Soon)
POST /api/v1/auth/register- User registrationPOST /api/v1/auth/login- User loginPOST /api/v1/auth/refresh- Refresh tokenPOST /api/v1/auth/logout- User logout
Games (Coming Soon)
GET /api/v1/games- List available gamesPOST /api/v1/games/free- Start free gamePOST /api/v1/games/ranked- Start ranked gamePOST /api/v1/games/special- Start special game
🔒 Security Features
- Helmet.js - Security headers
- CORS - Cross-origin resource sharing
- Rate Limiting - Request throttling
- Input Validation - Zod schema validation
- JWT Authentication - Secure token-based auth
- Password Hashing - bcrypt encryption
📊 Monitoring & Logging
- Morgan - HTTP request logging
- Custom Logger - Structured application logging
- Error Handling - Centralized error management
- Health Checks - System status monitoring
🗄️ Database
- PostgreSQL - Primary database
- Redis - Caching and sessions
- Connection Pooling - Optimized database connections
- Transactions - ACID compliance
🔧 Configuration
All configuration is managed through environment variables. See .env.example for required variables.
Key Environment Variables
NODE_ENV- Environment (development/production/test)PORT- Server port (default: 3001)DATABASE_URL- PostgreSQL connection stringREDIS_HOST- Redis server hostJWT_SECRET- JWT signing secretSTRIPE_SECRET_KEY- Stripe payment processing
🧪 Testing
The project uses Jest for testing with TypeScript support:
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage📈 Development Status
✅ Phase 1: Project Setup (Completed)
- [x] Express TypeScript project structure
- [x] Core dependencies and configuration
- [x] Middleware setup (CORS, logging, error handling)
- [x] Database connections (PostgreSQL, Redis)
- [x] Environment configuration
- [x] Development server setup
🔄 Phase 2: Authentication & User Management (Next)
- [ ] User registration and login
- [ ] JWT token management
- [ ] Password reset functionality
- [ ] User profile management
- [ ] Role-based access control
📋 Upcoming Phases
- Phase 3: Game Management
- Phase 4: Leaderboards & Scoring
- Phase 5: Payment & Subscription
- Phase 6: Referral System
- Phase 7: Admin Dashboard API
- Phase 8: Real-time Features
- Phase 9: Testing & Documentation
🤝 Contributing
- Follow the existing code style and patterns
- Write tests for new features
- Update documentation as needed
- Use conventional commit messages
📄 License
This project is part of the TJOC monorepo.
