Loading lessons...
Project 1: RESTful Todo Task Manager API
Build a Complete Task Manager REST API
In this interactive project, you will build a complete RESTful API for managing tasks with Express.
Project Architecture & Implementation
const express = require("express");
const app = express();
app.use(express.json());
let todos = [
{ id: 1, title: "Learn Event Loop", completed: true },
{ id: 2, title: "Master Express Middleware", completed: false }
];
// GET /api/todos - List all tasks with optional ?completed=true query filter
app.get("/api/todos", (req, res) => {
const { completed } = req.query;
let result = todos;
if (completed !== undefined) {
const isCompleted = completed === "true";
result = todos.filter(t => t.completed === isCompleted);
}
res.status(200).json({ status: "success", count: result.length, data: result });
});
// POST /api/todos - Create a task
app.post("/api/todos", (req, res) => {
const { title } = req.body;
if (!title) {
return res.status(400).json({ status: "fail", message: "Title is required" });
}
const newTodo = { id: Date.now(), title, completed: false };
todos.push(newTodo);
res.status(201).json({ status: "success", data: newTodo });
});
// PATCH /api/todos/:id - Toggle task completion status
app.patch("/api/todos/:id", (req, res) => {
const todo = todos.find(t => t.id === Number(req.params.id));
if (!todo) {
return res.status(404).json({ status: "fail", message: "Task not found" });
}
if (req.body.completed !== undefined) {
todo.completed = Boolean(req.body.completed);
}
if (req.body.title) {
todo.title = req.body.title;
}
res.status(200).json({ status: "success", data: todo });
});
// DELETE /api/todos/:id - Remove task
app.delete("/api/todos/:id", (req, res) => {
const index = todos.findIndex(t => t.id === Number(req.params.id));
if (index === -1) {
return res.status(404).json({ status: "fail", message: "Task not found" });
}
todos.splice(index, 1);
res.status(200).json({ status: "success", message: "Task deleted successfully" });
});
TL;DR
- Implement full RESTful endpoints with
GET,POST,PATCH, andDELETE. - Filter collections with query string parameters.