DEV Community

Cover image for Task Manager API
Ameh Mathias Ejeh
Ameh Mathias Ejeh

Posted on

Task Manager API

A RESTful API for managing tasks built with Node.js and Express.
Repo

Features

  • CRUD operations for tasks
  • In-memory data storage
  • Status filtering
  • Pagination support
  • Modern ES6+ JavaScript
  • Comprehensive error handling
  • Request logging middleware

Task Entity

Each task has the following properties:

  • id (UUID) - Unique identifier
  • title (string) - Task title (required)
  • description (string) - Task description (optional)
  • status (enum) - One of: "pending", "in-progress", "completed"
  • createdAt (timestamp) - Creation timestamp
  • updatedAt (timestamp) - Last update timestamp

API Endpoints

Get All Tasks

GET /api/tasks
Enter fullscreen mode Exit fullscreen mode

Query parameters:

  • status - Filter by status (pending, in-progress, completed)
  • page - Page number for pagination (default: 1)
  • limit - Number of items per page (default: 10)

Example: GET /api/tasks?status=completed&page=1&limit=5

Get Task by ID

GET /api/tasks/:id
Enter fullscreen mode Exit fullscreen mode

Create New Task

POST /api/tasks
Content-Type: application/json

{
  "title": "Complete project",
  "description": "Finish the task manager API",
  "status": "pending"
}
Enter fullscreen mode Exit fullscreen mode

Update Task

PUT /api/tasks/:id
Content-Type: application/json

{
  "title": "Updated title",
  "description": "Updated description",
  "status": "in-progress"
}
Enter fullscreen mode Exit fullscreen mode

Delete Task

DELETE /api/tasks/:id
Enter fullscreen mode Exit fullscreen mode

Installation & Setup

  1. Prerequisites: Ensure you have Node.js installed

  2. Install dependencies:

   npm install
Enter fullscreen mode Exit fullscreen mode
  1. Run the server:
   # Production mode
   npm start

   # Development mode with auto-restart
   npm run dev
Enter fullscreen mode Exit fullscreen mode
  1. Server will start on: http://localhost:3000

Project Structure

task-manager-api/
│   ├── controllers/
│   │   └── taskController.js
│   ├── middleware/
│   │   ├── errorHandler.js
│   │   └── logger.js
│   ├── models/
│   │   └── Task.js
│   ├── routes/
│   │   └── taskRoutes.js
│   ├── services/
│   │   └── taskService.js
│   ├── utils/
│   │   └── validation.js
│   └── server.js
├── .gitignore
├── .env
├── package.json
└── README.md
Enter fullscreen mode Exit fullscreen mode

Example Usage

Create a task

curl -X POST http://localhost:3000/api/tasks \
  -H "Content-Type: application/json" \
  -d '{"title": "Learn Node.js", "description": "Complete Node.js tutorial", "status": "pending"}'
Enter fullscreen mode Exit fullscreen mode

List all tasks

curl http://localhost:3000/api/tasks
Enter fullscreen mode Exit fullscreen mode

Filter completed tasks with pagination

curl "http://localhost:3000/api/tasks?status=completed&page=1&limit=5"
Enter fullscreen mode Exit fullscreen mode

Update a task

curl -X PUT http://localhost:3000/api/tasks/{task-id} \
  -H "Content-Type: application/json" \
  -d '{"status": "completed"}'
Enter fullscreen mode Exit fullscreen mode

Delete a task

curl -X DELETE http://localhost:3000/api/tasks/{task-id}
Enter fullscreen mode Exit fullscreen mode

Status Codes

  • 200 - Success
  • 201 - Created
  • 400 - Bad Request
  • 404 - Not Found
  • 500 - Internal Server Error

Error Response Format

{
  "error": {
    "message": "Error description",
    "code": "ERROR_CODE"
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The API includes sample data and comprehensive logging so you can immediately test all endpoints. This README provides detailed usage examples with curl commands for testing each endpoint.
The code is production-ready with proper error handling, validation, and follows REST API design principles throughout.

Future Improvements

  • Implement user route and authentication.
  • Connect to a database for persistent storage.
  • Generate API docs using Swagger UI
  • Deploy the API to Render, Railway, Heroku or AWS

Author

Ameh Mathias Ejeh LinkedInGitHub

Top comments (0)