A complete 3-page eCommerce application built with Next.js App Router, Express.js, and MongoDB, featuring dynamic payment simulation and email notifications.
- Landing Page: Product display with variant selection and quantity controls
- Checkout Page: Complete form validation and payment simulation
- Thank You Page: Order confirmation with full transaction details
- Payment Simulation: Different CVV codes trigger different payment outcomes
- Email Notifications: Automated confirmation emails via Mailtrap
- Database Storage: All order and customer data stored in MongoDB
- Responsive Design: Mobile-first design with Tailwind CSS
ecommerce-project/
โโโ backend/ # Express backend
โ โโโ server.js # Main server file
โ โโโ config/
โ โ โโโ db.js # MongoDB connection
โ โโโ models/
โ โ โโโ Product.js # Product schema
โ โ โโโ Order.js # Order schema
โ โโโ controllers/
โ โ โโโ productController.js
โ โ โโโ orderController.js
โ โ โโโ emailController.js
โ โโโ routes/
โ โ โโโ products.js
โ โ โโโ orders.js
โ โ โโโ email.js
โ โโโ middleware/
โ โ โโโ errorHandler.js
โ โโโ package.json
โ โโโ .env.example
โ
โโโ frontend/ # Next.js frontend (App Router)
โ โโโ app/
โ โ โโโ layout.js # Root layout
โ โ โโโ page.js # Landing page
โ โ โโโ globals.css # Global styles
โ โ โโโ checkout/
โ โ โ โโโ page.js # Checkout page
โ โ โโโ thank-you/
โ โ โโโ page.js # Thank you page
โ โโโ utils/
โ โ โโโ api.js # API utilities
โ โโโ package.json
โ โโโ tailwind.config.js
โ โโโ next.config.js
โ
โโโ README.md # This file
- Node.js (v16 or higher)
- MongoDB (local or cloud instance)
- Git
git clone
cd ecommerce-projectcd backend
npm installCreate a .env file in the backend directory:
cp .env.example .envUpdate the .env file with your configuration:
# Server Configuration
PORT=5000
NODE_ENV=development
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/ecommerce
# Mailtrap Email Configuration
MAILTRAP_HOST=sandbox.smtp.mailtrap.io
MAILTRAP_PORT=2525
MAILTRAP_USER=your_mailtrap_username
MAILTRAP_PASS=your_mailtrap_password
# Email Settings
FROM_EMAIL=[email protected]Start the backend server:
npm run devBackend will be running on http://localhost:5001
cd frontend
npm installCreate a .env.local file in the frontend directory:
NEXT_PUBLIC_API_URL=http://localhost:5001/apiStart the frontend development server:
npm run devFrontend will be running on http://localhost:3000
The application will automatically seed a sample product when you first visit the landing page. You can also manually seed products by making a POST request to:
POST http://localhost:5000/api/products/seed
Use these CVV codes during checkout to simulate different payment outcomes:
- 111 โ โ Approved Transaction
- 222 โ โ Declined Transaction
- 333 โ
โ ๏ธ Gateway Error/Failure - Any other 3 digits โ โ Approved
Card Number: 1234 5678 9012 3456
Expiry Date: 12/26 (any future date)
CVV: Use codes above for different outcomes
- Sign up for a free account at mailtrap.io
- Create a new inbox in sandbox mode
- Copy your SMTP credentials
- Update the
.envfile with your credentials
The application sends different email templates based on payment outcomes:
- Approved: Confirmation email with order details
- Declined: Payment declined notification with retry instructions
- Failed: Technical error notification
GET /api/products- Get all productsGET /api/products/:id- Get single productPOST /api/products/seed- Seed sample products
POST /api/orders- Create new orderGET /api/orders/:orderNumber- Get order by numberGET /api/orders- Get all orders (admin)
POST /api/email/send-order-email- Send confirmation emailGET /api/email/test-connection- Test email connection
- Responsive Design: Mobile-first approach with Tailwind CSS
- Form Validation: Real-time validation with react-hook-form
- Loading States: Smooth loading indicators and transitions
- Toast Notifications: User-friendly success/error messages
- Card Formatting: Automatic credit card number formatting
- Inventory Management: Real-time stock checking
- Variant Selection: Dynamic product variant options
- Input Validation: Server-side validation for all inputs
- Card Masking: Credit card numbers are masked in storage
- CVV Protection: CVV codes are never stored
- Error Handling: Comprehensive error handling and logging
- CORS Protection: Configured CORS policies
-
Landing Page (
/)- Browse product with variants (color, size)
- Select quantity and options
- Add to cart and proceed to checkout
-
Checkout Page (
/checkout)- Enter customer information
- Provide shipping address
- Enter payment details with simulation
- Submit order with real-time validation
-
Thank You Page (
/thank-you)- View order confirmation
- See payment status (approved/declined/failed)
- Download/print order details
- Access customer support
- Start both servers (backend on :5000, frontend on :3000)
- Visit landing page: Select product variants and quantity
- Proceed to checkout: Fill out the form with test data
- Use simulation CVV: Try different CVV codes for different outcomes
- View confirmation: Check the thank you page and email notifications
- Push code to GitHub
- Connect to Railway or Render
- Set environment variables
- Deploy with auto-scaling
- Connect GitHub repository
- Set
NEXT_PUBLIC_API_URLenvironment variable - Deploy with automatic CI/CD
Backend:
NODE_ENV=production
MONGODB_URI=your_production_mongodb_uri
MAILTRAP_USER=your_mailtrap_user
MAILTRAP_PASS=your_mailtrap_passFrontend:
NEXT_PUBLIC_API_URL=https://your-backend-url.com/api-
MongoDB Connection Error
- Ensure MongoDB is running
- Check connection string format
- Verify network connectivity
-
API Connection Issues
- Verify backend is running on correct port
- Check CORS configuration
- Ensure frontend API URL is correct
-
Email Not Sending
- Verify Mailtrap credentials
- Check spam folder
- Review server logs for errors
-
Build Errors
- Clear node_modules and reinstall
- Check Node.js version compatibility
- Review environment variable configuration
- App Router: Uses Next.js 13+ App Router for modern routing
- Server Components: Optimized for performance where possible
- Error Boundaries: Comprehensive error handling throughout
- TypeScript Ready: Easy migration to TypeScript if needed
- Scalable Architecture: Modular design for easy expansion