This project is an Express application built with TypeScript, integrating MongoDB using Mongoose, and validating data with Zod. The application handles basic product and order management for an e-commerce platform.
- Node.js (v14 or higher)
- npm or yarn
- MongoDB (Local or Atlas)
-
Clone the repository:
git clone https://github.com/your-github-username/your-repo-name.git
-
Navigate to the project directory:
cd your-repo-name
-
Install dependencies:
npm install # or yarn install
-
Set up environment variables:
Create a
.env
file in the root of your project with the following content:MONGO_URI=your-mongodb-connection-string PORT=5000
-
Run the application:
npm start # or yarn start
The API should now be running at
http://localhost:5000
.
- Name: String
- Description: String
- Price: Number
- Category: String
- Tags: Array of Strings
- Variants: Array of Objects
- Type: String
- Value: String
- Inventory: Object
- Quantity: Number
- InStock: Boolean
- Email: String
- ProductId: String (ObjectId of Product)
- Price: Number
- Quantity: Number
-
Create a New Product
- Endpoint:
/api/products
- Method:
POST
- Sample Request Body:
{ "name": "iPhone 13", "description": "A sleek and powerful smartphone.", "price": 999, "category": "Electronics", "tags": ["smartphone", "Apple"], "variants": [ {"type": "Color", "value": "Midnight Blue"}, {"type": "Storage", "value": "256GB"} ], "inventory": { "quantity": 50, "inStock": true } }
- Endpoint:
-
Retrieve All Products
- Endpoint:
/api/products
- Method:
GET
- Sample Response:
{ "success": true, "message": "Products fetched successfully!", "data": [ { "name": "iPhone 13", "description": "A sleek and powerful smartphone.", "price": 999, "category": "Electronics", "tags": ["smartphone", "Apple"], "variants": [ {"type": "Color", "value": "Midnight Blue"}, {"type": "Storage", "value": "256GB"} ], "inventory": { "quantity": 50, "inStock": true } } // More products... ] }
- Endpoint:
-
Retrieve a Specific Product by ID
- Endpoint:
/api/products/:productId
- Method:
GET
- Endpoint:
-
Update Product Information
- Endpoint:
/api/products/:productId
- Method:
PUT
- Sample Request Body: Same as creating a new product
- Endpoint:
-
Delete a Product
- Endpoint:
/api/products/:productId
- Method:
DELETE
- Endpoint:
-
Search a Product
- Endpoint:
/api/products?searchTerm=keyword
- Method:
GET
- Sample Response:
{ "success": true, "message": "Products matching search term 'keyword' fetched successfully!", "data": [ // Matching products... ] }
- Endpoint:
-
Create a New Order
- Endpoint:
/api/orders
- Method:
POST
- Sample Request Body:
{ "email": "example@example.com", "productId": "5fd67e890b60c903cd8544a3", "price": 999, "quantity": 1 }
- Endpoint:
-
Retrieve All Orders
- Endpoint:
/api/orders
- Method:
GET
- Endpoint:
-
Retrieve Orders by User Email
- Endpoint:
/api/orders?email=user@example.com
- Method:
GET
- Endpoint:
- Product Creation/Update: Ensured through Zod to validate fields like name, price, and inventory.
- Order Creation: Validated to ensure sufficient stock and valid pricing.
- Insufficient Quantity: Returns an error if the ordered quantity exceeds available stock.
- Not Found: Handles cases where requested routes are not found.
- Validation Errors: Provides meaningful error messages for invalid data inputs.
Ensure code quality with ESLint:
npm run lint
# or
yarn lint
Run tests with:
npm test
# or
yarn test
To deploy the application, make sure to:
- Set up your environment variables for the production database and any other necessary configurations.
- Build the project (if needed):
npm run build # or yarn build
- Start the application in production mode:
npm start # or yarn start
- Live Server Link: https://shop-venture-backend.vercel.app/