Skip to content

πŸ›’ Welcome to Fusion Electronics - a sample full-stack, lightweight, and modern online e-commerce application, built with the MERN (MongoDB, Express, React, Node.js) stack! Also features a product recommendation service using vector search and the Weaviate vector DB!

License

Notifications You must be signed in to change notification settings

hoangsonww/MERN-Stack-Ecommerce-App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fusion Electronics: A MERN-Stack E-commerce Application

Welcome to Fusion Electronics, a MERN-Stack E-commerce Application! This project is a working demo of a full-stack web application that was built using the MERN stack (MongoDB, Express.js, React.js, Node.js). Additionally, it also includes features such as user authentication, checkout process, product recommendations with vector search, and more! It aims to provide a comprehensive example of building a modern e-commerce platform, covering frontend user interface, backend server logic, database management, and integration with third-party libraries.

Fusion Electronics Logo

Table of Contents

  1. Introduction
  2. Live Deployment
  3. User Interface
  4. Features
  5. Technologies Used
  6. Getting Started
  7. Project Structure
  8. Running the Application
  9. Product Recommendations with Vector Database
  10. Testing the APIs
  11. Unit & Integration Testing
  12. Swagger API Documentation
  13. OpenAPI Specification
  14. Deployment
  15. Containerization
  16. GitHub Actions & CI/CD
  17. Contributing
  18. License
  19. Creator

Introduction

This project is a demonstration of building an e-commerce application using the MERN stack, which consists of MongoDB (database), Express.js (server), React.js (frontend), and Node.js (runtime environment). The application allows users to browse products, add them to a shopping cart, proceed to checkout, and simulate the order processing. It includes comprehensive validations for user inputs and simulates the checkout process on the backend.

The application is designed to be user-friendly and responsive, providing a seamless shopping experience. It also includes features such as product search, user authentication, and order confirmation. Additionally, it uses Weaviate for product recommendations based on vector search, enhancing the user experience by suggesting relevant products.

React badge Node.js badge Express badge MongoDB badge Jest badge Material UI badge Weaviate badge FAISS badge LangChain badge npm badge Vercel badge Render badge Swagger badge Docker badge Jest badge React Testing Library badge Git badge Postman badge Axios badge React Router badge Babel badge Webpack badge CRACO badge React Hook Form badge React Toastify badge React Credit Cards badge

Live Deployment

The application is deployed live on Vercel. You can access it at the following URL: Fusion Electronics App.

The primary backend server is deployed on Vercel and can be accessed at the following URL: Fusion Electronics API.

The backup backend server is deployed on Render and can be accessed at the following URL: Fusion Electronics API.

Important

Note: The backend server may take a few seconds to wake up if it has been inactive for a while. For your information, it is hosted on the free tier of Render, with 0.1 CPU and 512 MB of memory only, so it may take a bit longer to respond to requests, especially after periods of inactivity.

User Interface

Home Page

Fusion Electronics Homepage

Recommended Products (based on user interaction)

Fusion Electronics Recommended Products

Full Product List

Fusion Electronics Products List

Search Results

Fusion Electronics Search Results

Product Details Page

Fusion Electronics Product Details Page

Cart Page

Fusion Electronics Cart Page

Checkout Page

Fusion Electronics Checkout Page

Login Page

Fusion Electronics Login Page

Register Page

Fusion Electronics Register Page

Forgot Password Page

Fusion Electronics Forgot Password Page

Reset Password Page

Fusion Electronics Reset Password Page

Order Confirmation

Fusion Electronics Order Success Page

Footer

Fusion Electronics Footer

Features

  • Product Management:

    • View a list of products.
    • View detailed product information.
    • Add products to the shopping cart.
  • Shopping Cart:

    • View items in the shopping cart.
    • Remove items from the cart.
    • Calculate total amount of items in the cart.
  • Checkout Process:

    • Enter billing, shipping, and payment information.
    • Simulate the order creation process on the backend.
    • Receive confirmation of order success.

