SongBuddy Backend API - Node.js + Express + TypeScript + MongoDB for social music sharing platform
A robust backend API for SongBuddy, a social music sharing application that connects with Spotify to enable users to share their favorite tracks, discover new music, and connect with friends through the universal language of music.
- π Spotify OAuth Integration - Seamless authentication with Spotify accounts
- π₯ Social Features - Follow users, like posts, share music, real-time notifications
- π§ Music Discovery - Track recommendations, trending posts, search functionality
- π± Real-time Updates - Socket.io for live notifications and activity feeds
- π Secure Authentication - JWT tokens with refresh token rotation
- π User Management - Profiles, preferences, privacy settings
- πΌοΈ File Upload - Cloudinary integration for user avatars
- β‘ Performance - Redis caching, optimized database queries
- π‘οΈ Security - Rate limiting, input validation, CORS protection
- Runtime: Node.js 18+
- Framework: Express.js
- Language: TypeScript
- Database: MongoDB with Mongoose ODM
- Authentication: JWT + Spotify OAuth 2.0
- Real-time: Socket.io
- File Storage: Cloudinary
- Caching: Redis (optional)
- Logging: Custom structured logger
- Security: Helmet, CORS, Rate Limiting
- Node.js 18.0.0 or higher
- npm 8.0.0 or higher
- MongoDB (local or MongoDB Atlas)
- Spotify Developer Account
- Cloudinary Account (for image uploads)
-
Clone the repository
git clone https://github.com/mohammadaminrez/songbuddy-backend.git cd songbuddy-backend -
Install dependencies
npm install
-
Environment Setup
cp env.example .env
Edit the
.envfile with your credentials:# Database MONGODB_URI=mongodb://localhost:27017/songbuddy # JWT JWT_SECRET=your_super_secret_jwt_key_here JWT_EXPIRES_IN=7d # Spotify OAuth SPOTIFY_CLIENT_ID=your_spotify_client_id SPOTIFY_CLIENT_SECRET=your_spotify_client_secret SPOTIFY_REDIRECT_URI=http://localhost:3000/api/auth/spotify/callback # Cloudinary CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name CLOUDINARY_API_KEY=your_cloudinary_api_key CLOUDINARY_API_SECRET=your_cloudinary_api_secret # Server PORT=3000 NODE_ENV=development
-
Run the application
# Development mode npm run dev # Production build npm run build npm start
songbuddy-backend/
βββ src/
β βββ controllers/ # API route controllers
β βββ models/ # Database models
β β βββ User.ts
β β βββ Post.ts
β β βββ Notification.ts
β βββ routes/ # API routes
β βββ middleware/ # Custom middleware
β β βββ auth.ts
β βββ services/ # Business logic services
β β βββ database.ts
β β βββ spotifyService.ts
β β βββ jwtService.ts
β βββ utils/ # Utility functions
β β βββ logger.ts
β βββ types/ # TypeScript interfaces
β β βββ index.ts
β βββ app.ts # Main application file
βββ tests/ # Test files
βββ logs/ # Application logs
βββ package.json
βββ tsconfig.json
βββ README.md
# Development
npm run dev # Start development server with hot reload
npm run dev:build # Build and run in development
# Production
npm run build # Build TypeScript to JavaScript
npm start # Start production server
# Testing
npm test # Run tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage
# Code Quality
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint errors
npm run type-check # Check TypeScript types
# Utilities
npm run clean # Clean build directoryThe API uses JWT tokens for authentication with Spotify OAuth integration:
- User initiates Spotify OAuth
- Spotify redirects with authorization code
- Backend exchanges code for access token
- Backend creates/updates user account
- Backend returns JWT tokens
- Client uses JWT for API requests
All protected routes require the Authorization header:
Authorization: Bearer <jwt_token>
POST /api/auth/spotify- Initiate Spotify OAuthGET /api/auth/spotify/callback- Spotify OAuth callbackPOST /api/auth/refresh- Refresh JWT tokensPOST /api/auth/logout- Logout user
GET /api/users/profile- Get current user profilePUT /api/users/profile- Update user profileGET /api/users/:id- Get user by IDPOST /api/users/:id/follow- Follow a userDELETE /api/users/:id/follow- Unfollow a userGET /api/users/search- Search users
GET /api/posts/feed- Get user's feedPOST /api/posts- Create a new postGET /api/posts/:id- Get post by IDDELETE /api/posts/:id- Delete postPOST /api/posts/:id/like- Like a postDELETE /api/posts/:id/like- Unlike a postGET /api/posts/trending- Get trending posts
GET /api/notifications- Get user notificationsPUT /api/notifications/:id/read- Mark notification as readPUT /api/notifications/read-all- Mark all as read
The backend integrates with Spotify Web API to provide:
- User Authentication - OAuth 2.0 flow
- Music Data - Track, artist, album information
- User Preferences - Top tracks, recently played
- Music Discovery - Recommendations and search
- Playback Status - Currently playing tracks
user-read-private
user-read-email
user-read-currently-playing
user-read-playback-state
user-library-read
playlist-read-private
user-read-recently-played
user-top-read
Socket.io integration provides real-time updates for:
- Notifications - New likes, follows, comments
- Activity Feed - Live post updates
- User Presence - Online/offline status
- Chat Features - Direct messages (future)
- Rate Limiting - Prevent API abuse
- Input Validation - Sanitize and validate requests
- CORS Protection - Configured origins
- Helmet Security - Security headers
- JWT Token Rotation - Secure token management
- Environment Variables - Sensitive data protection
- Database Indexing - Optimized queries
- Redis Caching - Frequently accessed data
- Pagination - Efficient data loading
- Connection Pooling - Database connections
- Compression - Response compression
NODE_ENV=production
MONGODB_URI=mongodb+srv://user:pass@cluster.mongodb.net/songbuddy
JWT_SECRET=your_production_jwt_secret
SPOTIFY_CLIENT_ID=your_production_spotify_client_id
SPOTIFY_CLIENT_SECRET=your_production_spotify_client_secret
CLOUDINARY_CLOUD_NAME=your_production_cloudinary_name- Vercel - Serverless deployment
- Railway - Full-stack deployment
- Heroku - Container deployment
- DigitalOcean - VPS deployment
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watchFor detailed API documentation, visit:
- Swagger UI -
/api/docs(when implemented) - Postman Collection - Available in
/docsfolder
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow TypeScript best practices
- Write tests for new features
- Update documentation
- Follow conventional commit messages
- Ensure all tests pass
This project is licensed under the MIT License - see the LICENSE file for details.
- Spotify - For providing the amazing Web API
- MongoDB - For the flexible database solution
- Express.js - For the robust web framework
- Socket.io - For real-time communication
- Cloudinary - For image management
If you encounter any issues or have questions:
- GitHub Issues - Create an issue
- Documentation - Check this README and code comments
- Community - Join our discussions
- Frontend - SongBuddy Flutter App
- Mobile - Coming soon...
Made with β€οΈ for music lovers everywhere
Built with Node.js, Express, TypeScript, and MongoDB