Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Updated
4 min read
REST API Design Made Simple with Express.js

In modern web development, applications rarely work in isolation. They constantly communicate with servers to fetch or send data. This is where REST APIs come in. In this blog, we’ll break down REST API design using Express.js in a simple and structured way.

📌 What is a REST API?

A REST API (Representational State Transfer) is a way for systems to communicate over the internet using standard HTTP methods.

Think of it like this:

🧑‍💻 Client → Your frontend (browser, app)

🖥️ Server → Backend (Express.js)

🔄 API → The messenger between them

👉 Example:

When you open Instagram, your app requests user data from a server using an API.


🔗 APIs = Communication Between Client & Server

A REST API acts like a waiter in a restaurant:

  • Client = Customer

  • 🍽️ Server = Kitchen 👨‍🍳

  • API = Waiter 🧾

The waiter takes your request, gives it to the kitchen, and brings back the response.


📦 Resources in REST Architecture

In REST, everything is treated as a resource.

Examples:

  • Users → /users

  • Products → /products

  • Orders → /orders

Each resource is identified using a URL.

👉 Example:

/users/101

This represents a specific user.


⚙️ HTTP Methods (CRUD Operations)

REST APIs use HTTP methods to perform actions on resources.

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

CRUD vs HTTP Methods

CREATE  → POST
READ    → GET
UPDATE  → PUT
DELETE  → DELETE

This mapping is the core of REST design.

Code Meaning
200 Success ✅
201 Created 🎉
400 Bad Request ❌
404 Not Found 🔍
500 Server Error 💥

Example:

res.status(201).json({ message: "User created" });

🛣️ Designing Clean REST Routes

Good API design = clean and predictable routes.

✅ Best Practices:

  • Use nouns, not verbs

  • Keep naming consistent

  • Use plural resources

✔️ Good:

GET /users
POST /users
GET /users/1

❌ Bad:

GET /getUsers
POST /createUser

🔁 REST Request–Response Lifecycle

Client → Request → Server → Process → Response → Client

Steps:

  1. Client sends request

  2. Server processes it

  3. Server sends response

  4. Client receives data

👨‍💻 Example: Users Resource in Express.js

Here’s a simple example using Express.js:

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

app.use(express.json());

// GET all users
app.get("/users", (req, res) => {
  res.status(200).json([{ id: 1, name: "Raj" }]);
});

// POST create user
app.post("/users", (req, res) => {
  const user = req.body;
  res.status(201).json({ message: "User created", user });
});

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

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

app.listen(3000, () => console.log("Server running"));

🧠 Key Takeaways

  • REST APIs enable communication between client and server

  • Everything is treated as a resource

  • Use proper HTTP methods (GET, POST, PUT, DELETE)

  • Keep routes clean and meaningful

  • Always return appropriate status codes


📌 Conclusion

Designing a REST API is not just about writing code—it’s about creating a system that is simple, predictable, and scalable.

By following REST principles and using Express.js, you can build APIs that are easy to use and maintain.