DEV Community

Shivam Yadav
Shivam Yadav

Posted on

REST API Design Made Simple with Express.js

REST API Design Made Simple with Express.js

Every backend developer reaches a point where they hear this sentence:

“Build a REST API.”

And suddenly everyone starts pretending they fully understand:

  • REST
  • resources
  • endpoints
  • CRUD
  • status codes
  • HTTP methods

Meanwhile beginners are sitting there like:

“Brother I just learned app.get() yesterday.”

The good news?

REST APIs are actually based on very simple ideas.

Most of the scary terminology comes from people explaining it in the most robotic way possible.

So in this article, we are going to understand REST APIs properly using:

  • simple language
  • real-world examples
  • Express.js
  • one resource (users)
  • practical route design

By the end, REST APIs will stop looking like mysterious backend rituals.


First Understand: What Is an API?

API stands for:

Application Programming Interface

Sounds complicated.

But the real idea is simple.

An API allows:

two systems to communicate.


Real-Life API Analogy

Imagine a restaurant.

You:

  • place order to waiter

Waiter:

  • takes request to kitchen

Kitchen:

  • prepares food

Waiter:

  • brings response back

Here:

  • you = client
  • kitchen = server
  • waiter = API

The API acts as:

communication bridge between client and server.


What Is REST?

REST stands for:

Representational State Transfer

Yes.
That name sounds like it was invented by someone who hated beginners.

Thankfully:
you do not need to memorize the full form to understand REST.

REST is basically:

a standard way to design APIs.

It focuses on:

  • clean routes
  • proper HTTP methods
  • predictable structure

REST APIs Work Around Resources

This is the most important REST concept.

Resource = Anything your application manages

Examples:

  • users
  • products
  • orders
  • posts
  • comments

If your app stores or manages something,
it becomes a resource.


Example Resource: Users

We will use:

users

throughout this article.

Example user object:

```json id="a81ks2"
{
"id": 1,
"name": "Shivam",
"email": "shivam@gmail.com"
}




---

# REST API Structure Idea

REST focuses on:

* resources
* actions performed on resources

Instead of routes like:



```text id="b72ms1"
/getUsers
/createUser
/deleteUser
Enter fullscreen mode Exit fullscreen mode

REST prefers:

```text id="c63ps2"
/users
/users/:id




Cleaner.
Predictable.
Industry standard.

---

# Understanding HTTP Methods

REST APIs mainly use:

* GET
* POST
* PUT
* DELETE

These methods define:

> what action should happen.

---

# CRUD Operations

REST APIs usually map to CRUD operations.

CRUD means:

| Operation | Meaning     |
| --------- | ----------- |
| Create    | Add data    |
| Read      | Fetch data  |
| Update    | Modify data |
| Delete    | Remove data |

---

# CRUD vs HTTP Methods

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

This mapping is core REST design.

---

# Setting Up Express Server

First install Express:



```bash id="d54jd1"
npm install express
Enter fullscreen mode Exit fullscreen mode

Now create:

```text id="e45ks2"
app.js




Basic setup:



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

const app = express();

app.use(express.json());

app.listen(3000, () => {
    console.log("Server Running");
});
Enter fullscreen mode Exit fullscreen mode

Why express.json()?

It parses incoming JSON data.

Without it:
req.body becomes undefined.

And beginners spend 45 minutes questioning reality.


GET Method — Fetching Users

GET is used for:

reading data

Example route:

```js id="g27ps2"
app.get("/users", (req, res) => {

res.send("All Users");
Enter fullscreen mode Exit fullscreen mode

});




When client requests:



```text id="h18ks1"
GET /users
Enter fullscreen mode Exit fullscreen mode

server returns users.


Fetch Single User

```js id="i09jd2"
app.get("/users/:id", (req, res) => {

res.send(`User ID: ${req.params.id}`);
Enter fullscreen mode Exit fullscreen mode

});




Example request:



```text id="j90ms2"
GET /users/5
Enter fullscreen mode Exit fullscreen mode

Response:

```text id="k81ps1"
User ID: 5




---

# Understanding Route Parameters



```js id="l72ks2"
:id
Enter fullscreen mode Exit fullscreen mode

means:

dynamic value

Useful for:

  • user IDs
  • product IDs
  • order IDs

POST Method — Creating Data

POST is used for:

creating resources

Example:

```js id="m63ms1"
app.post("/users", (req, res) => {

const user = req.body;

res.send("User Created");
Enter fullscreen mode Exit fullscreen mode

});




Request body example:



```json id="n54ps2"
{
    "name": "Shivam",
    "email": "shivam@gmail.com"
}
Enter fullscreen mode Exit fullscreen mode

This creates new user.


PUT Method — Updating Data

PUT is used for:

updating existing resource

Example:

```js id="o45ks1"
app.put("/users/:id", (req, res) => {

res.send(`Updated User ${req.params.id}`);
Enter fullscreen mode Exit fullscreen mode

});




Request:



```text id="p36jd1"
PUT /users/2
Enter fullscreen mode Exit fullscreen mode

Meaning:
update user with ID 2.


DELETE Method — Removing Data

DELETE removes resource.

Example:

```js id="q27ms2"
app.delete("/users/:id", (req, res) => {

res.send(`Deleted User ${req.params.id}`);
Enter fullscreen mode Exit fullscreen mode

});




Request:



```text id="r18ps1"
DELETE /users/2
Enter fullscreen mode Exit fullscreen mode

REST Route Design Principles

Good REST APIs follow clean naming conventions.


Good Routes

```text id="s09ks2"
GET /users
GET /users/:id
POST /users
PUT /users/:id
DELETE /users/:id




---

# Bad Routes



```text id="t90jd1"
/getAllUsers
/createNewUser
/updateUserData
/deleteThisUser
Enter fullscreen mode Exit fullscreen mode

REST prefers:

  • nouns
  • resources
  • clean structure

Not action-heavy route names.

Because HTTP methods already define action.


REST Request-Response Lifecycle

```text id="u81ms2"
Client Sends Request

Express Route Matches

Controller Logic Runs

Response Sent Back




This cycle powers modern web applications.

---

# Status Codes Basics

Status codes tell client:

> what happened.

Very important in APIs.

---

# Common Status Codes

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

---

# Sending Status Codes

Example:



```js id="v72ps1"
res.status(200).send("Success");
Enter fullscreen mode Exit fullscreen mode

Example: Resource Created

```js id="w63ks2"
res.status(201).send("User Created");




201 means:
new resource successfully created.

---

# Example: Not Found



```js id="x54ms1"
res.status(404).send("User Not Found");
Enter fullscreen mode Exit fullscreen mode

Useful when requested data does not exist.


Why Status Codes Matter

Without status codes,
client cannot properly understand:

  • success
  • failure
  • permission issues
  • validation errors

They are basically:

backend communication signals.


Real-World REST API Example

Imagine building social media app.

Possible REST routes:

```text id="y45ps2"
GET /posts
POST /posts
GET /posts/:id
PUT /posts/:id
DELETE /posts/:id




Same REST principles everywhere.

---

# Full Example Together



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

const app = express();

app.use(express.json());

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

app.get("/users/:id", (req, res) => {
    res.send(`User ${req.params.id}`);
});

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

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

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

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Simple REST API structure.

Clean.
Readable.
Scalable.


Why REST Became Popular

REST APIs are:

  • simple
  • predictable
  • scalable
  • easy for frontend/backend communication

Almost every modern application:

  • mobile apps
  • web apps
  • dashboards
  • frontend frameworks

communicates using APIs.

And REST became one of the most common standards.


Common Beginner Mistakes


Using Verbs in Routes

Bad:

```text id="a25jd2"
/createUser




Good:



```text id="b16ms1"
/users
Enter fullscreen mode Exit fullscreen mode

Forgetting HTTP Method Meaning

Using GET for deletion?

Backend police arriving soon.


Ignoring Status Codes

Always send meaningful responses.


Confusing PUT and POST

Remember:

Method Purpose
POST Create
PUT Update

REST Is About Consistency

The biggest strength of REST APIs is:

predictability

When APIs follow proper conventions,
developers instantly understand:

  • routes
  • actions
  • behavior

Without reading entire backend code.

That consistency matters heavily in real-world systems.


Quick Revision

REST Means:

```text id="c07ps1"
Representational State Transfer




Standard API design approach.

---

## Resource Example:



```text id="d98ks2"
/users
Enter fullscreen mode Exit fullscreen mode

HTTP Methods:

Method Purpose
GET Fetch
POST Create
PUT Update
DELETE Remove

Common Status Codes:

Code Meaning
200 Success
201 Created
404 Not Found

Final Thoughts

REST APIs look complicated only because of the terminology.

At its core,
REST is simply:

organizing communication between client and server properly.

Client asks for resource.
Server processes request.
Server returns response.

That’s the entire game.

Once you understand:

  • resources
  • routes
  • HTTP methods
  • status codes

backend development starts feeling far more structured and logical.

And honestly,
most modern applications are basically:

different systems sending requests to each other all day long.

Top comments (0)