Lesson 20 of 30

REST API

Chapter 20

REST API

Learn the fundamentals of building REST APIs with Node.js and Express.

Theory

A REST API allows applications to communicate using HTTP. REST commonly uses resources and HTTP methods such as: GET → Read POST → Create PUT/PATCH → Update DELETE → Remove A REST API usually exchanges data using JSON. Good REST APIs use clear resource-based URLs and appropriate HTTP status codes.

Example

Node.js Example
const express = require("express");

const app = express();

app.use(express.json());

app.get("/users", (req, res) => {
  res.json([
    { id: 1, name: "Alex" },
    { id: 2, name: "Sam" }
  ]);
});

app.listen(3000);

Output

The /users endpoint returns user data as JSON.

Practice

Create a REST API for products with GET, POST, PUT, and DELETE routes.

Interview Questions

1.

What is a REST API?

2.

What HTTP methods are commonly used in REST?

3.

Why is JSON commonly used in APIs?

Quick Quiz

Which option best matches this chapter?

A.An API using HTTP-based resource operations
B.A CSS framework
C.A database engine
D.A frontend-only library

Answer: An API using HTTP-based resource operations