Features β’ Quick Start β’ API Documentation β’ Project Structure β’ Technologies
- Overview
- Features
- Quick Start
- Environment Variables
- API Documentation
- Database Schema
- Project Structure
- Technologies
- Contributing
- License
A production-ready Express.js REST API built with MongoDB and following the MVC (Model-View-Controller) architectural pattern. This API features complete JWT authentication, comprehensive CRUD operations, and intelligent inventory management.
Perfect for building e-commerce platforms, order management systems, or any application requiring user authentication and resource management.
|
|
|
|
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- npm or yarn
- MongoDB (v4.4 or higher)
# 1οΈβ£ Clone the repository
git clone https://github.com/yourusername/express-mongo-mvc.git
# 2οΈβ£ Navigate to project directory
cd express-mongo-mvc
# 3οΈβ£ Install dependencies
npm install
# 4οΈβ£ Configure environment variables
cp .env.example .env
# Edit .env with your configuration
# 5οΈβ£ Start MongoDB (if not running)
mongod
# 6οΈβ£ Run the application
npm start
# π₯ For development with auto-reload
npm run devCreate a .env file in the root directory:
# Server Configuration
PORT=5000
NODE_ENV=development
# Database Configuration
MONGO_URI=mongodb://localhost:27017/express_mvc_db
# JWT Configuration
JWT_SECRET=your_super_secret_jwt_key_change_this_in_production
JWT_EXPIRE=7d
β οΈ Security Note: Never commit your.envfile. Always use strong, unique secrets in production!
http://localhost:5000/api
π Authentication Endpoints
POST /auth/signup
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}Response: 201 Created
{
"success": true,
"data": {
"_id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}POST /auth/login
Request Body:
{
"email": "john@example.com",
"password": "password123"
}Response: 200 OK
{
"success": true,
"data": {
"_id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}GET /auth/me
Headers:
Authorization: Bearer YOUR_JWT_TOKEN
Response: 200 OK
{
"success": true,
"data": {
"_id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com"
}
}π₯ Customer Endpoints
Note: All customer endpoints require authentication. Include JWT token in Authorization header.
GET /customers
Response: 200 OK
{
"success": true,
"count": 2,
"data": [
{
"_id": "507f1f77bcf86cd799439011",
"name": "Jane Smith",
"address": "123 Main St, New York, NY",
"salary": 75000,
"createdAt": "2024-01-15T10:30:00.000Z"
}
]
}GET /customers/:id
Response: 200 OK
{
"success": true,
"data": {
"_id": "507f1f77bcf86cd799439011",
"name": "Jane Smith",
"address": "123 Main St, New York, NY",
"salary": 75000
}
}POST /customers
Request Body:
{
"name": "Jane Smith",
"address": "123 Main St, New York, NY",
"salary": 75000
}Response: 201 Created
PUT /customers/:id
Request Body:
{
"name": "Jane Smith Updated",
"salary": 80000
}Response: 200 OK
DELETE /customers/:id
Response: 200 OK
{
"success": true,
"data": {},
"message": "Customer deleted successfully"
}π¦ Product Endpoints
Note: All product endpoints require authentication.
GET /products
Response: 200 OK
{
"success": true,
"count": 3,
"data": [
{
"_id": "507f1f77bcf86cd799439012",
"description": "Laptop Computer",
"unitPrice": 999.99,
"qtyOnHand": 50,
"createdAt": "2024-01-15T10:30:00.000Z"
}
]
}GET /products/:id
POST /products
Request Body:
{
"description": "Gaming Laptop",
"unitPrice": 1299.99,
"qtyOnHand": 25
}Response: 201 Created
PUT /products/:id
Request Body:
{
"unitPrice": 1199.99,
"qtyOnHand": 30
}DELETE /products/:id
π Order Endpoints
Note: All order endpoints require authentication.
GET /orders
Response: 200 OK
{
"success": true,
"count": 1,
"data": [
{
"_id": "507f1f77bcf86cd799439013",
"customer": {
"_id": "507f1f77bcf86cd799439011",
"name": "Jane Smith",
"address": "123 Main St"
},
"date": "2024-01-15T10:30:00.000Z",
"totalAmount": 2599.98,
"productDetails": [
{
"product": {
"_id": "507f1f77bcf86cd799439012",
"description": "Gaming Laptop",
"unitPrice": 1299.99
},
"quantity": 2,
"price": 1299.99
}
]
}
]
}POST /orders
Request Body:
{
"customer": "507f1f77bcf86cd799439011",
"productDetails": [
{
"product": "507f1f77bcf86cd799439012",
"quantity": 2
},
{
"product": "507f1f77bcf86cd799439013",
"quantity": 1
}
]
}Features:
- β
Automatically calculates
totalAmount - β Validates product availability
- β
Updates inventory (
qtyOnHand) - β Sets product price at order time
Response: 201 Created
PUT /orders/:id
Note: Updating an order will:
- Restore old product quantities
- Validate new product availability
- Update inventory with new quantities
- Recalculate total amount
DELETE /orders/:id
Note: Deleting an order automatically restores product inventory.
{
name: String,
email: String (unique),
password: String (hashed),
createdAt: Date,
updatedAt: Date
}{
name: String,
address: String,
salary: Number,
createdAt: Date,
updatedAt: Date
} |
{
description: String,
unitPrice: Number,
qtyOnHand: Number,
createdAt: Date,
updatedAt: Date
}{
customer: ObjectId (ref: Customer),
date: Date,
totalAmount: Number,
productDetails: [{
product: ObjectId (ref: Product),
quantity: Number,
price: Number
}],
createdAt: Date,
updatedAt: Date
} |
express-mongo-mvc/
β
βββ π config/
β βββ db.js # Database connection
β
βββ π models/
β βββ User.js # User schema & methods
β βββ Customer.js # Customer schema
β βββ Product.js # Product schema
β βββ Order.js # Order schema
β
βββ π controllers/
β βββ authController.js # Authentication logic
β βββ customerController.js # Customer CRUD logic
β βββ productController.js # Product CRUD logic
β βββ orderController.js # Order CRUD logic
β
βββ π routes/
β βββ authRoutes.js # Auth endpoints
β βββ customerRoutes.js # Customer endpoints
β βββ productRoutes.js # Product endpoints
β βββ orderRoutes.js # Order endpoints
β
βββ π middleware/
β βββ auth.js # JWT verification
β
βββ π server.js # Application entry point
βββ π package.json # Dependencies
βββ π .env # Environment variables
βββ π .gitignore # Git ignore rules
βββ π README.md # Documentation
| Technology | Description |
|---|---|
| JavaScript Runtime | |
| Web Framework | |
| NoSQL Database | |
| ODM Library | |
| Authentication | |
| Password Hashing |
{
"express": "^4.18.2",
"mongoose": "^8.0.3",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.2",
"dotenv": "^16.3.1",
"cors": "^2.8.5",
"express-validator": "^7.0.1"
}- Import Collection: Import the API endpoints into Postman
- Signup/Login: Get your JWT token
- Set Authorization: Add token to Authorization header as
Bearer YOUR_TOKEN - Test Endpoints: Try all CRUD operations
# Signup
curl -X POST http://localhost:5000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:5000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"password123"}'
# Get Customers (with token)
curl -X GET http://localhost:5000/api/customers \
-H "Authorization: Bearer YOUR_TOKEN_HERE"β
MVC Architecture - Separation of concerns
β
RESTful API Design - Standard HTTP methods
β
Error Handling - Comprehensive error responses
β
Data Validation - Input validation on all endpoints
β
Security - JWT authentication & password hashing
β
Code Organization - Modular and maintainable
β
Environment Variables - Configuration management
β
CORS Enabled - Cross-origin resource sharing
| Code | Description |
|---|---|
200 |
β Success |
201 |
β Created |
400 |
β Bad Request |
401 |
β Unauthorized |
404 |
β Not Found |
500 |
β Server Error |
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the ISC License.
- Node.js Community
- Express.js Team
- MongoDB Team
- All contributors and supporters