REST APIs with Express

A REST API exposes resources at URLs and uses HTTP methods — GET, POST, PUT, DELETE — to read and change them, returning JSON with meaningful status codes.

Learn REST APIs with Express in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build a full CRUD API in Express: listing and fetching items, creating with POST, updating with PUT, deleting with DELETE, and returning the right status codes throughout.

What You'll Learn in This Lesson

1️⃣ Reading Data with GET

The simplest REST endpoints read data. GET /books returns the whole collection; GET /books/:id returns one item, where :id is a URL parameter available as req.params.id . If the item doesn't exist, return 404 .

Note the Number(req.params.id) conversion — URL parameters are always strings, so comparing a string id to a numeric id silently fails without it.

2️⃣ Creating Data with POST

POST creates a new resource. The client sends data in the request body as JSON; express.json() parses it onto req.body . Validate the input, create the item, then respond with 201 Created and the new resource.

3️⃣ Updating and Deleting

PUT /books/:id updates an existing item and returns it with 200 . DELETE /books/:id removes one and returns 204 No Content — success with an empty body. Both return 404 when the id isn't found.

Your turn. Fill in the two ___ blanks to finish a tiny todos API, then run it.

Build all four operations for a notes resource: list, fetch one, update, and delete — with correct status codes and 404s. Run it with node notes.js .

📋 Quick Reference — REST with Express

Practice quiz

In REST, which HTTP method is used to read a resource?

  • GET
  • POST
  • DELETE
  • PUT

Answer: GET. GET reads data; POST creates, PUT updates, and DELETE removes.

Which method creates a new resource?

  • GET
  • POST
  • PUT
  • DELETE

Answer: POST. POST creates a new resource from the data in the request body.

Why must you wrap req.params.id with Number() before comparing to a numeric id?

  • To round it
  • To make it negative
  • Because params are strings and '3' === 3 is false
  • To encrypt it

Answer: Because params are strings and '3' === 3 is false. URL params arrive as strings, so a strict comparison to a numeric id fails without conversion.

What status code should a successful POST that creates a resource return?

  • 200 OK
  • 404 Not Found
  • 204 No Content
  • 201 Created

Answer: 201 Created. 201 Created is the correct status after successfully creating a new resource.

Which middleware lets you read a JSON request body on req.body?

  • express.json()
  • express.static()
  • express.raw()
  • express.urlencoded()

Answer: express.json(). app.use(express.json()) parses JSON bodies and populates req.body.

What status code is appropriate for a successful DELETE with no body to return?

  • 201 Created
  • 204 No Content
  • 200 OK
  • 400 Bad Request

Answer: 204 No Content. 204 No Content signals success with an empty response body.

Which status code means the client sent invalid data, like a missing required field?

  • 404 Not Found
  • 500 Server Error
  • 400 Bad Request
  • 201 Created

Answer: 400 Bad Request. 400 Bad Request indicates the client's input was invalid.

What does req.params.id refer to in the route GET /books/:id?

  • The query string
  • The request body
  • A response header
  • The URL parameter value for :id

Answer: The URL parameter value for :id. req.params.id holds the value matched by the :id segment of the URL.

Why does 'Cannot set headers after they are sent' often occur in a route?

  • You forgot to return after an early res.json
  • The port is wrong
  • express.json() is missing
  • The database is down

Answer: You forgot to return after an early res.json. Without returning after an early response, code keeps running and responds twice.

Which status code means the requested resource does not exist?

  • 200
  • 204
  • 404
  • 201

Answer: 404. 404 Not Found is returned when the requested resource cannot be found.