https://github.com/mastermind-fa/product-inventory-api
A secure CRUD API built with Node.js, Express.js, and MongoDB for managing product inventory. It supports authentication via JWT, ensuring only authorized access to product management. Features include creating, reading, updating, and deleting products, along with filtering and sorting options to efficiently retrieve product data.
https://github.com/mastermind-fa/product-inventory-api
express javascript mongodb mongodb-atlas nodejs
Last synced: 3 months ago
JSON representation
A secure CRUD API built with Node.js, Express.js, and MongoDB for managing product inventory. It supports authentication via JWT, ensuring only authorized access to product management. Features include creating, reading, updating, and deleting products, along with filtering and sorting options to efficiently retrieve product data.
- Host: GitHub
- URL: https://github.com/mastermind-fa/product-inventory-api
- Owner: mastermind-fa
- Created: 2025-03-03T17:00:18.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-03T17:26:34.000Z (3 months ago)
- Last Synced: 2025-03-03T18:34:33.904Z (3 months ago)
- Topics: express, javascript, mongodb, mongodb-atlas, nodejs
- Language: JavaScript
- Homepage: https://product-inventory-api-iji4.onrender.com
- Size: 3.26 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Product Inventory Management API
This is a RESTful API for managing a product inventory system using Node.js, Express.js, MongoDB, and JWT authentication. It provides CRUD operations, pagination, sorting, filtering, and authentication for secure access.
## Features
- Create, Read, Update, and Delete (CRUD) products
- Pagination support for fetching products
- Sorting products by fields (e.g., price)
- Filtering by category
- JWT-based authentication for secure access
- Protected routes to ensure only authenticated users can modify products
- Error handling for invalid requests## Technologies Used
- **Node.js** – Backend runtime
- **Express.js** – Web framework for handling routes
- **MongoDB & Mongoose** – NoSQL database for storing product data
- **JWT (JSON Web Token)** – Secure authentication
- **dotenv** – Manage environment variables
- **body-parser** – Parse incoming request bodies
- **CORS** – Enable cross-origin requests
- **Nodemon** – Auto-restart for development## Installation
1. Clone the repository:
```sh
git clone https://github.com/mastermind-fa/product-inventory-api
cd product-inventory-api
```2. Install dependencies:
```sh
npm install
```3. Create a `.env` file in the root directory and add the following:
```env
PORT=8080
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
```4. Start the server:
```sh
npm start
```For development with automatic restart:
```sh
npm run dev
```## API Endpoints
### Authentication
| Method | Endpoint | Description |
|--------|----------------|-------------|
| POST | `/api/auth/register` | Register a new user |
| POST | `/api/auth/login` | Authenticate user and receive token |### Product Management (Protected)
| Method | Endpoint | Description |
|--------|---------|-------------|
| GET | `/api/products/?sort=price` | Get all products sorted by price |
| GET | `/api/products/?sort=-price` | Get all products sorted by price in descending order |
| GET | `/api/products/?category=` | Get all products filtered by category |
| GET | `/api/products/?category=&sort=price` | Get all products in the category sorted by price |
| GET | `/api/products/?category=&sort=-price` | Get all products in the category sorted by price in descending order |
| GET | `/api/products/:id` | Get a specific product by ID |
| POST | `/api/products` | Add a new product (requires authentication) |
| PUT | `/api/products/:id` | Update a product (requires authentication) |
| DELETE | `/api/products/:id` | Delete a product (requires authentication) |## Usage Examples
### 1. Register a User
```sh
curl -X POST "http://localhost:8080/api/auth/register" \
-H "Content-Type: application/json" \
-d '{"username": "user1", "password": "password123"}'
```### 2. Login to Get JWT Token
```sh
curl -X POST "http://localhost:8080/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"username": "user1", "password": "password123"}'
```
Response:
```json
{
"token": "your_jwt_token"
}
```### 3. Fetch Products with Authentication
```sh
curl -X GET "http://localhost:8080/api/products" \
-H "Authorization: Bearer your_jwt_token"
```### 4. Create a New Product (Authenticated)
```sh
curl -X POST "http://localhost:8080/api/products" \
-H "Authorization: Bearer your_jwt_token" \
-H "Content-Type: application/json" \
-d '{
"name": "Wireless Headphones",
"price": 49.99,
"category": "",
"stock": 25,
"description": "Bluetooth over-ear headphones with noise cancellation"
}'
```### 5. Fetch Products with Pagination, Sorting, and Filtering
```sh
curl -X GET "http://localhost:8080/api/products?page=1&limit=5&sort=-price&category=" \
-H "Authorization: Bearer your_jwt_token"
```## Deployment
To deploy on **Render, Vercel, or Railway**, follow these steps:
1. Push your project to GitHub
2. Link your repository to the chosen hosting platform
3. Add the required environment variables (`MONGO_URI`, `JWT_SECRET`)
4. Deploy and get the live API URLLive API URL: [https://product-inventory-api-iji4.onrender.com](https://product-inventory-api-iji4.onrender.com)
## License
This project is open-source and available under the MIT License.---
### Author
Developed by **Farhana Alam**For any queries, feel free to reach out!