A robust RESTful API for managing tasks with full CRUD operations, built with Node.js, Express.js, and MongoDB.
This API allows you to create, read, update, and delete tasks with persistent storage. Each task contains an ID, title, status (pending/completed), and creation timestamp. The API includes input validation, error handling, and filtering capabilities.
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - NoSQL database
- Mongoose - MongoDB object modeling
- express-validator - Input validation middleware
- CORS - Cross-origin resource sharing
- dotenv - Environment variable management
task-manager-api/
โโโ config/
โ โโโ db.js # Database connection configuration
โโโ controllers/
โ โโโ taskController.js # Request handlers and business logic
โโโ models/
โ โโโ Task.js # MongoDB schema definition
โโโ routes/
โ โโโ tasks.js # API route definitions
โโโ .env.example # Environment variables template
โโโ .gitignore # Git ignore rules
โโโ package.json # Project dependencies and scripts
โโโ README.md # Project documentation
โโโ server.js # Main application entry point
http://localhost:3000/tasks
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
| GET | /tasks |
Get all tasks | None |
| GET | /tasks?status=pending |
Get filtered tasks | None |
| GET | /tasks/:id |
Get single task | None |
| POST | /tasks |
Create new task | `{ "title": "string", "status": "pending |
| PUT | /tasks/:id |
Update task | `{ "title": "string", "status": "pending |
| DELETE | /tasks/:id |
Delete task | None |
status(optional): Filter tasks by status (pendingorcompleted)sortBy(optional): Sort field (default:created_at)order(optional): Sort order (ascordesc, default:desc)
Request:
POST /api/tasks
{
"title": "Task-1",
"status": "pending"
}Response:
{
"success": true,
"data": {
"_id": "686451eb2720807e376bc324",
"title": "Task-1",
"status": "pending",
"created_at": "2025-07-01T21:23:55.915Z",
"createdAt": "2025-07-01T21:23:55.918Z",
"updatedAt": "2025-07-01T21:23:55.918Z",
"__v": 0
}
}Request:
GET /tasksResponse:
{
"success": true,
"count": 2,
"data": [
{
"_id": "686451eb2720807e376bc324",
"title": "Task-1",
"status": "pending",
"created_at": "2025-07-01T21:23:55.915Z",
"createdAt": "2025-07-01T21:23:55.918Z",
"updatedAt": "2025-07-01T21:23:55.918Z",
"__v": 0
},
{
"_id": "6864044845dfef0184859988",
"title": "Attend meeting",
"status": "pending",
"created_at": "2025-07-01T15:52:40.159Z",
"createdAt": "2025-07-01T15:52:40.165Z",
"updatedAt": "2025-07-01T15:52:40.165Z",
"__v": 0
}
]
}Request:
PUT /tasks/6864044845dfef0184859988
{
"status": "completed"
}Response:
{
"success": true,
"data": {
"_id": "6864044845dfef0184859988",
"title": "Attend meeting",
"status": "completed",
"created_at": "2025-07-01T15:52:40.159Z",
"createdAt": "2025-07-01T15:52:40.165Z",
"updatedAt": "2025-07-01T15:52:40.165Z",
"__v": 0
}
}- Node.js (v14 or higher)
- MongoDB (local installation or MongoDB Atlas)
- npm or yarn package manager
-
Clone the repository:
git clone <your-repo-url> cd task-manager-api
-
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env
Edit
.envfile with your configuration:MONGODB_URI= PORT= NODE_ENV=
-
Start MongoDB:
- For local MongoDB:
mongod - For MongoDB Atlas: Use the connection string in MONGODB_URI
- For local MongoDB:
-
Run the application:
Development mode (with auto-restart):
npm run dev
Production mode:
npm start
-
Verify the API is running: Open browser or Postman and visit:
http://localhost:3000
- Create Task: POST
/taskswith body{"title": "Test Task"} - Get All Tasks: GET
/tasksto see the created task - Update Task: PUT
/tasks/{id}with body{"status": "completed"} - Filter Completed: GET
/tasks?status=completed - Delete Task: DELETE
/tasks/{id}
- Invalid Create: POST
/taskswith empty body (should return validation error) - Invalid ID: GET
/tasks/invalid-id(should return format error) - Not Found: GET
/tasks/65f123abc456def789012999(should return 404)
Create a .env file in the root directory with the following variables:
# MongoDB connection string
# For local MongoDB:
MONGODB_URI=mongodb://localhost:27017/TaskManagerAPI
# For MongoDB Atlas:
# MONGODB_URI=your_mongodb_connection_string_here
# Server port (default: 3000)
PORT=3000- MONGODB_URI: Database connection string
- PORT: Server port number
The API includes comprehensive error handling:
- 400 Bad Request: Invalid input data or malformed requests
- 404 Not Found: Resource doesn't exist
- 500 Internal Server Error: Server-side errors
Example error response:
{
"success": false,
"error": "Validation Error",
"details": [
{
"msg": "Title is required",
"param": "title",
"location": "body"
}
]
}- Title: Required, 1-100 characters, trimmed
- Status: Optional, must be 'pending' or 'completed'
- ID: Must be valid MongoDB ObjectId format
- โ Full CRUD operations
- โ Input validation with detailed error messages
- โ MongoDB integration with Mongoose
- โ Task filtering by status
- โ Sorting capabilities
- โ CORS enabled
- โ Environment-based configuration
- โ Comprehensive error handling
- โ RESTful API design
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request