Node.js · Lesson 6 of 7

Build a REST API (CRUD)

Create, read, update and delete with Express routers, validation and correct status codes.

A REST API exposes resources (jobs, users) at URLs and uses HTTP methods for actions:

  • GET /jobs — list · GET /jobs/:id — one item
  • POST /jobs — create → 201 Created
  • PUT /jobs/:id — replace · PATCH — partly update
  • DELETE /jobs/:id — remove → 204 No Content

Use express.Router() to keep each resource in its own file, and validate input before saving. Return 400 for bad input and 404 when an item doesn't exist.

Example code

Run with Node.js 20+
// routes/jobs.js
import { Router } from "express";

const router = Router();
let jobs = [];
let nextId = 1;

router.post("/", (req, res) => {
  const { title, city } = req.body;
  if (!title || !city) {
    return res.status(400).json({ error: "title and city are required" });
  }
  const job = { id: nextId++, title, city };
  jobs.push(job);
  res.status(201).json(job);
});

router.get("/", (req, res) => res.json(jobs));

router.delete("/:id", (req, res) => {
  const before = jobs.length;
  jobs = jobs.filter((j) => j.id !== Number(req.params.id));
  if (jobs.length === before) return res.status(404).json({ error: "Not found" });
  res.status(204).end();
});

export default router;

// app.js:  app.use("/api/jobs", jobsRouter);

Output

$ curl -X POST localhost:3000/api/jobs -H "Content-Type: application/json" -d '{"title":"Backend Developer","city":"Kochi"}'
{"id":1,"title":"Backend Developer","city":"Kochi"}      (201 Created)

$ curl -X POST localhost:3000/api/jobs -H "Content-Type: application/json" -d '{"title":"Tester"}'
{"error":"title and city are required"}                  (400 Bad Request)

$ curl -X DELETE localhost:3000/api/jobs/1 -i
HTTP/1.1 204 No Content

Save the code in a file such as app.js and run node app.js. For Express lessons, run npm install express first. Names and data are fictional.

Key points

  • Map actions to HTTP methods: GET, POST, PUT/PATCH, DELETE.
  • Return 201 after create, 204 after delete, 400 for bad input, 404 if missing.
  • Use a Router per resource.
  • Validate request bodies before saving.

Quick check

Which status code fits a successful POST that created a record?

Practice task

Add PATCH /api/jobs/:id that updates only the fields sent in the body and returns the updated job (404 if the id doesn't exist).

Interview corner

What is the difference between PUT and PATCH?

PUT replaces the whole resource with the data sent. PATCH changes only the fields sent. PUT is idempotent — sending the same request twice gives the same result.

How do you validate input in an Express API?

Check required fields and types before using them — by hand for small APIs, or with a schema library such as Zod or Joi — and return 400 with a clear message when validation fails.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.