A complete REST API backend application demonstrating all HTTP methods (GET, POST, PUT, DELETE, OPTIONS) for managing an items store.
The Netlify-version will only use mock data for display. The localhost version is running an actually separate server for the API.
TODO: Attach to separate backend for live API.
- β GET - Retrieve all items or specific item by ID
- β POST - Create new items
- β PUT - Update existing items
- β DELETE - Remove items
- β OPTIONS - CORS preflight support
- π CORS enabled for cross-origin requests
- π± HTML interface for browser testing
- π§ JSON API for programmatic access
- β‘ In-memory data storage (resets on server restart)
# Navigate to project directory
cd node-rest-api
# Install dependencies (if any)
npm install
# Start the server
node server.jsThe server will start on http://localhost:3000
Visit http://localhost:3000 in your browser to see the HTML interface with all items and API documentation.
# Run the test script (server must be running)
node test-endpoints.js# HTML response (default)
curl http://localhost:3000/
# JSON response
curl -H "Accept: application/json" http://localhost:3000/itemsResponse Example:
{
"message": "Items retrieved successfully",
"count": 5,
"items": [
{
"id": 1,
"name": "Apple",
"category": "Fruit",
"price": 0.5,
"inStock": true
}
]
}curl http://localhost:3000/items/1Response Example:
{
"message": "Item retrieved successfully",
"item": {
"id": 1,
"name": "Apple",
"category": "Fruit",
"price": 0.5,
"inStock": true
}
}curl -X POST http://localhost:3000/items \
-H "Content-Type: application/json" \
-d '{
"name": "Orange",
"category": "Fruit",
"price": 0.8,
"inStock": true
}'Required Fields: name, category, price
Optional Fields: inStock (defaults to true)
Response Example:
{
"message": "Item created successfully",
"item": {
"id": 6,
"name": "Orange",
"category": "Fruit",
"price": 0.8,
"inStock": true
}
}curl -X PUT http://localhost:3000/items/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Green Apple",
"price": 0.6
}'Note: You can update any combination of fields. Omitted fields remain unchanged.
Response Example:
{
"message": "Item updated successfully",
"item": {
"id": 1,
"name": "Green Apple",
"category": "Fruit",
"price": 0.6,
"inStock": true
}
}curl -X DELETE http://localhost:3000/items/1Response Example:
{
"message": "Item deleted successfully",
"deletedItem": {
"id": 1,
"name": "Apple",
"category": "Fruit",
"price": 0.5,
"inStock": true
}
}curl -X OPTIONS http://localhost:3000/itemsResponse: Empty body with CORS headers for cross-origin requests.
The API provides comprehensive error responses:
{
"error": "Bad Request",
"message": "Missing required fields: name, category, price",
"required": ["name", "category", "price"],
"optional": ["inStock"]
}{
"error": "Item not found",
"message": "Item with ID 999 does not exist"
}{
"error": "Internal Server Error",
"message": "An unexpected error occurred"
}Each item has the following structure:
{
"id": number, // Auto-generated unique identifier
"name": string, // Item name (required)
"category": string, // Item category (required)
"price": number, // Item price (required)
"inStock": boolean // Availability status (optional, defaults to true)
}The API includes full CORS support with the following headers:
Access-Control-Allow-Origin: *Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONSAccess-Control-Allow-Headers: Content-Type, Authorization
A complete web-based demo application with Material Design that provides an intuitive interface for managing items through the REST API. Features professional UI/UX with modern design principles.
π Quick Start (Local Development):
# Start both API server and demo application
./start-demo.sh
# Or manually:
node server.js # Terminal 1 - API server (port 3000)
node index.js # Terminal 2 - Demo app (port 8080)π Static Deployment (Netlify/Vercel):
# Serve static files (demo mode with mock data)
npm run serve
# Deploy to Netlify
npm run deploy:netlify
# Deploy to Vercel
npm run deploy:vercelOpen in browser: http://localhost:8080
- π¨ Material Design UI - Professional design following Google's Material Design principles
- π± Responsive Interface - Adaptive layout for desktop, tablet, and mobile
- π Statistics Dashboard - Material Design cards with real-time data
- β‘ Floating Action Button - Quick access to add new items
- ποΈ Material Components - Buttons, forms, alerts, and navigation following MD specs
- π Smooth Animations - Elevation changes, hover effects, and transitions
- π Consistent Theming - Material Design color palette and typography
- π Interactive Lists - Material Design list styling with icons and actions
- π Snackbar Alerts - Material Design notification system
- οΏ½ Auto-Detection - Smart environment detection (development vs production)
- οΏ½ Static Deployment Ready - Deploy to Netlify, Vercel, GitHub Pages
- π Dual Mode Operation - API integration + standalone demo mode
- π Progressive Enhancement - Works offline with mock data
- βοΈ Environment Detection - Automatic fallback from API to demo mode
- π§ Easy Configuration - Simple config for different API endpoints
Web Browser (localhost:8080)
β HTTP Requests
Demo Server (index.js)
β Proxy/Static Files
REST API Server (server.js - localhost:3000)
β CRUD Operations
In-Memory Data Store
# Quick start (recommended)
./start-demo.sh start # Start both servers
./start-demo.sh stop # Stop both servers
./start-demo.sh status # Check server status
./start-demo.sh test # Run API tests
./start-demo.sh open # Open demo in browser
# NPM scripts
npm run dev # Start both servers
npm run api # Start only API server
npm run demo # Start only demo application
npm run test-api # Run automated API tests
npm run demo-bash # Run bash-based API demonode-rest-api/
βββ server.js # REST API server (port 3000)
βββ index.js # Demo application server (port 8080)
βββ index.html # Demo web interface
βββ items.json # Initial data (5 sample items)
βββ test-endpoints.js # Automated Node.js test script
βββ demo.sh # Bash script for API testing
βββ start-demo.sh # Complete startup script
βββ package.json # Project metadata with scripts
βββ README.md # This documentation
Check out this Coursera short course to learn the basics of Node.js CRUD API apps.
- Persistent data storage (database integration)
- Authentication and authorization
- Input validation and sanitization
- Rate limiting
- API versioning
- Logging and monitoring
- Import/export data
- Should 'in stock' be changed automatically when item count is zero? Or should 'in stock' be renamed to 'available for purchase'?
- Changing unit numbers is possible inside the list β desirable or not?
Running demo / dev env:
# start app and api
bash start-demo.sh
# check if server/app running
bash start-demo.sh status
# stop app and api
bash start-demo.sh stop