A sample Node.js Express application demonstrating RESTful API design with proper project structure, controllers, services, and models.
- RESTful API Design - Follows REST principles
- Modular Architecture - Controllers, Services, Models separation
- Input Validation - Request validation middleware
- Error Handling - Comprehensive error handling
- Security - Helmet for security headers
- Logging - Morgan for HTTP request logging
- CORS Support - Cross-origin resource sharing enabled
nodejs-api/
├── src/
│ ├── controllers/ # Request handlers
│ │ ├── userController.js
│ │ └── productController.js
│ ├── services/ # Business logic
│ │ ├── userService.js
│ │ └── productService.js
│ ├── models/ # Data models
│ │ ├── User.js
│ │ └── Product.js
│ ├── routes/ # API routes
│ │ ├── userRoutes.js
│ │ └── productRoutes.js
│ ├── middleware/ # Custom middleware
│ │ └── validation.js
│ └── server.js # Main server file
├── package.json
└── README.md
-
Clone or navigate to the project directory
cd nodejs-api -
Install dependencies
npm install
-
Start the server
# Development mode (with auto-restart) npm run dev # Production mode npm start
- GET
/health- Server health status
- GET
/api/users- Get all users- Query params:
search,role
- Query params:
- GET
/api/users/:id- Get user by ID - POST
/api/users- Create new user - PUT
/api/users/:id- Update user - DELETE
/api/users/:id- Delete user
- GET
/api/products- Get all products- Query params:
search,category,inStock,minPrice,maxPrice
- Query params:
- GET
/api/products/:id- Get product by ID - POST
/api/products- Create new product - PUT
/api/products/:id- Update product - DELETE
/api/products/:id- Delete product - PATCH
/api/products/:id/stock- Update product stock
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"role": "user"
}'curl -X POST http://localhost:3000/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop",
"description": "High-performance laptop",
"price": 999.99,
"category": "Electronics",
"stock": 10
}'curl "http://localhost:3000/api/products?search=laptop&inStock=true"curl -X PATCH http://localhost:3000/api/products/1/stock \
-H "Content-Type: application/json" \
-d '{"quantity": -2}'The server runs on port 3000 by default. You can change this by setting the PORT environment variable:
PORT=8080 npm start- Handle HTTP requests and responses
- Validate input data
- Call appropriate services
- Return formatted responses
- Contain business logic
- Handle data operations
- Perform validations
- Manage data state
- Define data structure
- Provide data manipulation methods
- Handle data validation
- Convert to/from JSON
- Define API endpoints
- Map URLs to controllers
- Apply middleware
- Handle route parameters
- Helmet - Sets security headers
- CORS - Configurable cross-origin requests
- Input Validation - Request data validation
- Error Handling - Secure error responses
The application comes with sample data:
- John Doe (admin)
- Jane Smith (user)
- Bob Johnson (user)
- Laptop (Electronics)
- Smartphone (Electronics)
- Coffee Mug (Accessories)
- Wireless Headphones (Electronics)
- Desk Lamp (Furniture)
- Start the server:
npm run dev - Visit:
http://localhost:3000/health - Test the APIs using the examples above
- Explore the code structure to understand the patterns
This project demonstrates:
- RESTful API design
- MVC architecture pattern
- Separation of concerns
- Error handling strategies
- Input validation
- Express.js best practices
- Node.js project structure
