Skip to content

DevAryanSin/CoffeeChaos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

β˜• CoffeeChaos - Netlify Serverless Edition

A coffee ordering application built with Next.js (frontend) and Express (backend), optimized for Netlify serverless deployment.

πŸš€ Quick Start

Prerequisites

  • Node.js 18+
  • MongoDB Atlas account
  • Netlify account (for deployment)

Local Development

  1. Clone and Install

    # Install backend dependencies
    cd backend
    npm install
    
    # Install frontend dependencies
    cd ../frontend
    npm install
  2. Configure Environment

    # Backend: Copy .env.example to .env
    cd backend
    cp .env.example .env
    # Edit .env and add your MongoDB connection string
  3. Run Development Servers

    # Terminal 1: Backend (Express server)
    cd backend
    npm run dev
    # Runs on http://localhost:5000
    
    # Terminal 2: Frontend (Next.js)
    cd frontend
    npm run dev
    # Runs on http://localhost:3000
  4. Access the App

🌐 Deploy to Netlify

Option 1: Git-based Deployment (Recommended)

  1. Push to GitHub

    git add .
    git commit -m "Ready for Netlify deployment"
    git push origin main
  2. Connect to Netlify

    • Go to Netlify
    • Click "Add new site" β†’ "Import an existing project"
    • Connect your Git repository
    • Netlify auto-detects netlify.toml configuration
  3. Configure Environment Variables

    In Netlify Dashboard β†’ Site settings β†’ Environment variables:

    Variable Value
    MONGODB_URI Your MongoDB Atlas connection string
    FRONTEND_URL Your Netlify site URL
    NODE_VERSION 18
  4. Deploy

    • Click "Deploy site"
    • Wait for build to complete
    • Test your live site!

Option 2: Netlify CLI

# Install Netlify CLI
npm install -g netlify-cli

# Login
netlify login

# Deploy
netlify deploy --prod

πŸ“ Project Structure

CoffeeChaos/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ functions/
β”‚   β”‚   └── api.js              # Netlify serverless function
β”‚   β”œβ”€β”€ server.js               # Express app (serverless-ready)
β”‚   β”œβ”€β”€ .env                    # Local environment variables
β”‚   β”œβ”€β”€ .env.example            # Environment template
β”‚   └── package.json
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ app/                    # Next.js pages
β”‚   β”œβ”€β”€ components/             # React components
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   └── axios.js            # API client (auto-detects env)
β”‚   β”œβ”€β”€ next.config.js          # Next.js config (static export)
β”‚   └── package.json
β”œβ”€β”€ netlify.toml                # Netlify deployment config
β”œβ”€β”€ DEPLOYMENT.md               # Detailed deployment guide
β”œβ”€β”€ DEPLOYMENT_CHECKLIST.md     # Step-by-step checklist
└── CONVERSION_SUMMARY.md       # Architecture overview

πŸ”§ Configuration Files

netlify.toml

Configures Netlify build, functions, and redirects:

  • Build command: Installs deps and builds frontend
  • Publish directory: frontend/out (static export)
  • Functions: backend/functions
  • Redirects: /api/* β†’ serverless functions

next.config.js

Configures Next.js for static export:

  • output: 'export' - Generates static HTML
  • images.unoptimized: true - Required for static export

backend/functions/api.js

Wraps Express app for Netlify Functions:

  • Loads environment variables
  • Uses serverless-http adapter
  • Handles all /api/* routes

πŸ—„οΈ Database

MongoDB Atlas Setup

  1. Create Cluster

  2. Create Database User

    • Database Access β†’ Add New Database User
    • Set username and password
    • Grant read/write permissions
  3. Configure Network Access

    • Network Access β†’ Add IP Address
    • For Netlify: Allow access from anywhere (0.0.0.0/0)
  4. Get Connection String

    • Clusters β†’ Connect β†’ Connect your application
    • Copy connection string
    • Replace <password> with your database password

Collections

The app automatically creates these collections:

  • users - User profiles and total cups
  • orders - Coffee orders
  • ratings - Order ratings

πŸ› οΈ API Endpoints

Method Endpoint Description
GET /api/health Health check
POST /api/orders Create new order
GET /api/orders/:username Get user's orders
GET /api/leaderboard Get top 10 users by cups
POST /api/ratings Submit order rating
GET /api/ratings/:username Get user's ratings

🎨 Features

  • β˜• Coffee Ordering: Order different types of coffee
  • πŸ“Š Leaderboard: See top coffee drinkers
  • ⭐ Ratings: Rate your coffee orders
  • 🎯 Quiz: Coffee lover verification quiz
  • πŸ“± Responsive: Works on all devices
  • πŸš€ Serverless: Scales automatically

πŸ” Environment Variables

Backend (.env)

MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database
PORT=5000                           # Local dev only
FRONTEND_URL=http://localhost:3000  # Local dev

Netlify (Dashboard)

MONGODB_URI=mongodb+srv://...       # Production database
FRONTEND_URL=https://your-site.netlify.app
NODE_VERSION=18

πŸ§ͺ Testing

Local Testing

# Test backend health
curl http://localhost:5000/api/health

# Test frontend
# Open http://localhost:3000 in browser

Production Testing

# Test API
curl https://your-site.netlify.app/api/health

# Expected response:
# {"status":"OK","message":"Running!"}

πŸ“š Documentation

πŸ› Troubleshooting

MongoDB Connection Failed

  • Verify MONGODB_URI is correct
  • Check MongoDB Atlas network access allows 0.0.0.0/0
  • Verify database user credentials

404 on API Routes

  • Check netlify.toml redirects
  • Verify backend/functions/api.js exists
  • Redeploy the site

CORS Errors

  • Update FRONTEND_URL environment variable
  • Check CORS settings in backend/server.js

See DEPLOYMENT.md for detailed troubleshooting.

πŸš€ Performance

Frontend

  • βœ… Static HTML (pre-rendered)
  • βœ… CDN delivery via Netlify
  • βœ… Optimized assets

Backend

  • βœ… Serverless functions (auto-scaling)
  • βœ… MongoDB connection caching
  • βœ… Optimized pool size

Database

  • βœ… MongoDB Atlas (managed)
  • βœ… Automatic backups
  • βœ… Global clusters

πŸ“ˆ Monitoring

Netlify

  • Dashboard β†’ Functions β†’ View logs
  • Dashboard β†’ Analytics β†’ Usage stats

MongoDB Atlas

  • Metrics β†’ Monitor connections
  • Performance Advisor β†’ Query optimization

πŸ”’ Security

  • βœ… Environment variables (not in code)
  • βœ… CORS configuration
  • βœ… MongoDB authentication
  • βœ… Network access controls

🎯 Next Steps

  1. βœ… Deploy to Netlify
  2. βœ… Test all features
  3. βœ… Set up custom domain (optional)
  4. βœ… Add database indexes for performance
  5. βœ… Implement authentication (if needed)
  6. βœ… Set up monitoring/alerts

πŸ“„ License

ISC

🀝 Contributing

Feel free to submit issues and pull requests!


Built with ❀️ and β˜•

For detailed deployment instructions, see DEPLOYMENT.md

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors