# REST API Design Made Simple with Express.js

If you've ever used an app that fetches data from a server — like checking your Twitter feed or logging into a website — you've already used a REST API without knowing it. Let's break down what that actually means, and how you can build one using Express.js.

## What REST API Means

An API (Application Programming Interface) is basically a messenger between the client (your browser or app) and the server (where the data lives). The client sends a request, the server does something, and sends back a response.

REST stands for **Representational State Transfer**. It's not a technology — it's a set of rules for how that conversation between client and server should happen. When an API follows these rules, we call it a RESTful API.

Think of it like ordering food at a restaurant. You (the client) ask the waiter (the API) for something. The waiter goes to the kitchen (the server), gets what you need, and brings it back. Clean, structured, and predictable.

## Resources in REST Architecture

In REST, everything revolves around **resources**. A resource is just a thing — a user, a post, a product, an order. You give each resource its own URL, and that URL becomes its permanent address on the server.

For example, if you're working with users, the resource URL might look like:

```plaintext
/users
/users/1
```

That's it. Clean and simple. The URL tells you *what* you're dealing with.

## HTTP Methods

Once you have your resource, you need to tell the server *what you want to do* with it. That's where HTTP methods come in.

**GET** — Fetch data. You're just reading, not changing anything.

```plaintext
GET /users       → get all users
GET /users/1     → get user with ID 1
```

**POST** — Create something new. You're sending data to the server to store.

```plaintext
POST /users      → create a new user
```

**PUT** — Update an existing resource. You send the full updated version.

```plaintext
PUT /users/1     → update user with ID 1
```

**DELETE** — Remove a resource from the server.

```plaintext
DELETE /users/1  → delete user with ID 1
```

Four methods. Four actions. That's the core of REST.

![](https://cdn.hashnode.com/uploads/covers/696366dd106754dd4f43abd0/0577490d-7903-4448-bae5-c456b4b2555f.png align="center")

## Status Codes Basics

After every request, the server sends back a **status code** — a three-digit number that tells you what happened.

*   **200** — OK. Everything worked fine.
    
*   **201** — Created. A new resource was successfully made.
    
*   **400** — Bad Request. Something was wrong with the data you sent.
    
*   **404** — Not Found. That resource doesn't exist.
    
*   **500** — Internal Server Error. Something broke on the server's end.
    

You don't need to memorize all of them. Just know these five and you'll handle most situations.

## Designing Routes Using REST Principles

Here's how all of this looks together in Express.js using the `users` resource:

```js
const express = require('express');
const app = express();
app.use(express.json());

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

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

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

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

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

app.listen(3000, () => console.log('Server running on port 3000'));
```

Notice how the URL stays the same (`/users/:id`) — only the HTTP method changes. That's the REST way.

![](https://cdn.hashnode.com/uploads/covers/696366dd106754dd4f43abd0/46b0c019-7f7a-4b71-b480-f76de961b6b5.png align="center")

## Wrapping Up

REST API design isn't complicated once you see the pattern. Pick a resource, give it a clean URL, use the right HTTP method, and return the right status code. That's the whole game.

Express.js makes this even easier — it gets out of your way and lets you focus on building. Start with one resource like `users`, get comfortable with the four methods, and you'll find that every other resource follows the exact same pattern.

Simple, consistent, and it works.
