# REST API Design Made Simple with Express.js

Modern web applications constantly communicate with servers.

Examples:

*   frontend fetching user profiles
    
*   mobile apps loading products
    
*   dashboards updating analytics
    
*   applications sending login requests
    

This communication usually happens through:

# APIs

And one of the most common API styles today is:

# REST

If you’ve worked with Express.js before, you’ve already seen REST patterns even if you didn’t realize it.

Let’s understand REST APIs step-by-step in a practical way.

* * *

# What Does API Mean?

API stands for:

# Application Programming Interface

In simple terms:

*   APIs allow different systems to communicate
    

Example:

```txt
Frontend ↔ Backend
Mobile App ↔ Server
Website ↔ Database Service
```

An API acts like a bridge between client and server.

* * *

# What Is a REST API?

REST stands for:

# Representational State Transfer

The name sounds complicated, but the idea is simple.

REST is a structured way of designing APIs using:

*   resources
    
*   HTTP methods
    
*   predictable routes
    

REST APIs organize backend functionality around resources.

* * *

# What Are Resources in REST?

A resource is simply:

*   something your application manages
    

Examples:

*   users
    
*   products
    
*   posts
    
*   comments
    
*   orders
    

In REST, resources are usually represented using nouns.

Example:

```txt
/users
/products
/posts
```

* * *

# Why REST Became Popular

REST makes APIs:

*   predictable
    
*   organized
    
*   scalable
    
*   easier to understand
    

Instead of random route naming, REST follows conventions.

That consistency helps frontend and backend developers work together more easily.

* * *

# REST Uses HTTP Methods

REST APIs mainly rely on standard HTTP methods.

Examples:

*   GET
    
*   POST
    
*   PUT
    
*   DELETE
    

Each method represents a specific action.

* * *

# CRUD and HTTP Methods

REST APIs usually map CRUD operations to HTTP methods.

| Operation | HTTP Method |
| --- | --- |
| Create | POST |
| Read | GET |
| Update | PUT |
| Delete | DELETE |

* * *

# Example Resource: Users

Let’s use:

```txt
/users
```

as the main resource example.

* * *

# 1\. GET Request - Fetch Data

Used for:

*   retrieving data
    
*   reading resources
    

Example:

```txt
GET /users
```

Meaning:

> “Fetch all users.”

* * *

# Fetch Single User

```txt
GET /users/42
```

Meaning:

> “Fetch user with ID 42.”

* * *

# Express Example

```js
app.get("/users", (req, res) => {
  res.send("All Users");
});
```

* * *

# 2\. POST Request - Create Data

Used for:

*   creating resources
    
*   sending new data to server
    

Example:

```txt
POST /users
```

Meaning:

> “Create a new user.”

* * *

# Express Example

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

* * *

# 3\. PUT Request - Update Data

Used for:

*   updating existing resources
    

Example:

```txt
PUT /users/42
```

Meaning:

> “Update user with ID 42.”

* * *

# Express Example

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

* * *

# 4\. DELETE Request - Remove Data

Used for:

*   deleting resources
    

Example:

```txt
DELETE /users/42
```

Meaning:

> “Delete user with ID 42.”

* * *

# Express Example

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

* * *

# Designing Clean REST Routes

One of the biggest REST principles is:

# Use Nouns, Not Verbs

Good REST route:

```txt
/users
```

Bad REST route:

```txt
/getUsers
/createUser
/deleteUser
```

Why?

Because HTTP methods already describe the action.

Example:

```txt
GET /users
POST /users
DELETE /users/42
```

The method explains the operation clearly.

* * *

# REST Request-Response Lifecycle

Example flow:

```txt
Client sends request
↓
Express route receives request
↓
Server processes logic
↓
Database operation happens
↓
Response returned as JSON
```

This is the core lifecycle of REST APIs.

* * *

# Sending JSON Responses

REST APIs commonly return:

# JSON

because it’s lightweight and easy for applications to understand.

Example:

```js
res.json({
  id: 1,
  name: "Dhiraj"
});
```

* * *

# Status Codes Basics

HTTP responses also include:

# Status Codes

These indicate request results.

* * *

# Common Status Codes

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

* * *

# Example Response

```js
res.status(201).json({
  message: "User Created"
});
```

This means:

*   request succeeded
    
*   new resource created
    

* * *

# Example Small REST API in Express

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

const app = express();

app.use(express.json());

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

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

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

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

app.listen(3000);
```

This small application already demonstrates core REST principles.

* * *

# Why REST APIs Feel Predictable

REST works well because routes follow conventions.

Example:

| Route | Meaning |
| --- | --- |
| GET /users | Fetch users |
| POST /users | Create user |
| GET /users/1 | Fetch specific user |
| PUT /users/1 | Update user |
| DELETE /users/1 | Delete user |

Once developers learn the pattern, APIs become easier to use.

* * *

# Simple Mental Model

Think of REST like organized resource management.

## Resource

> “What data are we managing?”

* * *

## HTTP Method

> “What operation are we performing?”

* * *

## Route

> “Where is that resource located?”

* * *

REST APIs provide a clean and structured way for applications to communicate.

The core ideas are:

*   resources represent data
    
*   HTTP methods represent actions
    
*   routes stay predictable
    
*   status codes describe results
    

Express.js makes REST API development simple because routing maps naturally to REST concepts.

Once you understand:

*   resources
    
*   HTTP methods
    
*   route conventions
    
*   request-response flow
    

designing APIs becomes much more intuitive.
