# REST API Design Made Simple with Express.js

Building APIs is a core skill for any backend or full-stack developer. If you're working with Node.js, understanding how to design clean and scalable REST APIs using Express.js is essential.

This guide breaks it down in a simple, practical way.

* * *

## 🚀 What is a REST API?

A **REST API (Representational State Transfer)** is a way for different systems (usually a client and a server) to communicate over HTTP.

Think of it like this:

*   The **client** (browser, mobile app) sends a request
    
*   The **server** processes it
    
*   The **server** sends back a response
    

👉 Example:

*   You open Instagram → app requests data → server sends posts
    

So essentially:

> APIs are the **communication bridge** between client and server.

* * *

## 📦 What are Resources in REST?

In REST architecture, everything is treated as a **resource**.

A resource is simply:

> Any data you want to expose via your API

Examples:

*   Users → `/users`
    
*   Products → `/products`
    
*   Orders → `/orders`
    

Each resource is accessed via a **URL endpoint**.

* * *

## 🔧 HTTP Methods (Core of REST)

REST APIs use HTTP methods to define actions on resources.

### 1\. GET → Fetch Data

Used to retrieve data from the server.

```http
GET /users
```

👉 Fetch all users

* * *

### 2\. POST → Create Data

Used to create a new resource.

```http
POST /users
```

👉 Create a new user

* * *

### 3\. PUT → Update Data

Used to update an existing resource (usually full update).

```http
PUT /users/1
```

👉 Update user with ID = 1

* * *

### 4\. DELETE → Remove Data

Used to delete a resource.

```http
DELETE /users/1
```

👉 Delete user with ID = 1

* * *

## 📊 Status Codes Basics

Status codes tell the client what happened with the request.

### Common ones you should know:

*   **200 OK** → Request successful
    
*   **201 Created** → Resource created successfully
    
*   **400 Bad Request** → Client error
    
*   **404 Not Found** → Resource doesn’t exist
    
*   **500 Internal Server Error** → Server failed
    

👉 Example:

```js
res.status(200).json(users);
```

* * *

## 🛣️ Designing RESTful Routes

A good REST API follows **clean and predictable naming conventions**.

### Rules:

*   Use **nouns**, not verbs
    
*   Keep URLs **plural**
    
*   Use **IDs** for specific resources
    

* * *

## 👨‍💻 Example: Users Resource

Let’s design a simple `users` API using Express.js.

### Setup

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

app.use(express.json());
```

* * *

### Routes

#### Get all users

```js
app.get("/users", (req, res) => {
  res.status(200).json({ message: "Get all users" });
});
```

#### Get single user

```js
app.get("/users/:id", (req, res) => {
  res.status(200).json({ message: `Get user ${req.params.id}` });
});
```

#### Create user

```js
app.post("/users", (req, res) => {
  res.status(201).json({ message: "User created" });
});
```

#### Update user

```js
app.put("/users/:id", (req, res) => {
  res.status(200).json({ message: `User ${req.params.id} updated` });
});
```

#### Delete user

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

* * *

## 🧠 Key Design Principles (Keep This in Mind)

*   Keep routes **consistent and predictable**
    
*   Use correct **HTTP methods**
    
*   Return meaningful **status codes**
    
*   Separate logic (routes vs controllers in real apps)
    
*   Think in terms of **resources, not actions**
    

* * *

## 🔚 Final Thoughts

REST API design is not about complexity — it's about **clarity and consistency**.

If your API:

*   Uses proper HTTP methods
    
*   Has clean route naming
    
*   Returns correct status codes
    

👉 You're already ahead of most beginners.

Once you're comfortable with this, the next step is:

*   Adding authentication (JWT)
    
*   Connecting databases (MongoDB)
    
*   Structuring scalable backend systems
