This is a simple e-commerce application REST API built with Node.js, Express.js, and PostgreSQL. It provides user authentication and supports different functionalities for sellers and buyers.
Clone the repository:
git clone https://github.com/yourusername/your-repo-name.git
Navigate into the project directory:
cd your-repo-name
Install the dependencies:
npm install
Create a .env
file in the root directory and add the following environment variables:
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=shahbaz
DB_NAME=ecommerce
JWT_SECRET=shahbaz
Set up the PostgreSQL database by creating the necessary tables. You can use a migration tool like pg-migrate
or manually execute SQL commands.
Create the following tables in your PostgreSQL database:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) CHECK (role IN ('seller', 'buyer')) NOT NULL
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
category VARCHAR(50) NOT NULL,
seller_id INTEGER REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE cart (
id SERIAL PRIMARY KEY,
buyer_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
product_id INTEGER REFERENCES products(id) ON DELETE CASCADE,
quantity INTEGER NOT NULL
);
## API Endpoints
### User Endpoints
- **Sign Up**: `POST /api/users/signup`
- **Login**: `POST /api/users/login`
### Seller Endpoints
- **Add Product**: `POST /api/seller/products`
- **Edit Product**: `PUT /api/seller/products/:id`
- **Delete Product**: `DELETE /api/seller/products/:id`
### Buyer Endpoints
- **Search Products**: `GET /api/buyer/products`
- **Add to Cart**: `POST /api/buyer/cart`
- **Remove from Cart**: `DELETE /api/buyer/cart/:id`
## Start the development server:
- npm run server
## Testing the API
You can use tools like [ThunderClient](https://www.thunderclient.com/) or [Postman](https://www.postman.com/) to test the API endpoints.