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.
- User Registration and Authentication
- User registration
- JWT-based authentication
- Product Management
- Full CRUD operations for products
- Seeding script to import products from an external API (
importProducts.js)
- User Account Management
- Update user information (
src/controllers/userController.js) - Account deletion (
src/controllers/userController.js)
- Update user information (
- Shopping Cart Management
- Add/remove items
- Update quantities
- View cart summary (
src/controllers/cartController.js)
- Order Management
- Place orders via checkout
- View order history (Functionality may need implementation/verification)
- Checkout Process
- Transactional checkout process (
src/controllers/checkoutController.js) - Creates an order and order items, then clears the cart.
- Transactional checkout process (
- API Documentation
- Interactive Swagger UI (
/api-docs)
- Interactive Swagger UI (
- Interactive Frontend (
client/)- React-based client for seamless user experience (
client/src/App.js) - Uses Tailwind CSS for styling (
client/src/index.css) - Interacts with backend API via Axios (
client/src/services/api.js)
- React-based client for seamless user experience (
- Backend:
- Node.js
- Express (
src/app.js) - PostgreSQL
- Sequelize ORM (
src/config/db.js,models/,src/models/) - Swagger UI
- JWT (for authentication)
- bcrypt (for password hashing)
- dotenv (for environment variables)
- CORS
- Git
- Frontend:
- React (
client/src/App.js) - Tailwind CSS (
client/tailwind.config.js) - Axios
- React Router
- Stripe.js (for payment element in
client/src/pages/Checkout.jsx)
- React (
- Clone the repository:
git clone <repository-url> cd ecommerce-api
- Install dependencies:
npm install
- Configure Environment:
Create a
.envfile in the root directory with the following variables. UseDATABASE_URLor the individualDB_*variables depending on yoursrc/config/db.jssetup.# 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)
- Database Setup:
- Ensure PostgreSQL is installed and running.
- Create your database using a tool like
psqlor a GUI:createdb <database-name> - Run migrations to create tables:
npx sequelize-cli db:migrate(.sequelizerc,src/migrations/)
- Seed Database (Optional but Recommended):
Populate the database with initial product data using the import script. Ensure your
.envfile is configured correctly for the target database (local or production).node importProducts.js
- Start the server:
The API should be running at
npm run start # Or node src/server.jshttp://localhost:PORT(e.g.,http://localhost:3000). Access Swagger docs at/api-docs.
Frontend Setup (client/)
- Navigate to the client directory:
cd client - Install dependencies:
npm install
- Configure Environment:
Create a
.envfile in theclientdirectory:REACT_APP_API_URL=http://localhost:3000/api # Adjust port if backend runs elsewhere REACT_APP_STRIPE_PUBLIC_KEY=<your_stripe_publishable_key>
- Start the client:
The React app should open in your browser, typically at
npm start
http://localhost:3001.
This application is configured for deployment on platforms like Render. Key considerations:
- Set environment variables (
DATABASE_URLorDB_*,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_URLcorrectly points to the deployed frontend URL for CORS. - Ensure the frontend service's
REACT_APP_API_URLcorrectly 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.jsscript against the production database (e.g., via Render's shell or by temporarily configuring your local environment) to seed initial data.
Refer to the Swagger documentation available at /api-docs when the backend server is running.