Practical Session: API Development

Session Overview
This hands-on session focuses on applying concepts from previous modules to
build a complete Express.js API for a task management application. We'll focus
on best practices, real-world patterns, and troubleshooting common issues.
Project Requirements
- •Create a RESTful API for managing tasks
- •Implement user authentication
- •Add task filtering and sorting capabilities
- •Create proper error handling
- •Implement validation
- •Document the API
- •Set up automated testing
Project Setup
Basic Server Setup
Data Models
Authentication Implementation
Task CRUD Operations
Route Implementation
Testing in Postman
- 1.Register a User
- •POST /api/auth/register
- •Body: { "name": "Test User", "email": "test@example.com", "password":
- 2.Login
- •POST /api/auth/login
- •Body: { "email": "test@example.com", "password": "123456" }
- 3.Create a Task
- •POST /api/tasks
- •Headers: Authorization: Bearer {your_token}
- •Body: { "title": "Task 1", "description": "Test task", "priority": "high" }
- 4.Get All Tasks
- •GET /api/tasks
- •Headers: Authorization: Bearer {your_token}
- 5.Filter and Sort Tasks
- •GET /api/tasks?status=pending&priority=high&sort=dueDate
- •Headers: Authorization: Bearer {your_token}
Practical Exercises
- 1.Add the ability to assign tasks to other users
- 2.Implement task categories or tags
- 3.Add the ability to search tasks by title or description
- 4.Create an endpoint to get task statistics (counts by status/priority)
- 5.Add pagination and filtering to the tasks endpoint
- 6.Implement task comments functionality
Recommended Image
A screenshot of Postman showing a successful API call to one of the task
endpoints with the proper authentication headers and response.
Further Reading
- •[Building a RESTful API with Express.js](https://www.freecodecamp.org/news/build-a-restful-api-using-node-express-and-mongodb/)
- •[Comprehensive Guide to Express.js Validation](https://express-validator.github.io/docs/)
- •[Securing Node.js RESTful APIs with JWT](https://medium.com/swlh/securing-node-js-restful-apis-with-json-web-tokens-bcbf025efeb8)
- •[Practical MongoDB Query Examples](https://www.mongodb.com/docs/manual/reference/method/db.collection.find/)