Technologies Used

  • Frontend:

    • React.js
    • Material-UI for styling
    • Axios for API requests
    • react-credit-cards-2 for credit card visualization
    • react-router-dom for routing
    • react-hook-form for form validation
    • react-toastify for toast notifications
    • Jest and React Testing Library for testing
  • Backend:

    • Node.js
    • Express.js
    • MongoDB (with Mongoose ODM)
    • Axios for external API requests
    • JsonWebToken for user authentication
    • Bcrypt for password hashing
    • Dotenv for environment variables
    • Cors for cross-origin resource sharing
    • Swagger for API documentation
    • Nodemon for server hot-reloading
    • Middleware: JWT for securing API endpoints
    • Weaviate for product recommendations with vector database
    • FAISS & LangChain for efficient similarity search
    • Jest for unit and integration testing
  • Development Tools:

    • Jetbrains WebStorm (IDE)
    • Postman (for API testing)
    • Git (version control)
    • npm (package manager)
    • Docker (for containerization)

Project Structure

The project is organized into two main "stacks": The backend is in the backend directory that hosts all product & order data and the frontend is in the root directory. Here is an overview of the project structure:

fullstack-ecommerce/
β”œβ”€β”€ backend/                       # Node.js server files
β”‚   β”œβ”€β”€ config/                    # Configuration files
β”‚   β”‚   └── db.js                  # Database connection
β”‚   β”œβ”€β”€ docs/
β”‚   β”‚   └── swagger.js             # Swagger API documentation setup
β”‚   β”œβ”€β”€ models/                    # Mongoose models
β”‚   β”‚   β”œβ”€β”€ user.js                # User schema
β”‚   β”‚   └── product.js             # Product schema
β”‚   β”œβ”€β”€ routes/                    # Route handlers
β”‚   β”‚   β”œβ”€β”€ products.js            # Product routes
β”‚   β”‚   β”œβ”€β”€ search.js              # Search routes
β”‚   β”‚   └── checkout.js            # Checkout routes
β”‚   β”œβ”€β”€ middleware/                # Middleware functions
β”‚   β”‚   β”œβ”€β”€ auth.js                # Authentication middleware
β”‚   β”œβ”€β”€ scripts/                   # Scripts for various tasks
β”‚   β”‚   β”œβ”€β”€ build-faiss-index.js   # Script to build FAISS index
β”‚   β”‚   β”œβ”€β”€ search-faiss-index.js  # Script to search FAISS index
β”‚   β”‚   β”œβ”€β”€ query-weaviate.js      # Script to query Weaviate
β”‚   β”‚   β”œβ”€β”€ weaviate-upsert.js     # Script to upsert products to Weaviate
β”‚   β”‚   └── sync-weaviate.js       # Script to synchronize products with Weaviate
β”‚   β”œβ”€β”€ seed/                      # Database seed data
β”‚   β”‚   └── productSeeds.js        # Product seed data
β”‚   β”œβ”€β”€ weaviateClient.js          # Weaviate client setup
β”‚   β”œβ”€β”€ faiss.sh                   # FAISS index setup script
β”‚   β”œβ”€β”€ .env                       # Environment variables
β”‚   └── index.js                   # Server entry point
β”œβ”€β”€ public/                        # Frontend public assets
β”‚   β”œβ”€β”€ index.html                 # HTML template
β”‚   β”œβ”€β”€ manifest.json              # Web app manifest
β”‚   └── favicon.ico                # Favicon
β”œβ”€β”€ src/                           # React.js frontend files
β”‚   β”œβ”€β”€ components/                # Reusable components
β”‚   β”‚   β”œβ”€β”€ CheckoutForm.jsx       # Checkout form component
β”‚   β”‚   β”œβ”€β”€ ProductCard.jsx        # Product card component
β”‚   β”‚   β”œβ”€β”€ NavigationBar.jsx      # Navigation bar component
β”‚   β”‚   β”œβ”€β”€ OrderConfirmation.jsx  # Order confirmation component
β”‚   β”‚   β”œβ”€β”€ ProductListing.jsx     # Product listing component
β”‚   β”‚   β”œβ”€β”€ SearchResults.jsx      # Search results component
β”‚   β”‚   └── ShoppingCart.jsx       # Shopping cart component
β”‚   β”œβ”€β”€ dev/                       # Development utilities
β”‚   β”‚   β”œβ”€β”€ index.js               # Development entry point
β”‚   β”‚   β”œβ”€β”€ palette.jsx            # Color palette
β”‚   β”‚   β”œβ”€β”€ preview.jsx            # Component preview
β”‚   β”‚   └── useInitials.js         # Initials hook
β”‚   β”œβ”€β”€ pages/                     # Page components
β”‚   β”‚   β”œβ”€β”€ Cart.jsx               # Cart page component
β”‚   β”‚   β”œβ”€β”€ Checkout.jsx           # Checkout page component
β”‚   β”‚   β”œβ”€β”€ Home.jsx               # Home page component
β”‚   β”‚   β”œβ”€β”€ ProductDetails.jsx     # Product details page component
β”‚   β”‚   β”œβ”€β”€ OrderSuccess.jsx       # Order success page component
β”‚   β”‚   β”œβ”€β”€ ProductDetails.jsx     # Product details page component
β”‚   β”‚   └── Shop.jsx               # Shop page component
β”‚   β”œβ”€β”€ App.jsx                    # Main application component
β”‚   β”œβ”€β”€ App.css                    # Main application styles
β”‚   └── index.js                   # React entry point
β”œβ”€β”€ build/                         # Frontend production build files
β”œβ”€β”€ tests/                         # Test files
β”œβ”€β”€ .gitignore                     # Git ignore file
β”œβ”€β”€ package.json                   # NPM package file
β”œβ”€β”€ jsconfig.json                  # JS config file
└── setupProxy.js                  # Proxy configuration
(... and more files not listed here ...)

