Production-ready backend API for the CoupleBond mobile application. Handles authentication, couple pairing, activities, prayers, calendar events, and more.
This backend was completely debugged and refactored in November 2024. See COMPLETE_SUMMARY.md for details.
# 1. Install dependencies
npm install
# 2. Set up database
createdb cs262_couplebond
psql -U postgres -d cs262_couplebond -f migrations/001_couplebond_schema.sql
# 3. Configure environment (create .env file)
DATABASE_URL=postgres://postgres:YOUR_PASSWORD@localhost:5432/cs262_couplebond
JWT_SECRET=your_secret_key_here
PORT=4000
# 4. Run server
npm run devServer runs on http://localhost:4000
- COMPLETE_SUMMARY.md - Overview of all improvements
- DEBUG_AND_IMPROVEMENTS_COMPLETE.md - Detailed debugging report
- API_TESTING_GUIDE.md - Test all endpoints
- MIGRATION_GUIDE.md - Migrate from old schema
- Runtime: Node.js with TypeScript
- Framework: Express.js
- Database: PostgreSQL
- Authentication: JWT + bcrypt
- Features: Transactions, validation, error handling
Tables: users, couples, pairing_codes, activities, photo_collages, calendar_events, prayer_items
Migration: migrations/001_couplebond_schema.sql
POST /api/auth/register- Create accountPOST /api/auth/login- LoginGET /api/auth/me- Get current user
GET /api/user/profile- Get profilePUT /api/user/profile- Update profile
POST /api/user/partner/generate-code- Generate pairing codePOST /api/user/partner/connect- Connect with partnerGET /api/user/partner- Get partner infoDELETE /api/user/partner/unmatch- Disconnect
POST /api/couple/create- Create couplePOST /api/couple/join- Join coupleGET /api/couple/me- Get couple infoDELETE /api/couple/leave- Leave couple
POST /api/activities- Create activityGET /api/activities- Get all activitiesGET /api/activities/:id- Get activityPUT /api/activities/:id- Update activityDELETE /api/activities/:id- Delete activityPOST /api/activities/:id/photos- Add photoDELETE /api/activities/:activityId/photos/:photoId- Delete photoGET /api/activities/timeline/all- Timeline view
POST /api/calendar/events- Create eventGET /api/calendar/events- Get all eventsGET /api/calendar/events/:id- Get eventPUT /api/calendar/events/:id- Update eventDELETE /api/calendar/events/:id- Delete eventGET /api/calendar/upcoming- Upcoming eventsGET /api/calendar/anniversaries- Calculate anniversaries
POST /api/prayers- Create prayerGET /api/prayers- Get all prayersGET /api/prayers/:id- Get prayerPUT /api/prayers/:id- Update prayerPUT /api/prayers/:id/toggle-answered- Toggle answeredDELETE /api/prayers/:id- Delete prayer
See API_TESTING_GUIDE.md for curl commands.
Create .env file:
# Database connection
DATABASE_URL=postgres://username:password@host:5432/database
# JWT secret (change in production!)
JWT_SECRET=your_super_secret_key
# Server port
PORT=4000# Health check
curl http://localhost:4000/api/health
# Register user
curl -X POST http://localhost:4000/api/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"pass123","name":"Test User"}'Full testing guide: API_TESTING_GUIDE.md
npm run dev # Development mode with auto-restart
npm run build # Compile TypeScript to JavaScript
npm start # Production mode (requires build first)β
Transaction Safety - All multi-step operations atomic
β
Permission Checks - Users can only access their couple's data
β
Input Validation - All user input validated
β
Standardized Responses - Consistent { success, data, message } format
β
Error Handling - Comprehensive error catching with proper status codes
β
Security - JWT auth, bcrypt passwords, SQL injection prevention
β
Performance - Database indexes, connection pooling, optimized queries
Service/
βββ src/
β βββ db.ts # Database connection & transactions
β βββ index.ts # Express app setup
β βββ middleware/
β β βββ auth.ts # JWT authentication
β βββ routes/
β βββ auth.ts # Auth endpoints
β βββ user.ts # User profile & partner pairing
β βββ couple.ts # Couple management
β βββ activities.ts # Activities & photos
β βββ calendar.ts # Calendar events
β βββ prayers.ts # Prayer items
βββ migrations/
β βββ 001_couplebond_schema.sql # Database schema
βββ package.json
βββ tsconfig.json
βββ .env (create this)
- Check PostgreSQL is running:
pg_ctl status - Verify DATABASE_URL is correct
- Run migration:
psql -U postgres -d your_db -f migrations/001_couplebond_schema.sql
- Token expired (7 days) - login again
- JWT_SECRET changed - regenerate tokens
- User must be paired first using generate-code + connect flow
If you have an existing database with old migrations:
See MIGRATION_GUIDE.md for step-by-step instructions.
// Success
{
success: true,
data: { ... },
message?: string
}
// Error
{
success: false,
message: string
}Include token in all requests (except register/login):
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}- 200/201: Success
- 400: Validation error
- 401: Need to login
- 404: Not found
- 409: Already exists
- 500: Server error
Questions? Check the documentation:
COMPLETE_SUMMARY.md- OverviewDEBUG_AND_IMPROVEMENTS_COMPLETE.md- Detailed reportAPI_TESTING_GUIDE.md- API reference
β Production Ready
- All bugs fixed
- All features implemented
- Fully documented
- Security hardened
- Performance optimized
- Client Repository - React Native app
- Project Repository - Project docs
Backend last updated: November 2024
game,player,playergame- for the monopoly game data
If you need to recreate the users table, run:
node setup-azure-db.jsnpm run devThe service will run on http://localhost:4000 (or the PORT specified in .env).
You should see:
[db] connected
Server running on port 4000, listening on all interfaces
Test the health endpoint:
Invoke-RestMethod -Uri http://localhost:4000/api/healthTest registration:
$body = @{ email="test@example.com"; password="password123" } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "http://localhost:4000/api/auth/register" -ContentType "application/json" -Body $bodyPOST /api/auth/register- Register a new userPOST /api/auth/login- Login with email/passwordGET /api/auth/me- Get current user (requires JWT token)
GET /api/user/profile- Get user profile (requires JWT token)
GET /api/health- Check if service is runningGET /- Root endpoint
If you get ETIMEDOUT errors, your IP address needs to be added to the Azure firewall rules.
Make sure you're using the correct database credentials in your .env file.
If port 4000 is already in use, change the PORT in your .env file.