A simple and robust CRUD (Create, Read, Update, Delete) API built with Express.js for managing items.
- ✅ Full CRUD operations (Create, Read, Update, Delete)
- ✅ Input validation
- ✅ Error handling
- ✅ Filtering and search capabilities
- ✅ CORS enabled
- ✅ Security headers with Helmet
- ✅ Request logging with Morgan
- ✅ RESTful API design
- ✅ JSON responses
- Clone the repository or navigate to the project directory
- Install dependencies:
npm install
npm run devnpm startThe server will start on http://localhost:3000 (or the port specified in the PORT environment variable).
http://localhost:3000/api
- GET
/ - Description: Get API information and available endpoints
- Response:
{ "message": "Welcome to the CRUD API", "version": "1.0.0", "endpoints": { "GET /api/items": "Get all items", "GET /api/items/:id": "Get item by ID", "POST /api/items": "Create new item", "PUT /api/items/:id": "Update item by ID", "DELETE /api/items/:id": "Delete item by ID" } }
- GET
/api/items - Description: Retrieve all items with optional filtering
- Query Parameters:
category(string): Filter by categoryminPrice(number): Filter by minimum pricemaxPrice(number): Filter by maximum pricesearch(string): Search in name and description
- Example:
/api/items?category=electronics&minPrice=50&search=sample - Response:
{ "success": true, "count": 3, "data": [ { "id": 1, "name": "Sample Item 1", "description": "This is a sample item", "category": "electronics", "price": 99.99, "createdAt": "2024-01-01T00:00:00.000Z" } ] }
- GET
/api/items/:id - Description: Retrieve a specific item by its ID
- Parameters:
id(number) - Item ID - Response:
{ "success": true, "data": { "id": 1, "name": "Sample Item 1", "description": "This is a sample item", "category": "electronics", "price": 99.99, "createdAt": "2024-01-01T00:00:00.000Z" } }
- POST
/api/items - Description: Create a new item
- Request Body:
{ "name": "New Item", "description": "Description of the new item", "category": "electronics", "price": 149.99 } - Response:
{ "success": true, "message": "Item created successfully", "data": { "id": 4, "name": "New Item", "description": "Description of the new item", "category": "electronics", "price": 149.99, "createdAt": "2024-01-01T00:00:00.000Z", "updatedAt": "2024-01-01T00:00:00.000Z" } }
- PUT
/api/items/:id - Description: Update an existing item
- Parameters:
id(number) - Item ID - Request Body:
{ "name": "Updated Item Name", "description": "Updated description", "category": "books", "price": 29.99 } - Response:
{ "success": true, "message": "Item updated successfully", "data": { "id": 1, "name": "Updated Item Name", "description": "Updated description", "category": "books", "price": 29.99, "createdAt": "2024-01-01T00:00:00.000Z", "updatedAt": "2024-01-01T01:00:00.000Z" } }
- DELETE
/api/items/:id - Description: Delete an item by its ID
- Parameters:
id(number) - Item ID - Response:
{ "success": true, "message": "Item deleted successfully", "data": { "id": 1, "name": "Deleted Item", "description": "This item was deleted", "category": "electronics", "price": 99.99, "createdAt": "2024-01-01T00:00:00.000Z" } }
{
"success": false,
"message": "Validation failed",
"errors": [
"Name is required",
"Price must be a valid positive number"
]
}{
"success": false,
"message": "Item not found"
}{
"success": false,
"message": "Something went wrong!",
"error": "Internal server error"
}curl -X GET http://localhost:3000/api/itemscurl -X GET http://localhost:3000/api/items/1curl -X POST http://localhost:3000/api/items \
-H "Content-Type: application/json" \
-d '{
"name": "Test Item",
"description": "A test item",
"category": "test",
"price": 25.99
}'curl -X PUT http://localhost:3000/api/items/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Test Item",
"description": "An updated test item",
"category": "updated",
"price": 35.99
}'curl -X DELETE http://localhost:3000/api/items/1Each item has the following structure:
{
id: number, // Unique identifier (auto-generated)
name: string, // Item name (required)
description: string, // Item description (required)
category: string, // Item category (required)
price: number, // Item price (required, must be positive)
createdAt: string, // ISO timestamp (auto-generated)
updatedAt: string // ISO timestamp (auto-updated)
}- Express.js - Web framework
- CORS - Cross-Origin Resource Sharing
- Helmet - Security headers
- Morgan - HTTP request logger
- Node.js - Runtime environment
yeasty-pink/
├── index.js # Main application file
├── package.json # Dependencies and scripts
├── README.md # Documentation
└── node_modules/ # Dependencies
This API currently uses in-memory storage. To add database support:
- Install a database driver (e.g.,
mongoosefor MongoDB,pgfor PostgreSQL) - Replace the in-memory
itemsarray with database operations - Add connection configuration
- Update the helper functions to use database queries
You can configure the following environment variables:
PORT- Server port (default: 3000)NODE_ENV- Environment mode (development/production)
ISC