This is the backend server for the Spotify Collect application that allows users to fetch and download their Spotify playlist information.
- Rate Limiting: Implements rate limiting on all endpoints to prevent abuse
- Environment-based Debug Logging: Sensitive debug information only shown in development
- Secure Session Management: Uses secure cookies in production with proper SameSite settings
- Input Validation: Validates Spotify IDs and request structure
- Error Sanitization: Sanitizes error messages to prevent information leakage
- Helmet Security Headers: Adds security headers in production
- CORS Configuration: Properly configured for frontend domains
-
Install Dependencies
npm install
-
Environment Configuration
cp .env.example .env
Edit
.envfile with your actual values:- Get Spotify API credentials from Spotify Developer Dashboard
- Set redirect URI in Spotify app to:
http://127.0.0.1:5173/auth/callback(for development) - Generate a secure session secret (32+ characters)
- Set appropriate URLs for your deployment
-
Required Environment Variables
SPOTIFY_CLIENT_ID: Your Spotify app client IDSPOTIFY_CLIENT_SECRET: Your Spotify app client secretSPOTIFY_REDIRECT_URI: OAuth redirect URI (should match your Spotify app settings)SESSION_SECRET: Long random string for session encryptionNODE_ENV: Set to 'production' for production deploymentPORT: Port to run the server (default: 3001)FRONTEND_URL: Frontend URL (required in production, must be HTTPS)
-
Development
npm start
-
Production Deployment
- Set
NODE_ENV=production - Set
FRONTEND_URLto your HTTPS frontend domain - Ensure all environment variables are set
- Use a process manager like PM2 for production
- Set
GET /auth/login- Start Spotify OAuth flowGET /auth/callback- Handle OAuth callback
GET /api/status- Check authentication statusGET /api/playlists- Get user's playlistsGET /api/playlists/:id/tracks- Get tracks for a playlistPOST /api/download- Download selected tracks in various formats
GET /api/session-test- Test session functionality (dev only)GET /api/test-session/:sessionId- Test specific session (dev only)
- Auth endpoints: 25 requests per 5 minutes per IP
- API endpoints: 100 requests per 15 minutes per IP
- Download endpoint: 10 requests per 15 minutes per IP
- Sessions are stored in files under
./sessions/directory - Session files are automatically cleaned up
- All sensitive data is only logged in development mode
- Production deployment requires HTTPS for secure cookies
- Input validation prevents malicious requests
- Error messages are sanitized to prevent information disclosure
- Set
NODE_ENV=production - Configure all required environment variables
- Set
FRONTEND_URLto HTTPS domain - Ensure Spotify app redirect URI matches your domain
- Use process manager (PM2, systemd, etc.)
- Set up reverse proxy (nginx, Apache, etc.)
- Configure SSL/TLS certificates
- Set up monitoring and logging