Task Management REST API
Task Management REST API — purpose and prerequisites
A task management API exposes listing, creation, completion, and deletion. Preserve separation between the handler, task service, and storage layer. The primary demonstration exercises HTTP-like operations in memory, while a companion shows how to attach them to Node's HTTP server.
What you’ll learn
- Explain the purpose of task management rest api.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
IDs increase independently of array length. The service copies objects at its public boundary and reports undefined when an ID is missing. The HTTP adapter should map these results to 201, 200, or 404 statuses.
A production API cannot rely on in-memory data if tasks must survive restarts. Concurrent requests and permissions also need real enforcement.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
type Task = { id: number; title: string; done: boolean };
class TaskApi {
private tasks: Task[] = [];
private nextId = 1;
create(title: string): Task {
if (!title.trim()) throw new Error("Title required");
const task = { id: this.nextId++, title: title.trim(), done: false };
this.tasks.push(task);
return { ...task };
}
complete(id: number): Task | undefined {
const task = this.tasks.find(x => x.id === id);
if (!task) return undefined;
task.done = true;
return { ...task };
}
list(): Task[] { return this.tasks.map(task => ({ ...task })); }
}
const api = new TaskApi();
const created = api.create("Plan tasks");
console.log(api.complete(created.id));
console.log(api.list());Verified output
{ id: 1, title: 'Plan tasks', done: true }
[ { id: 1, title: 'Plan tasks', done: true } ]Compile: npx tsc --strict --target es2022 --module commonjs task-management-rest-api.ts; run: node task-management-rest-api.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
IDs increase independently of array length. The service copies objects at its public boundary and reports undefined when an ID is missing. The HTTP adapter should map these results to 201, 200, or 404 statuses.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Build the practical integration
Integration companion: The primary example above is self-contained. The files below demonstrate a real framework, runtime, or project setup and may need additional packages or a browser.
server.ts
import { createServer } from "node:http";
type Task = { id: number; title: string; done: boolean };
const tasks: Task[] = [];
let nextId = 1;
const server = createServer(async (req, res) => {
res.setHeader("content-type", "application/json");
if (req.method === "GET" && req.url === "/tasks") {
res.writeHead(200); res.end(JSON.stringify(tasks)); return;
}
if (req.method === "POST" && req.url === "/tasks") {
let body = "";
for await (const chunk of req) {
body += String(chunk);
if (body.length > 4096) { res.writeHead(413); res.end(JSON.stringify({ error: "Too large" })); return; }
}
try {
const value: unknown = JSON.parse(body);
if (typeof value !== "object" || value === null || !("title" in value) || typeof value.title !== "string" || !value.title.trim()) throw new Error("Invalid title");
const task: Task = { id: nextId++, title: value.title.trim(), done: false };
tasks.push(task);
res.writeHead(201); res.end(JSON.stringify(task));
} catch { res.writeHead(400); res.end(JSON.stringify({ error: "Invalid JSON or title" })); }
return;
}
res.writeHead(404); res.end(JSON.stringify({ error: "Not found" }));
});
server.listen(3000, "127.0.0.1");Setup and execution
npm i -D typescript @types/node
npx tsc --strict --target es2022 --module commonjs server.ts
node server.js
curl http://127.0.0.1:3000/tasks
curl -X POST -H "Content-Type: application/json" -d '{"title":"Study"}' http://127.0.0.1:3000/tasksThis is a real executable HTTP subset of the task service with GET and POST. For a complete CRUD service, add PATCH and DELETE, persistent storage, user authentication, and tests. Request body limits here are simplified for teaching.
Validation: This companion’s TypeScript source was also compiled successfully. Network-facing or browser interaction was not runtime-tested.
Common mistake and correction
A production API cannot rely on in-memory data if tasks must survive restarts. Concurrent requests and permissions also need real enforcement.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Build GET /tasks, POST /tasks, PATCH /tasks/:id, and DELETE /tasks/:id on a local server with validation.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
IDs increase independently of array length. The service copies objects at its public boundary and reports undefined when an ID is missing. The HTTP adapter should map these results to 201, 200, or 404 statuses.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.