Skip to content

EmilioPG13/ecommerce-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

194 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

E-commerce API

Overview

A comprehensive e-commerce REST API built with Express, Node.js, and PostgreSQL, complemented by a React-based frontend. This API enables CRUD operations for products, user accounts, shopping carts, and orders, with Swagger documentation for easy endpoint interaction. The client application provides a user-friendly interface for interacting with the API. This project is deployed on Render.

Features

Technologies

Setup Instructions

Backend Setup

  1. Clone the repository:
    git clone <repository-url>
    cd ecommerce-api
  2. Install dependencies:
    npm install
  3. Configure Environment: Create a .env file in the root directory with the following variables. Use DATABASE_URL or the individual DB_* variables depending on your src/config/db.js setup.
    # Option 1: Connection String
    DATABASE_URL=postgres://<username>:<password>@<host>:<port>/<database-name>
    
    # Option 2: Individual Variables (often preferred for flexibility)
    DB_HOST=<host>
    DB_USER=<username>
    DB_PASSWORD=<password>
    DB_NAME=<database-name>
    DB_PORT=<port> # Usually 5432
    
    # Required Variables
    JWT_SECRET=<your_very_strong_jwt_secret_key>
    PORT=3000 # Or another port if 3000 is taken
    NODE_ENV=development # Use 'production' in production environments
    
    # Required for CORS in production/deployment
    FRONTEND_URL=http://localhost:3001 # URL of your running frontend (change for deployment)
  4. Database Setup:
    • Ensure PostgreSQL is installed and running.
    • Create your database using a tool like psql or a GUI: createdb <database-name>
    • Run migrations to create tables: npx sequelize-cli db:migrate (.sequelizerc, src/migrations/)
  5. Seed Database (Optional but Recommended): Populate the database with initial product data using the import script. Ensure your .env file is configured correctly for the target database (local or production).
    node importProducts.js
  6. Start the server:
    npm run start # Or node src/server.js
    The API should be running at http://localhost:PORT (e.g., http://localhost:3000). Access Swagger docs at /api-docs.

Frontend Setup (client/)

  1. Navigate to the client directory:
    cd client
  2. Install dependencies:
    npm install
  3. Configure Environment: Create a .env file in the client directory:
    REACT_APP_API_URL=http://localhost:3000/api # Adjust port if backend runs elsewhere
    REACT_APP_STRIPE_PUBLIC_KEY=<your_stripe_publishable_key>
  4. Start the client:
    npm start
    The React app should open in your browser, typically at http://localhost:3001.

Deployment

This application is configured for deployment on platforms like Render. Key considerations:

  • Set environment variables (DATABASE_URL or DB_*, JWT_SECRET, NODE_ENV=production, FRONTEND_URL, REACT_APP_API_URL, REACT_APP_STRIPE_PUBLIC_KEY) in the Render service settings.
  • Ensure the backend service's FRONTEND_URL correctly points to the deployed frontend URL for CORS.
  • Ensure the frontend service's REACT_APP_API_URL correctly points to the deployed backend API URL.
  • Use Render's PostgreSQL addon for the database.
  • Run migrations as part of the build or deploy process (npx sequelize-cli db:migrate).
  • Run the importProducts.js script against the production database (e.g., via Render's shell or by temporarily configuring your local environment) to seed initial data.

API Endpoints

Refer to the Swagger documentation available at /api-docs when the backend server is running.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages