# REST API Design Made Simple with Express.js

When building backend applications, one of the most common patterns you will use is a **REST API**. It provides a clean and structured way for clients (frontend, mobile apps) to communicate with your server.

In this blog, you will understand REST concepts in a simple way and learn how to design APIs using Express.js.

* * *

## 1\. What REST API Means

REST stands for <mark class="bg-yellow-200 dark:bg-yellow-500/30">Representational State Transfer</mark>.

A REST API is a way of designing APIs where:

*   Everything is treated as a resource
    
*   Each resource is accessed using a URL
    
*   Standard HTTP methods are used to perform actions
    

### Example:

```javascript
/users
/products
/orders
```

Each of these represents a resource.

* * *

## 2\. Resources in REST Architecture

A **resource** is any data or entity in your application.

Examples:

*   User
    
*   Product
    
*   Order
    

Each resource:

*   Has a unique URL
    
*   Can be created, read, updated, or deleted
    

### Example:

```plaintext
/users        → collection of users
/users/101    → specific user
```

* * *

## 3\. HTTP Methods

REST APIs use standard HTTP methods to define actions.

* * *

### a) GET (Read data)

Used to fetch data from the server.

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

* * *

### b) POST (Create data)

Used to create a new resource.

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

* * *

### c) PUT (Update data)

Used to update an existing resource.

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

* * *

### d) DELETE (Remove data)

Used to delete a resource.

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

* * *

## 4\. Status Codes Basics

Status codes tell the client what happened with the request.

### Common status codes:

*   **200 OK** → Request successful
    
*   **201 Created** → Resource created successfully
    
*   **400 Bad Request** → Invalid input
    
*   **401 Unauthorized** → Authentication required
    
*   **404 Not Found** → Resource not found
    
*   **500 Internal Server Error** → Server error
    

### Example:

```js
res.status(200).json({ message: 'Success' });
```

* * *

## 5\. Designing Routes Using REST Principles

REST APIs follow a consistent structure.

### Good REST design:

```plaintext
GET    /users        → get all users
GET    /users/:id    → get one user
POST   /users        → create user
PUT    /users/:id    → update user
DELETE /users/:id    → delete user
```

### Key rules:

*   Use **nouns**, not verbs
    
*   Keep URLs clean and meaningful
    
*   Use HTTP methods for actions
    

* * *

## 6\. Example Resource: Users

Let’s build a simple example using Express.

```js
import express from "express";
const app = express();

app.use(express.json());

let users = [];

// GET all users
app.get('/users', (req, res) => {
  res.json(users);
});

// GET single user
app.get('/users/:id', (req, res) => {
  const user = users.find(u => u.id == req.params.id);
  if (!user) return res.status(404).send('User not found');
  res.json(user);
});

// POST create user
app.post('/users', (req, res) => {
  const user = { id: Date.now(), ...req.body };
  users.push(user);
  res.status(201).json(user);
});

// PUT update user
app.put('/users/:id', (req, res) => {
  const index = users.findIndex(u => u.id == req.params.id);
  if (index === -1) return res.status(404).send('User not found');

  users[index] = { ...users[index], ...req.body };
  res.json(users[index]);
});

// DELETE user
app.delete('/users/:id', (req, res) => {
  users = users.filter(u => u.id != req.params.id);
  res.send('User deleted');
});

app.listen(3000);
```

* * *

## Final Understanding

*   REST API is a structured way to design backend services
    
*   Everything is treated as a resource
    
*   HTTP methods define actions
    
*   Status codes indicate results
    
*   Routes should follow clean and consistent patterns
    

* * *

## Summary

REST API design helps you build scalable and maintainable backend systems by following clear rules and standards. By treating data as resources and using HTTP methods correctly, your APIs become predictable and easy to use. Express.js makes implementing REST APIs simple with its clean routing system. Once you understand these principles, you can design APIs that are easy to expand, debug, and integrate with different clients such as web and mobile applications.
