You are given an existing Express Todo API that is almost complete, but several bugs are causing incorrect behavior.
Your task is to debug the existing implementation so that all Todo endpoints behave correctly and follow proper REST API conventions.
The API stores todos in memory. Each todo has the following structure:
{
id: 1,
title: "Learn Express",
completed: false
}
The API must support:
POST /todos — Create a new todo.
GET /todos — Return all todos.
GET /todos/:id — Return a specific todo.
PATCH /todos/:id — Partially update an existing todo.
DELETE /todos/:id — Delete an existing todo.
Expected behavior
POST /todos
title is required.
- An empty or whitespace-only title is invalid.
completed is optional and should default to false.
- A successfully created todo should return 201 Created.
- Every newly created todo must receive a unique ID.
GET /todos
- Should return 200 OK.
- The response body should be an array containing all todos.
GET /todos/:id
- Should return 200 OK when the todo exists.
- Should return 404 Not Found when it does not exist.
PATCH /todos/:id
- Should update only the fields provided in the request body.
- Existing values must not be overwritten when a field is omitted.
- If
title is provided, it must not be empty or whitespace-only.
- Should return
404 Not Found when the todo does not exist.
DELETE /todos/:id
- Should remove the todo.
- Should return 204 No Content after successful deletion.
- Should return 404 Not Found when the todo does not exist.
Several issues are hidden throughout the existing code. Fix the implementation without modifying the tests.