Getting Started

Prerequisites

Before running this project, ensure you have the following installed:

  • Node.js (with npm)
  • MongoDB (with either local or remote instance)
  • Git

Installation

  1. Clone the repository:

    git clone https://github.com/hoangsonww/MERN-Stack-Ecommerce-App.git
    cd MERN-Stack-Ecommerce-App  # Fix the path if necessary
  2. Install project dependencies:

    # Install server (backend) dependencies
    cd backend
    npm install
    
    # Note: If you encounter any issues with the backend/package-lock.json not updating, run the following command from root directory:
    npm install --no-workspaces --prefix backend
    
    # Install client (frontend) dependencies
    cd ..
    npm install
  3. Set up the backend:

    • Create a .env file in the backend/ directory and add the following environment variable (replace the URI with your MongoDB connection string):

      MONGO_URI=mongodb://localhost:27017/Ecommerce-Products
      JWT_SECRET=your_secret_key
      

      For your information, I am using MongoDB Atlas for this project. You can create a free account and get your connection string from there if you want to deploy the application to the cloud.

    • Ensure that your MongoDB server is running. If you're using Mac, you can start the MongoDB server with the following command:

    brew services start mongodb-community
    • Seed the database with sample data:

      cd backend/seed
      node productSeeds.js dev
    • Run the backend server: (first cd into the backend directory)

      cd ..
      npm start
  4. Set up the frontend:

    • First, cd into the root directory if you are not already there:
      cd ..
      Or
      cd fullstack-ecommerce
    • Start the frontend development server:
      npm start

Tip

The frontend server will run on http://localhost:3000 by default. If you encounter any errors when starting related to the react-credit-cards-2 package, it is OK to just ignore them as the application will still run correctly.

Running the Application

  • Open your browser and navigate to http://localhost:3000 to view the application.

Product Recommendations with Vector Database

The application uses Weaviate & FAISS as vector databases/stores to provide product recommendations based on vector search. The product data is indexed in Weaviate, allowing for efficient similarity searches and recommendations.

To set up Weaviate for product recommendations, follow these steps:

  1. Sign up for a Weaviate account at Weaviate Cloud.
  2. Create a new Weaviate instance and note the API endpoint.
  3. Get your Weaviate API key from the Weaviate console.
  4. Update the .env file in the backend/ directory with your Weaviate API endpoint and API key:
    WEAVIATE_HOST=https://your-weaviate-instance.weaviate.network
    WEAVIATE_API_KEY=your_weaviate_api_key
    
  5. Run the Weaviate client script to index the product data:
    cd backend
    npm run weaviate-upsert
  6. Synchronize the product data with Weaviate by running the following command:
    npm run sync-weaviate
  7. Start or restart the backend server to apply the changes:
    npm start

Additionally, to set up FAISS & LangChain for efficient similarity search, you can run the following commands in the backend/scripts/ directory:

  1. Build the FAISS index:

    cd backend
    node build-faiss-index.js
  2. Search efficiently with LangChain:

      npm run faiss-search -- "your search text" 5

Replace "your search text" with the text you want to search for, and 5 with the number of results you want to retrieve. It should return something like:

πŸ” Top 5 results for "your search text 5":

