R
Rishtaara
Node.js Fundamentals
Lesson 6 of 8Article17 min

Designing a REST API

Designing a REST API

REST resource design

  • Use nouns for resources: /users, /courses, /enrollments.
  • GET for reads, POST for creation, PUT/PATCH for updates, DELETE for removal.
  • Return proper status codes and structured JSON responses.
  • Add pagination and filtering for list endpoints.

CRUD routes example

In-memory course API sketch
const courses = [{ id: 1, title: "Node Basics" }];

app.get("/api/courses", (req, res) => {
  res.json({ data: courses });
});

app.post("/api/courses", (req, res) => {
  const newCourse = { id: Date.now(), title: req.body.title };
  courses.push(newCourse);
  res.status(201).json({ data: newCourse });
});

app.patch("/api/courses/:id", (req, res) => {
  const id = Number(req.params.id);
  const course = courses.find((c) => c.id === id);
  if (!course) return res.status(404).json({ error: "Course not found" });
  course.title = req.body.title ?? course.title;
  res.json({ data: course });
});