Overview
This guide walks you through setting up the E-commerce API in both development and production environments. The API is built with Node.js, TypeScript, Express, and uses MySQL as the database with Prisma ORM.The installation process takes approximately 10-15 minutes for a fresh setup.
Prerequisites
Ensure you have the following installed on your system:Node.js
Version 18.x or higher
MySQL
Version 8.4 or higher
npm or yarn
Package manager
Git
Version control
- Docker & Docker Compose (for containerized deployment)
- Cloudinary account (for image uploads)
Installation Methods
Choose your preferred installation method:- Local Development
- Docker
- Production
Best for development and testing
Method 1: Local Development Setup
Install Dependencies
Install all required npm packages:This installs the following key dependencies:
express(5.1.0) - Web framework@prisma/client(6.16.2) - Database ORMjsonwebtoken(9.0.2) - JWT authenticationbcrypt(6.0.0) - Password hashingclass-validator(0.14.2) - Input validationcloudinary(2.9.0) - Image storageexpress-rate-limit(8.2.1) - Rate limitinghelmet(8.1.0) - Security headers
Configure Environment Variables
Create a
.env file in the backend directory:.env
JWT_SECRET: Generate a secure random string. Use
openssl rand -base64 32 to generate one.Cloudinary: Optional but required for product image uploads. Sign up at cloudinary.com for free.Run Database Migrations
Initialize the database schema using Prisma:This creates all necessary tables:
User- User accounts with authenticationProduct- Product catalogCategory- Product categoriesCart&CartItem- Shopping cartOrder&OrderItem- Order management
Seed the Database (Optional)
Populate the database with sample data:This creates:
- Sample admin and customer users
- Product categories
- Sample products with images
Default admin credentials (if seeded):
- Email:
[email protected] - Password: Check
prisma/seed.tsfile
Verify Installation
Test that the API is running correctly:Method 2: Docker Setup
The easiest way to get started with all dependencies included.Create Environment Files
Create Create a root
.env file in the backend directory with the same configuration as above, but use Docker service names:backend/.env
.env file for Docker Compose:.env
Build and Start Containers
Launch all services with Docker Compose:This starts:
- MySQL on port 3306
- Backend API on port 3000
- Frontend (if available) on port 4200
Docker Commands
Method 3: Production Deployment
Build the Application
Compile TypeScript to JavaScript:This creates a
dist/ directory with compiled code using the tsconfig.build.json configuration.Run Database Migrations
For zero-downtime deployments, use Prisma’s migration strategies and consider running migrations in a separate deployment step.
Production Best Practices
Reverse Proxy
Use Nginx or Caddy to handle SSL/TLS termination and serve as a reverse proxy to the Node.js application.
Process Management
Use PM2 or systemd to ensure the application restarts on crashes and starts on system boot.
Database Backups
Schedule regular MySQL backups and test restoration procedures.
Monitoring
Implement logging and monitoring with tools like Winston, Sentry, or Datadog.
Environment Variables Reference
| Variable | Required | Default | Description |
|---|---|---|---|
DATABASE_URL | Yes | - | MySQL connection string |
JWT_SECRET | Yes | - | Secret key for JWT signing |
JWT_EXPIRY | No | 1h | Token expiration time (e.g., 1h, 7d) |
PORT | No | 3000 | Server port |
NODE_ENV | No | development | Environment mode |
FRONTEND_URL | No | http://localhost:3001 | Frontend origin for CORS |
CLOUDINARY_CLOUD_NAME | For uploads | - | Cloudinary cloud name |
CLOUDINARY_API_KEY | For uploads | - | Cloudinary API key |
CLOUDINARY_API_SECRET | For uploads | - | Cloudinary API secret |
Database Schema
The Prisma schema (prisma/schema.prisma) defines the following models:
Testing the Installation
Run the test suite to verify everything is working:- Authentication service (
auth.service.test.ts) - Product service (
product.service.test.ts) - Cart service (
cart.service.test.ts) - Order service (
order.service.test.ts) - Category service (
category.service.test.ts)
Common Issues
Database Connection Errors
Problem:Can't connect to MySQL server
Solutions:
- Verify MySQL is running:
systemctl status mysqlordocker-compose ps - Check DATABASE_URL credentials and hostname
- Ensure MySQL port (3306) is not blocked by firewall
- For Docker: Use service name
mysqlinstead oflocalhost
Prisma Migration Errors
Problem:Migration failed or Schema out of sync
Solutions:
Port Already in Use
Problem:EADDRINUSE: address already in use :::3000
Solutions:
- Change PORT in
.envfile - Kill process using port 3000:
lsof -ti:3000 | xargs kill -9
TypeScript Compilation Errors
Problem: Build fails with TypeScript errors Solutions:- Ensure TypeScript version is 5.9.3:
npm list typescript - Clean build directory:
rm -rf dist/ - Reinstall dependencies:
rm -rf node_modules && npm install
Cloudinary Upload Errors
Problem: Image uploads fail Solutions:- Verify Cloudinary credentials are correct
- Check image size is under 5MB limit
- Ensure CLOUDINARY variables are set in
.env
Next Steps
Quick Start Tutorial
Make your first API calls and test the installation
API Reference
Explore all available endpoints
Architecture Overview
Understand the API architecture and design
Docker Deployment
Deploy with Docker and docker-compose
Getting Help
If you encounter issues not covered in this guide:- Check the logs:
npm run devshows detailed error messages - Review Prisma logs:
DEBUG="prisma:*" npm run dev - Verify environment variables:
printenv | grep -E '(DATABASE|JWT|CLOUDINARY)' - Test database connection:
npx prisma db pull
The API uses structured logging in development mode. Check console output for detailed error messages and stack traces.