# REST API Design Made Simple with Express.js

APIs are like waiters in a restaurant. 🍜 The client (frontend/mobile app) gives a request, the server processes it, and the API delivers the response back.

In web development, REST APIs are one of the most common ways for apps to communicate.

* * *

# What is a REST API?

REST stands for **Representational State Transfer**.

A REST API follows a set of rules to manage and access resources using HTTP methods.

A **resource** can be anything:

*   users
    
*   products
    
*   posts
    
*   comments
    

Example:

```bash
/users
```

This represents a collection of users.

* * *

# HTTP Methods in REST

REST APIs mainly use four HTTP methods for CRUD operations.

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

Tiny naming mistake people make:

```bash
/getUsers
/createUser
```

This is not clean REST design because the HTTP method already tells the action. Your route should focus on the resource.

Correct way:

```bash
GET /users
POST /users
```

Your routes should look clean, not like a folder made by a sleep-deprived raccoon at 3AM.

* * *

# REST Request-Response Flow

1.  Client sends request
    
2.  Server receives request
    
3.  Express handles route
    
4.  Database logic runs
    
5.  Response is returned
    

Example:

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

* * *

# CRUD Example Using Express.js

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

app.use(express.json());

// GET users
app.get('/users', (req, res) => {
  res.status(200).json({ message: 'Users fetched' });
});

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

// PUT 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 on port 3000');
});
```

* * *

# Status Codes Basics

Status codes tell the client what happened.

| Status Code | Meaning |
| --- | --- |
| 200 | Success |
| 201 | Resource created |
| 400 | Bad request |
| 404 | Resource not found |
| 500 | Server error |

Example:

```js
res.status(404).json({ message: 'User not found' });
```

* * *

# Why REST APIs Matter

REST APIs make communication between frontend and backend structured and predictable.

Without proper API design, projects become chaos very quickly:

*   confusing routes
    
*   inconsistent responses
    
*   impossible debugging
    
*   future-you crying in production
    

Clean REST design keeps projects scalable and easier to maintain.

* * *

# Conclusion

REST APIs are the backbone of modern web applications. By following REST principles, using proper HTTP methods, and keeping routes resource-based, developers can build scalable and clean backend systems using Express.js.

Start simple:

```bash
/users
/products
/posts
```

Design around resources, not actions. That single habit already puts you ahead of many beginner backend developers.
