Backend Exercise
It’s time to build! In this exercise, you will complete the mocked API for the Task Manager and create a comprehensive test suite in Postman.
Objective
Section titled “Objective”- Implement the missing
PATCH(Update) andDELETEendpoints in your Hono application. - Add a custom middleware that logs the request method and URL.
- Create a Postman Collection that tests every endpoint.
Part 1: API Implementation
Section titled “Part 1: API Implementation”Open your src/index.ts (or wherever your Hono app is).
Task 1: Middleware
Section titled “Task 1: Middleware”Create a middleware that logs: [METHOD] URL - Timestamp.
Task 2: Update Task (PATCH)
Section titled “Task 2: Update Task (PATCH)”Implement PATCH /api/tasks/:id.
- It should accept a JSON body with
title,description, orstatus. - Update the task in your mock array.
- Return the updated task.
- Return 404 if the task doesn’t exist.
Task 3: Delete Task (DELETE)
Section titled “Task 3: Delete Task (DELETE)”Implement DELETE /api/tasks/:id.
- Remove the task from the array.
- Return a success message (e.g.,
{ message: "Deleted" }). - Return 404 if the task doesn’t exist.
Part 2: Postman Collection
Section titled “Part 2: Postman Collection”Create a collection named “Task Manager Exercise” with the following requests:
- Get All Tasks (
GET {{baseUrl}}/api/tasks) - Create Task (
POST {{baseUrl}}/api/tasks)- Body:
{"title": "Test Task", "description": "Testing Postman"}
- Body:
- Get Single Task (
GET {{baseUrl}}/api/tasks/:id)- Use a real ID from the previous response.
- Update Task (
PATCH {{baseUrl}}/api/tasks/:id)- Body:
{"status": "in_progress"}
- Body:
- Delete Task (
DELETE {{baseUrl}}/api/tasks/:id)
💡 Click for Code Hints
Middleware:
app.use("*", async (c, next) => { console.log(`[${c.req.method}] ${c.req.url} - ${new Date()}`); await next();});Delete Endpoint:
app.delete("/api/tasks/:id", (c) => { const id = c.req.param("id"); const index = tasks.findIndex((t) => t.id === id);
if (index === -1) return c.json({ error: "Not found" }, 404);
tasks.splice(index, 1); return c.json({ message: "Deleted" });});Verification
Section titled “Verification”- Start your server (
bun run dev). - Open Postman.
- Run your “Create Task” request.
- Copy the ID of the new task.
- Use that ID to run “Update Task” and verify the status changes.
- Run “Delete Task” with that ID.
- Run “Get Single Task” with that ID - it should now fail (404).
Next Steps
Section titled “Next Steps”Congratulations! You have built a functional (mocked) REST API and verified it with industry-standard tools. In the next module, we will connect this to a real database using Drizzle ORM.