A robust Node.js backend API powering the Trivia Flair 3.0 game. Provides user authentication, radio station proxy services, and seamless integration with the React frontend.
๐ Live API: trivia-game-v3-backend.onrender.com ๐ฎ Frontend: trivia-game-v3-frontend.onrender.com
- โจ Features
- ๐ ๏ธ Tech Stack
- ๐๏ธ Architecture
- ๐ก API Endpoints
- ๐ Quick Start
- โ๏ธ Configuration
- ๐งช Testing
- ๐ Deployment
- ๐ Performance Features
- ๐ Security
- ๐ค Contributing
- JWT-based authentication with secure token handling
- User registration and login with input validation
- Password hashing using bcrypt for security
- Persistent sessions with token extraction middleware
- MongoDB Atlas integration for user data storage
- Intelligent server discovery via DNS SRV lookup
- Health checking for Radio Browser API servers
- Load balancing across multiple radio servers
- Fallback mechanisms for high availability
- Caching with LRU cache for improved performance
- 30,000+ radio stations proxied from Radio Browser API
- Comprehensive error middleware for graceful failures
- Request logging with Morgan for debugging
- CORS configuration for secure cross-origin requests
- Input validation using the validator library
- Unknown endpoint handling for better API responses
- Node.js + Express 5.1 - Core server framework
- MongoDB Atlas + Mongoose 8.17 - Database and ODM
- JWT + bcrypt - Authentication and security
- Axios 1.12 - HTTP client for external APIs
- Morgan - HTTP request logger
- Validator 13.15 - Input validation
- LRU Cache 11.2 - In-memory caching
- SuperTest + Node.js Test Runner - Testing framework
- CORS - Cross-origin resource sharing
- dotenv - Environment variable management
controllers/
โโโ users.js # User registration and management
โโโ login.js # Authentication and JWT handling
โโโ radio.js # Radio Browser API proxy
- Token Extractor - JWT token parsing and validation
- Error Handler - Centralized error processing
- Unknown Endpoint - 404 handling for undefined routes
- Radio Config - Configuration for Radio Browser API
- Server Discovery - DNS SRV lookup and health checking
- Logger - Structured logging utilities
- Config - Environment-based configuration management
POST /api/users # Register new user
POST /api/login # User login
GET /api/radio/* # Proxy to Radio Browser API with automatic server selection
- Node.js 18+
- MongoDB Atlas account
- Git
- Clone the repository
git clone https://github.com/Akiz-Ivanov/trivia-game-v3-backend
cd trivia-game-v3-backend
- Install dependencies
npm install
- Environment setup
Create a
.env
file in the root directory:
MONGODB_URI=your_mongodb_atlas_connection_string
SECRET=your_jwt_secret_key
FRONTEND_URL=http://localhost:5173
PORT=3001
- Run development server
npm run dev
- Run tests
npm test
Variable | Description | Default |
---|---|---|
MONGODB_URI |
MongoDB Atlas connection string | Required |
SECRET |
JWT secret for token signing | Required |
FRONTEND_URL |
Frontend URL for CORS | http://localhost:5173 |
PORT |
Server port | 3001 |
NODE_ENV |
Environment mode | development |
The backend automatically discovers and health-checks Radio Browser API servers:
- DNS SRV Lookup:
_api._tcp.radio-browser.info
- Fallback Servers: Hardcoded list of reliable endpoints
- Health Checking: 3-second timeout per server
- Load Balancing: Random server selection from healthy instances
The backend includes comprehensive tests for the user authentication system:
# Run all tests
npm test
# Run tests in development
npm run test:dev
# Start test server
npm run start:test
Test Coverage:
- โ User registration validation
- โ Login authentication flow
- โ JWT token generation and validation
- โ Error handling scenarios
- ๐ง Radio proxy tests (planned for future)
The backend is configured for easy deployment on Render:
- Automatic builds from GitHub repository
- Environment variables managed through Render dashboard
- Health checks for service monitoring
- Auto-scaling based on demand
Deploy Configuration (render.yaml
):
services:
- type: web
name: trivia-game-backend
env: node
buildCommand: npm install
startCommand: npm start
envVars:
- key: MONGODB_URI
value: ${MONGODB_URI}
- key: SECRET
value: ${SECRET}
- key: FRONTEND_URL
value: ${FRONTEND_URL}
- Set production environment variables
- Install production dependencies:
npm install --production
- Start server:
npm start
- LRU Cache for frequently accessed radio station data
- Server health status caching to reduce DNS lookups
- Automatic cache invalidation for stale data
- Multi-server support for Radio Browser API
- Automatic failover when servers are unavailable
- Health monitoring with periodic server checks
- Geographic distribution across EU and US servers
- Exponential backoff for failed requests
- Graceful degradation when external services are down
- Circuit breaker pattern for Radio Browser API calls
- Password hashing with bcrypt (10 rounds)
- JWT tokens with secure secret keys
- Token expiration for session management
- Input sanitization with validator library
- CORS configuration for trusted origins only
- Rate limiting through proxy server capabilities
- Error message sanitization to prevent information leakage
- Secure headers and middleware stack
- MongoDB Atlas with built-in encryption
- Environment variable protection for sensitive data
- No sensitive data logging in production
- Secure token transmission via HTTP headers
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make your changes and add tests
- Ensure all tests pass:
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
- Use CommonJS module system
- Follow Express.js best practices
- Include tests for new features
- Use descriptive commit messages
- Maintain consistent code formatting
- Frontend Repository: trivia-game-v3-frontend
- Live Demo: Play Trivia Flair
- API Documentation: Backend API
- Radio Browser API: radio-browser.info
Built with โค๏ธ for trivia enthusiasts and radio lovers worldwide