Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Updated
β€’3 min read
REST API Design Made Simple with Express.js
S

Like to study,fitness freak,my looks is my first priority, hardworking person, like discipline and love to learn new thing

πŸš€ Introduction

In modern web development, APIs are everywhere.

πŸ‘‰ They allow different systems (frontend, backend, mobile apps) to communicate with each other

One of the most popular styles of building APIs is:

πŸ‘‰ REST (Representational State Transfer)

Let’s understand REST in the simplest way possible πŸ”₯


🧠 What Is a REST API?

A REST API is:

πŸ‘‰ A way for a client (browser/app) to communicate with a server using HTTP


πŸ’‘ Simple Idea

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

πŸ‘‰ Client asks β†’ Server responds


πŸ“¦ What Are Resources in REST?

In REST, everything is treated as a resource

Examples:

  • Users πŸ‘€

  • Products πŸ“¦

  • Orders 🧾

πŸ‘‰ Each resource has its own URL


Example:

/users
/products

βš™οΈ HTTP Methods (CRUD Operations)

REST uses HTTP methods to perform actions:

Operation Method Example
Read GET /users
Create POST /users
Update PUT /users/1
Delete DELETE /users/1

πŸ‘‰ This is called CRUD mapping


πŸ“Š Diagram Idea: CRUD Mapping

GET    β†’ Read  
POST   β†’ Create  
PUT    β†’ Update  
DELETE β†’ Delete  

βš™οΈ Designing Routes (Users Example)

Let’s build a simple REST API for users


1️⃣ GET All Users

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

2️⃣ GET Single User

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

3️⃣ Create User

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

4️⃣ Update User

app.put("/users/:id", (req, res) => {
  res.send(`User ${req.params.id} updated`);
});

5️⃣ Delete User

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

πŸ”’ Status Codes (Basics)

Status codes tell the client what happened:

Code Meaning
200 OK
201 Created
400 Bad Request
404 Not Found
500 Server Error

Example:

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

πŸ“Š Diagram Idea: Request Lifecycle

Client β†’ Request β†’ Route β†’ Logic β†’ Response

🧠 REST Design Principles

πŸ‘‰ Keep URLs clean and meaningful:

❌ Bad:

/getUsers
/createUser

βœ… Good:

/users
/users/1

πŸ‘‰ Use HTTP methods instead of action names


🧩 Real-World Example

/users           β†’ GET all users  
/users/101       β†’ GET user  
/users           β†’ POST new user  
/users/101       β†’ PUT update  
/users/101       β†’ DELETE  

🧠 Conceptual Understanding

πŸ‘‰ REST = Structured way to design APIs πŸ‘‰ Resources + Methods = Clean API design


🏁 Conclusion

REST APIs make your backend:

  • Organized

  • Scalable

  • Easy to understand


✨ Final Tip

If your API URLs look like actions β†’ rethink them as resources 😎