Create an Item With POST
Easy · Route Handlers
Export a POST function from route.ts to handle POST requests. Read the JSON body with await request.json(), and set a status code with the second argument: Response.json(data, { status: 201 }).
Implement POST in app/api/todos/route.ts:
- If the body has no title (missing or empty string), return status 400 with { error: "Title is required" } and do not store anything.
- Otherwise create { id, title, done: false } where id is todos.length + 1, push it to todos, and return it with status 201 (Created).
The driver createTodo(existing, payload) is already written: it fills todos with existing, sends payload as the JSON body and returns { status, body, count } (count = number of stored todos afterwards).
Next: Detect "use client"