Simple CRUD Apps - Full-Stack Product Management SystemΒΆ
A complete full-stack CRUD (Create, Read, Update, Delete) application featuring both a REST API backend and an interactive web frontend. Built with Node.js, Express.js, MongoDB (Mongoose), and vanilla JavaScript, deployed on Vercel. The codebase uses a lightweight separation of concerns (routes β controllers β models) while serving the frontend as static files from the same Express app.
π OverviewΒΆ
This project demonstrates a complete full-stack development workflow:
- Backend API: Express.js REST API with MongoDB integration
- Frontend Interface: Vanilla JS web app served from
public/ - Database: MongoDB Atlas cloud database
- Deployment: Hosted on Vercel at https://simple-crud-apps.vercel.app
- Architecture: Single Express app serves both API routes and static frontend assets
- Features: Real-time CRUD operations, responsive design, modal dialogs, notifications, basic client/server validation
π‘ InspirationΒΆ
This project was inspired by the YouTube tutorial: Build Node.js API from Scratch
πββοΈ Running the Project LocallyΒΆ
PrerequisitesΒΆ
- Node.js (v16 or higher)
- MongoDB Atlas account
- Git for cloning
Installation StepsΒΆ
-
Clone the Repository
-
Install Dependencies
-
Setup Environment Variables Create a
.envfile in the root directory: -
Start Development Server
-
Verify Installation
- Server runs on:
http://localhost:3000 - Web interface:
http://localhost:3000(opens the frontend application) - API endpoint:
GET http://localhost:3000/api/products - Test the full application by adding, updating, and deleting products through the web interface
Project StructureΒΆ
βββ controllers/
β βββ product.controller.js # Business logic for CRUD operations
βββ models/
β βββ product.models.js # Mongoose schema definition
βββ routes/
β βββ product.route.js # API route definitions
βββ public/
β βββ index.html # Frontend entry (vanilla JS)
β βββ css/
β β βββ styles.css # UI styles
β βββ js/
β βββ app.js # Frontend logic (Fetch API CRUD + modals)
βββ index.js # Application entry point
βββ package.json # Dependencies and scripts
βββ vercel.json # Vercel deployment configuration
βββ README.md
π§© Architecture (Current)ΒΆ
Request Flow (Local + Production)ΒΆ
- Single runtime: One Express app (
index.js) handles both the REST API and the static frontend. - API first:
/api/productsis mounted before static middleware. - Static frontend:
express.static('public')serves/index.html,/css/styles.css, and/js/app.js. - SPA-style fallback:
GET /returnspublic/index.html(non-API routes are intended to be handled by the static middleware / fallback). - Frontend β API: The browser calls relative URLs like
fetch('/api/products'), so it works on bothlocalhostand Vercel without changing base URLs.
Backend LayeringΒΆ
- Routes (
routes/product.route.js): Defines REST endpoints for products. - Controllers (
controllers/product.controller.js): Implements CRUD using Mongoose and returns JSON responses. - Products list is sorted by newest first:
sort({ createdAt: -1 }) - Basic non-negative validation is enforced for
priceandquantityon create/update. - Models (
models/product.models.js):Productschema withtimestamps: trueand field validation (min: 0forpriceandquantity).
Deployment on Vercel (Serverless)ΒΆ
- Entrypoint:
vercel.jsonbuildsindex.jswith@vercel/node. - Routing: All paths are routed to
index.js, so Express serves both: - API under
/api/products - Frontend assets under
/public/*(via Express static middleware) - Local vs production: In non-production, the app listens on port
3000; in production it only exports the Express app for Vercel.
ποΈ CRUD Application StructureΒΆ
API EndpointsΒΆ
The application provides a complete REST API for product management:
| Method | Endpoint | Description | Body Required |
|---|---|---|---|
GET |
/api/products |
Get all products | No |
GET |
/api/products/:id |
Get single product | No |
POST |
/api/products |
Create new product | Yes |
PUT |
/api/products/:id |
Update existing product | Yes |
DELETE |
/api/products/:id |
Delete product | No |
Request/Response ExamplesΒΆ
Create Product (POST)ΒΆ
// Request Body
{
"name": "Laptop",
"quantity": 10,
"price": 15000000
}
// Response
{
"_id": "60d5ecb74e33a81234567890",
"name": "Laptop",
"quantity": 10,
"price": 15000000,
"createdAt": "2023-06-25T10:30:00.000Z",
"updatedAt": "2023-06-25T10:30:00.000Z",
"__v": 0
}
Update Product (PUT)ΒΆ
// Request Body
{
"name": "Gaming Laptop",
"price": 18000000
}
// Response - Updated product with new values
Delete Product (DELETE)ΒΆ
ποΈ MongoDB IntegrationΒΆ
Database ConfigurationΒΆ
- Provider: MongoDB Atlas (Cloud)
- Connection: Mongoose ODM
- Environment Variables:
SCA_DB_NAME: Database usernameSCA_DB_PASSWORD: Database password
Product SchemaΒΆ
{
name: {
type: String,
required: [true, "Please enter product name"]
},
quantity: {
type: Number,
required: true,
default: 0,
min: [0, "Quantity cannot be negative"]
},
price: {
type: Number,
required: true,
default: 0,
min: [0, "Price cannot be negative"]
},
Image: {
type: String,
required: false
}
}
FeaturesΒΆ
- Timestamps: Automatic
createdAtandupdatedAtfields - Validation: Required field validation with custom messages
- MongoDB ObjectId: Automatic ID generation and validation
π¨ Frontend FeaturesΒΆ
User InterfaceΒΆ
- Modern Design: Clean, responsive interface with gradient backgrounds
- Real-time Updates: Live product list updates after CRUD operations
- Modal Dialogs: Smooth update and delete confirmation modals
- Notifications: Toast notifications for user feedback
- Responsive Layout: Mobile-first design that works on all devices
User ExperienceΒΆ
- Form Validation: Client-side validation with user-friendly error messages
- Loading States: Visual feedback during API operations
- Empty States: Helpful messages when no products exist
- Keyboard Navigation: Full keyboard accessibility support
- Error Handling: Graceful error handling with user-friendly messages
Interactive ElementsΒΆ
- Add Products: Intuitive form with real-time validation
- Update Products: Pre-filled modal with current values
- Delete Products: Confirmation dialog to prevent accidental deletion
- Product Display: Organized table view with all product details
π Deployment on VercelΒΆ
Live ApplicationΒΆ
- URL: https://simple-crud-apps.vercel.app
- Status: Production ready
- Environment: Node.js runtime on Vercel
Deployment FeaturesΒΆ
- Automatic Deployment: Triggered by GitHub pushes
- Environment Variables: Securely stored MongoDB credentials
- Serverless: Express runs as a Vercel Serverless Function (
@vercel/node) - Global CDN: Fast worldwide access
- Static File Serving: Frontend files are served by Express from the
/publicdirectory - Mixed Content: Both API and UI are served from the same domain/runtime
π Frontend ApplicationΒΆ
Web Interface FeaturesΒΆ
- Live Demo: Visit https://simple-crud-apps.vercel.app to see the application in action
- Product Management: Complete CRUD operations through an intuitive web interface
- Real-time Updates: Changes are immediately reflected in the product list
- Responsive Design: Works seamlessly on desktop, tablet, and mobile devices
- Modern UI: Clean, professional interface with smooth animations and transitions
Key Frontend ComponentsΒΆ
- Product Form: Add new products with validation
- Product List: View all products in an organized table format
- Update Modal: Edit existing products with pre-filled forms
- Delete Confirmation: Safe deletion with confirmation dialogs
- Notifications: Real-time feedback for all operations
π§ͺ API TestingΒΆ
For comprehensive API testing and automation, check out the dedicated testing repository:
- GitHub Repository: https://github.com/fahmi-wiradika/newman-automation
- Live Test Results: https://fahmi-wiradika.github.io/newman-automation
The testing repository includes: - Automated Newman/Postman test collections - CI/CD pipeline with GitHub Actions - Performance reporting and monitoring - Both local and production environment testing
π οΈ Development ScriptsΒΆ
{
"scripts": {
"dev": "nodemon index.js", // Development with auto-reload
"serve": "node index.js", // Production server
"test": "echo \"Error: no test specified\" && exit 1"
}
}
π§° Tech StackΒΆ
BackendΒΆ
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Environment: dotenv for configuration
- Middleware: Express JSON parsing, static file serving
FrontendΒΆ
- Language: Vanilla JavaScript (ES6+)
- Styling: Modern CSS3 with responsive design
- UI Components: Custom modal dialogs, notifications, forms
- API Integration: Fetch API for REST communication
- Features: Real-time updates, form validation, responsive layout
DeploymentΒΆ
- Platform: Vercel (Serverless)
- Database: MongoDB Atlas (Cloud)
- Domain: Custom Vercel domain
- Static Files: Served from
/publicdirectory
π§ TroubleshootingΒΆ
Common IssuesΒΆ
- Database Connection Failed
- Verify MongoDB Atlas credentials
- Check IP whitelist in MongoDB Atlas
-
Ensure environment variables are set correctly
-
Local Server Won't Start
- Check if port 3000 is available
- Verify Node.js installation
-
Install dependencies with
npm install -
Environment Variables Not Loading
- Create
.envfile in root directory - Check
.envfile syntax - Restart server after adding variables
π API Usage ExamplesΒΆ
Using cURLΒΆ
# Get all products
curl -X GET https://simple-crud-apps.vercel.app/api/products
# Create a product
curl -X POST https://simple-crud-apps.vercel.app/api/products \
-H "Content-Type: application/json" \
-d '{"name":"Smartphone","quantity":5,"price":8000000}'
# Get single product
curl -X GET https://simple-crud-apps.vercel.app/api/products/{product_id}
# Update product
curl -X PUT https://simple-crud-apps.vercel.app/api/products/{product_id} \
-H "Content-Type: application/json" \
-d '{"name":"iPhone","price":12000000}'
# Delete product
curl -X DELETE https://simple-crud-apps.vercel.app/api/products/{product_id}
Using JavaScript (Fetch API)ΒΆ
// Get all products
const response = await fetch('https://simple-crud-apps.vercel.app/api/products');
const products = await response.json();
// Create new product
const newProduct = await fetch('https://simple-crud-apps.vercel.app/api/products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Tablet',
quantity: 3,
price: 5000000
})
});
π€ ContributingΒΆ
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π LicenseΒΆ
This project is open source and available under the MIT License.
π Quick LinksΒΆ
- Live API: https://simple-crud-apps.vercel.app
- API Testing Repository: https://github.com/fahmi-wiradika/newman-automation
- Live Test Results: https://fahmi-wiradika.github.io/newman-automation
- YouTube Tutorial: Build Node.js API from Scratch
- MongoDB Atlas: https://www.mongodb.com/cloud/atlas
- Vercel: https://vercel.com
Happy Coding! π