Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js πŸš€

Building Clean and Scalable Backend APIs

Updated
β€’5 min read
REST API Design Made Simple with Express.js πŸš€

πŸ“Œ Introduction

Modern web applications are all about communication.

πŸ‘‰ Your frontend (React, mobile app, etc.) talks to your backend πŸ‘‰ Backend sends data back

This communication happens through something called:

REST APIs


🧠 What is a REST API?

REST stands for:

πŸ‘‰ Representational State Transfer

But let’s keep it simple:

A REST API is a way for client and server to communicate using HTTP.


🧩 Simple Meaning:

  • Client requests data πŸ“±

  • Server responds with data πŸ–₯️


🌐 APIs in Simple Terms

πŸ‘‰ API = messenger between client and server

Example:

  • You order food πŸ”

  • Waiter takes order

  • Kitchen prepares food

  • Waiter brings food

πŸ‘‰ Waiter = API


πŸ“¦ What are Resources in REST?

In REST, everything is treated as a resource.


🧠 Example Resource:

πŸ‘‰ users


πŸ“Œ Resource examples:

  • users

  • products

  • orders

  • posts


πŸ”„ REST Architecture Concept

Client β†’ HTTP Request β†’ Server β†’ Response

βš™οΈ HTTP Methods in REST API

Each operation uses a specific HTTP method.


πŸ“₯ 1. GET (Read Data)

πŸ‘‰ Used to fetch data

app.get("/users", (req, res) => {
  res.send("Get all users");
});

🧠 Example:

  • Get all users

  • Get user by ID


πŸ“€ 2. POST (Create Data)

πŸ‘‰ Used to create new resource

app.post("/users", (req, res) => {
  res.send("Create user");
});

🧠 Example:

  • Create new user

  • Add product


πŸ”„ 3. PUT (Update Data)

πŸ‘‰ Used to update existing resource

app.put("/users/:id", (req, res) => {
  res.send("Update user");
});

🧠 Example:

  • Update user profile

  • Modify product details


❌ 4. DELETE (Remove Data)

πŸ‘‰ Used to delete resource

app.delete("/users/:id", (req, res) => {
  res.send("Delete user");
});

🧠 Example:

  • Delete user account

  • Remove product


πŸ“Š CRUD vs HTTP Methods

CRUD Operation HTTP Method Route Example
Create POST /users
Read GET /users
Update PUT /users/:id
Delete DELETE /users/:id

πŸ”’ HTTP Status Codes (Basics)

Status codes tell what happened with a request.


🧠 Common Codes:

Code Meaning
200 Success
201 Created
400 Bad Request
401 Unauthorized
404 Not Found
500 Server Error

🧩 Example:

res.status(201).send("User created");

🧭 Designing REST Routes (Best Practice)

Let’s take a users resource.


πŸ“Œ Clean REST Routes:

GET     /users        β†’ get all users
GET     /users/:id    β†’ get single user
POST    /users        β†’ create user
PUT     /users/:id    β†’ update user
DELETE  /users/:id    β†’ delete user

🧠 Why This Structure?

πŸ‘‰ Predictable πŸ‘‰ Scalable πŸ‘‰ Easy to maintain


πŸ”„ REST Request-Response Flow

Client Request
      ↓
Route Handler (Express)
      ↓
Business Logic
      ↓
Database Operation
      ↓
Response Sent

πŸš€ Example Full Express REST API

const express = require("express");
const app = express();

app.use(express.json());

// GET all users
app.get("/users", (req, res) => {
  res.send("Get all users");
});

// GET single user
app.get("/users/:id", (req, res) => {
  res.send(`Get user ${req.params.id}`);
});

// POST create user
app.post("/users", (req, res) => {
  res.status(201).send("User created");
});

// PUT update user
app.put("/users/:id", (req, res) => {
  res.send("User updated");
});

// DELETE user
app.delete("/users/:id", (req, res) => {
  res.send("User deleted");
});

app.listen(3000);

🌍 Real-World Example

πŸ›’ E-commerce API:

GET /products
POST /orders
PUT /orders/:id
DELETE /cart/:id

⚠️ Important REST Principles

  • Use nouns, not verbs in routes

  • Keep routes consistent

  • Use correct HTTP methods

  • Return proper status codes


🎯 Key Takeaways

  • REST APIs enable client-server communication

  • Resources represent data (users, products, etc.)

  • HTTP methods define actions

  • Status codes explain results

  • Clean route design improves scalability


✍️ Practice Challenge

Try building:

πŸ‘‰ A REST API for:

  • users

  • posts

With full CRUD operations.


πŸ”₯ Conclusion

REST API design is the foundation of backend development.

πŸ‘‰ Simple structure πŸ‘‰ Predictable routes πŸ‘‰ Scalable architecture

Once you understand REST principles, building APIs becomes clean, structured, and professional πŸ’ͺ


Happy Coding πŸš€βœ¨

How the Web Works πŸ•ΈοΈπŸ•ΈοΈ

Part 4 of 50

A practical web development series explaining how the web worksβ€”from DNS and browsers to servers, HTTP, APIs, and deploymentβ€”while clearly connecting these fundamentals to real-world website programming using frontend and backend examples.

Up next

Map and Set in JavaScript πŸ—‚οΈβš‘

Understanding Better Data Structures in Modern JavaScript