1. id=6874a44ee237afdff3374d27    distance=1.2893
2. id=6874a44ee237afdff3374d25    distance=1.3328
3. id=6874a44ee237afdff3374d23    distance=1.3522
4. id=6874a44ee237afdff3374d2e    distance=1.3739
5. id=6874a44ee237afdff3374d28    distance=1.3753

Now, the application will use Weaviate & FAISS & LangChain to provide product recommendations based on vector search. When users view a product, they will see recommended products based on similarity to the viewed product. Try going to the product details page of any product, and you will see a list of recommended products based on the current product!

Testing the APIs

  • Simply open your browser and navigate to http://localhost:5000/api/products to view the list of products.
  • You can also change the sample data by navigating to backend/seed/productSeeds.js and modifying the data there.

Unit & Integration Testing

We have implemented unit and integration tests for the application using Jest and React Testing Library. To run the tests, follow these steps:

Backend Tests

cd backend

# Run backend tests (default mode)
npm run test

# Run frontend tests (watch mode - this will automatically re-run tests on file changes)
npm run test:watch

# Run frontend tests (coverage mode - this will generate a coverage report)
npm run test:coverage

Frontend Tests

cd .. # if you are still in the backend directory

# Run frontend tests (default mode)
npm run test

# Run frontend tests (watch mode - this will automatically re-run tests on file changes)
npm run test:watch

# Run frontend tests (coverage mode - this will generate a coverage report)
npm run test:coverage

Note

If you encounter any issues when running the tests, ensure that you have run npm install in both the backend and root (frontend) directories to install all necessary dependencies.

Also, if the issue persists, try removing the node_modules directory and the package-lock.json file in both directories, and then run npm install again to reinstall all dependencies.

Swagger API Documentation

  • The backend server includes Swagger API documentation that can be accessed at http://localhost:5000/api-docs.
  • Before accessing the above URL, ensure that the backend server is running.
  • The Swagger UI provides a detailed overview of the API endpoints, request/response schemas, and example requests.
  • If you have everything set up correctly, you should see the Swagger UI documentation page:

The MovieVerse App Interface

OpenAPI Specification

Using the openapi.yaml File

  1. View the API Documentation
  • Open Swagger Editor.
  • Upload the openapi.yaml file or paste its content.
  • Visualize and interact with the API documentation.
  1. Test the API
  • Import openapi.yaml into Postman:
    • Open Postman β†’ Import β†’ Select openapi.yaml.
    • Test the API endpoints directly from Postman.
  • Or use Swagger UI:
    • Provide the file URL or upload it to view and test endpoints.
  1. Generate Client Libraries
  • Install OpenAPI Generator:
    npm install @openapitools/openapi-generator-cli -g
  • Generate a client library:
    openapi-generator-cli generate -i openapi.yaml -g <language> -o ./client
  • Replace <language> with the desired programming language.
  1. Generate Server Stubs
  • Generate a server stub:
    openapi-generator-cli generate -i openapi.yaml -g <framework> -o ./server
  • Replace <framework> with the desired framework.
  1. Run a Mock Server
  • Install Prism:
    npm install -g @stoplight/prism-cli
  • Start the mock server:
    prism mock openapi.yaml
  1. Validate the OpenAPI File
  • Use Swagger Validator:
    • Upload openapi.yaml or paste its content to check for errors.

This guide enables you to view, test, and utilize the API. You can generate client libraries, server stubs, and run a mock server using the OpenAPI Specification.

Deployment

To deploy the application:

  • Configure deployment settings for both frontend (React) and backend (Node.js) according to your chosen hosting provider (e.g., AWS, Heroku, Netlify).

Containerization

This project can be containerized using Docker. First, ensure that Docker Desktop is running on your system. Then, to create a Docker image, run the following command:

docker compose up --build

This command will create a Docker image for the frontend and backend, and run the application in a containerized environment.

GitHub Actions & CI/CD

This project includes a GitHub Actions workflow for continuous integration and deployment. The workflow is defined in the .github/workflows/ci.yml file and will automatically run tests and build the application on every push or pull request.

GitHub Actions Workflow

Contributing

Contributions to this project are welcome! Here are some ways you can contribute:

  • Report bugs and request features by opening issues.
  • Implement new features or enhancements and submit pull requests.
  • Improve documentation and README files.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Creator

Fusion Electronics was created with ❀️ by:


Thank you for exploring Fusion Electronics - a MERN Stack E-commerce Application! If you have any questions or feedback, feel free to reach out or open an issue.

Happy coding! πŸš€