CodeBlanks Logo Icon CodeBlanks

CRUD routes

Write CRUD (Create, Read, Update, Delete) routes.

CRUD routes

The third lesson adds CRUD routes: Create, Read, Update, Delete. For now we do Read and Create. You use app.get("/books", ...) for Read and app.post("/books", ...) for Create.

The GET route returns all books with res.json(books). The POST route takes the new book from req.body, adds it with books.push() and returns it with res.status(201).json(req.body). 201 means “Created”. app.use(express.json()) automatically parses incoming JSON bodies; we added it to the starting code. Now the API can read and write data.

Adding data with POST

The POST route is used to add new data. It is defined with app.post("/books", (req, res) => { ... }). The data sent in the body lives in req.body.

First app.use(express.json()) must be called; otherwise req.body stays undefined. This middleware parses the JSON body automatically. Then you add the new book to the array with books.push(req.body). You give the “Created” status code with res.status(201); this is the REST standard for create operations. Finally you return the added book with .json(req.body). This lets the client confirm the data was added.

Practice Define GET and POST /books routes

Define two routes. With app.get("/books", ...) return the books array as JSON with res.json. With app.post("/books", ...) add a book from req.body and respond with res.status(201).json(...). The self-request is ready.

Starter code

index.js
const books = [];
books.push({ id: 1, title: "Node.js" });

const express = require("express");
const app = express();
app.use(express.json());

// GET /books route — return the books array as JSON
// write here

// POST /books route — add a book from req.body, respond with 201
// write here

// Self-request (boilerplate)
const server = app.listen(3000, () => {
  const http = require("http");
  http.get("http://localhost:3000/books", (res) => {
    let body = "";
    res.on("data", (c) => body += c);
    res.on("end", () => { console.log(body); server.close(); });
  });
});

Sample solution

index.js
app.get("/books", (req, res) => {
  res.json(books);
});
app.post("/books", (req, res) => {
  books.push(req.body);
  res.status(201).json(req.body);
});

Expected output

[{"id":1,"title":"Node.js"}]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
app.get Reads data
app.post Writes data
req.body Incoming body
res.status(201) Created code

2 Where is the new book read from in the POST /books route?

index.js
app.post("/books", (req, res) => ...)
  • req.params
  • req.query
  • req.body Correct answer
  • req.url

Explanation: Body data is parsed into req.body by express.json().

3 Complete the POST route

Question
app.____("/books", (req, res) => { books.push(req.body); res.status(201).json(req.body); });
Answer
app.post("/books", (req, res) => { books.push(req.body); res.status(201).json(req.body); });

Explanation: app.post defines a route for adding new data.

All lessons in Intermediate