This guide will help you set up the GitHub Analytics Tool backend for development.
- Node.js (v18 or higher)
- MongoDB (v6 or higher)
- Git
- GitHub Account with Personal Access Token
Create a .env file in the backend/ directory:
# Server Configuration
NODE_ENV=development
PORT=5000
API_VERSION=v1
# Database Configuration
MONGODB_URI=mongodb://localhost:27017/github-analytics
# GitHub API Configuration
GITHUB_TOKEN=your_github_personal_access_token_here
GITHUB_CLIENT_ID=your_github_app_client_id
GITHUB_CLIENT_SECRET=your_github_app_client_secret
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-minimum-32-characters-long
JWT_EXPIRE=30d
# Security Configuration
BCRYPT_SALT_ROUNDS=12
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
# Cache Configuration
CACHE_TTL=300
CACHE_MAX_KEYS=1000
# Logging Configuration
LOG_LEVEL=info
LOG_FILE=logs/app.logcd backend
npm install# macOS with Homebrew
brew services start mongodb-community
# Ubuntu/Debian
sudo systemctl start mongod
# Windows
net start MongoDB# Development mode with auto-reload
npm run dev
# Production mode
npm startThe API will be available at http://localhost:5000
- Go to GitHub Settings → Developer settings → Personal access tokens
- Click "Generate new token (classic)"
- Select these scopes:
public_repo- Access public repositoriesread:user- Read user profile informationuser:email- Access user email addressesread:org- Read organization membership
- Copy the token to your
.envfile
curl http://localhost:5000/api/health# Get all users
curl http://localhost:5000/api/users
# Get specific user
curl http://localhost:5000/api/users/octocat
# Search users
curl "http://localhost:5000/api/users/search?q=javascript"# Global leaderboard
curl http://localhost:5000/api/leaderboard
# Leaderboard stats
curl http://localhost:5000/api/leaderboard/stats
# Top contributors
curl http://localhost:5000/api/leaderboard/contributors# Global analytics
curl http://localhost:5000/api/analytics/global
# User analytics
curl http://localhost:5000/api/analytics/user/octocat
# Analytics trends
curl http://localhost:5000/api/analytics/trends{
username: "octocat",
name: "The Octocat",
avatarUrl: "https://github.com/images/error/octocat_happy.gif",
bio: "GitHub mascot",
location: "San Francisco",
company: "GitHub",
blog: "https://github.blog",
email: "octocat@github.com",
followers: 3000,
following: 9,
publicRepos: 8,
totalRepos: 8,
totalCommits: 1500,
totalPullRequests: 100,
totalIssues: 50,
totalReviews: 200,
totalContributions: 1850,
currentStreak: 5,
longestStreak: 30,
topLanguages: [
{ name: "JavaScript", percentage: 45.2 },
{ name: "Python", percentage: 30.1 }
],
recentRepos: [...],
contributionCalendar: [...],
isActive: true,
createdAt: "2024-01-01T00:00:00.000Z",
updatedAt: "2024-01-15T12:00:00.000Z",
lastAnalyticsUpdate: "2024-01-15T12:00:00.000Z"
}{
username: "octocat",
date: "2024-01-15T00:00:00.000Z",
commits: 5,
pullRequests: 2,
issues: 1,
reviews: 3,
repositories: [...],
languages: [...],
additions: 150,
deletions: 50
}npm start # Start production server
npm run dev # Start development server with nodemon
npm test # Run tests
npm run test:watch # Run tests in watch mode
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint issues
npm run format # Format code with Prettier- ESLint
- Prettier
- REST Client
- MongoDB for VS Code
- GitLens
| Variable | Description | Default | Required |
|---|---|---|---|
NODE_ENV |
Environment (development/production) | development |
No |
PORT |
Server port | 5000 |
No |
MONGODB_URI |
MongoDB connection string | - | Yes |
GITHUB_TOKEN |
GitHub personal access token | - | Yes |
JWT_SECRET |
JWT signing secret | - | Yes |
JWT_EXPIRE |
JWT expiration time | 30d |
No |
LOG_LEVEL |
Logging level (debug/info/warn/error) | info |
No |
Error: MongoNetworkError: failed to connect to serverSolution: Make sure MongoDB is running:
brew services start mongodb-communityError: API rate limit exceededSolution:
- Check your token has sufficient rate limit
- Implement caching for repeated requests
- Use GraphQL for more efficient queries
Error: listen EADDRINUSE: address already in use :::5000Solution:
# Find and kill process using port 5000
lsof -ti:5000 | xargs kill -9Error: Cannot find module 'package-name'Solution:
rm -rf node_modules package-lock.json
npm install- Ensure indexes are created for frequently queried fields
- Use aggregation pipelines for complex queries
- Implement proper pagination for large datasets
- Cache GitHub API responses
- Use Redis for production caching
- Implement cache invalidation strategies
- Use field selection to reduce payload size
- Implement request compression
- Add proper rate limiting
- Never commit
.envfiles to version control - Use different secrets for development and production
- Rotate secrets regularly
- Validate all input data
- Implement proper authentication
- Use HTTPS in production
- Set up proper CORS policies
- Use MongoDB authentication
- Limit database user permissions
- Regular security updates
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
npm test - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
If you need help:
- Check this guide first
- Search existing GitHub issues
- Create a new issue with:
- Error message
- Steps to reproduce
- Environment details
- Logs (remove sensitive data)
Happy coding! 🚀