Node.js REST API Project

Build a small CRUD API with Express.

✍️ 2 exercises in this lesson

A REST API exposes resources through URLs and HTTP methods:

  • GET /api/books: list
  • GET /api/books/:id: read one
  • POST /api/books: create
  • PUT /api/books/:id: update
  • DELETE /api/books/:id: delete

This example keeps data in memory. In a real app, replace the array with a database (MySQL, PostgreSQL or MongoDB) through a library such as Prisma or Sequelize.

Key Takeaways: Node.js REST API Project

Mastering Node.js REST API Project helps you write clean and reliable Node.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Node.js REST API Project Code Example

Node.js
import express from "express";
const app = express();
app.use(express.json());

let books = [{ id: 1, title: "Clean Code", author: "Robert C. Martin" }];
let nextId = 2;

app.get("/api/books", (req, res) => res.json(books));

app.get("/api/books/:id", (req, res) => {
  const book = books.find(b => b.id === Number(req.params.id));
  book ? res.json(book) : res.status(404).json({ error: "Not found" });
});

app.post("/api/books", (req, res) => {
  const { title, author } = req.body;
  if (!title) return res.status(422).json({ error: "Title is required" });
  const book = { id: nextId++, title, author };
  books.push(book);
  res.status(201).json(book);
});

app.delete("/api/books/:id", (req, res) => {
  books = books.filter(b => b.id !== Number(req.params.id));
  res.status(204).end();
});

app.listen(3000, () => console.log("Books API on :3000"));
Output
Books API on :3000
GET /api/books → [{"id":1,"title":"Clean Code",...}]
POST /api/books → 201 {"id":2,...}
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Node.js REST API Project Practice Exercises

Check what you learned in “Node.js REST API Project”.

/ 2 solved
Fill in the blank

Respond with "Created" status and JSON.

res.(201).json(book);
✓ Correct! Not quite. Try again.
Multiple choice

Which HTTP method is used to delete a resource?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services