Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Updated
6 min read
REST API Design Made Simple with Express.js

REST API Design Made Simple with Express.js

Modern web applications constantly communicate between:

  • frontend and backend

  • mobile apps and servers

  • services and databases

This communication usually happens through:

REST APIs

REST APIs are one of the most important concepts in backend development.

In this article, we’ll learn:

  • What REST API means

  • Resources in REST architecture

  • HTTP methods

  • Status codes basics

  • Designing routes using REST principles

  • Building a simple users resource example

Let’s begin.


What Is an API?

API stands for:

Application Programming Interface

An API allows different applications to communicate with each other.

Example:

Frontend ↔ Backend

The frontend sends requests and the backend sends responses.


What Does REST Mean?

REST stands for:

Representational State Transfer

It is a design style for building APIs.

REST APIs organize data around:

Resources

What Is a Resource in REST?

A resource is simply:

A piece of data

Examples:

  • users

  • products

  • orders

  • posts

In REST APIs, each resource gets its own routes.

Example:

/users
/products
/orders

Real-World Analogy

Imagine a restaurant.

  • You place an order → Request

  • Kitchen processes it → Server Logic

  • Food arrives → Response

REST APIs work similarly.

Client Request
      ↓
Server Processes
      ↓
Response Returned

Understanding HTTP Methods

REST APIs use HTTP methods to define actions.

The four most common methods are:

  • GET

  • POST

  • PUT

  • DELETE

These map directly to CRUD operations.


CRUD vs HTTP Methods

CRUD Operation HTTP Method
Create POST
Read GET
Update PUT
Delete DELETE

1. GET Request

Used for:

Fetching data

Example:

GET /users

Fetches all users.


Express Example

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

2. POST Request

Used for:

Creating new data

Example:

POST /users

Creates a new user.


Express Example

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

3. PUT Request

Used for:

Updating existing data

Example:

PUT /users/1

Updates user with ID:

1

Express Example

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

4. DELETE Request

Used for:

Removing data

Example:

DELETE /users/1

Deletes user with ID:

1

Express Example

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

REST Route Design Principles

REST APIs follow clean route naming conventions.


Good REST Routes

/users
/users/1
/products
/orders

Avoid Verb-Based Routes

Avoid routes like:

/getUsers
/createUser
/deleteUser

HTTP methods already define the action.


Understanding Resource IDs

Example:

/users/10

Here:

10

represents a specific user resource.


REST Request-Response Lifecycle

Client Request
       ↓
Express Route
       ↓
Business Logic
       ↓
Database
       ↓
Response Sent

Example Users API

Let’s design a simple REST API for users.


Get All Users

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

Get Single User

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

Create User

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

Update User

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

Delete User

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

Status Codes Basics

Servers send status codes to indicate request result.


Common Status Codes

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

Example Using Status Codes

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

Why Status Codes Matter

Status codes help clients understand:

  • success

  • failure

  • missing data

  • authentication problems

without reading long messages.


REST API Naming Best Practices


Use Nouns

Good:

/users

Bad:

/getUsers

Keep Routes Consistent

Follow same pattern everywhere.

Example:

/users
/products
/orders

Use Plural Resource Names

Usually preferred:

/users

instead of:

/user

Real-World REST Example

Imagine Instagram.

Possible APIs:

GET /posts
POST /posts
GET /users
DELETE /comments/10

REST principles help organize large systems cleanly.


Why REST APIs Became Popular

REST APIs are popular because they are:

✅ Simple ✅ Scalable ✅ Easy to understand ✅ Language independent ✅ Frontend friendly

Almost every modern application uses APIs.


Common Beginner Mistakes

Using Wrong HTTP Methods

Example:

Using:

GET

for deleting data is incorrect.


Poor Route Naming

Avoid:

/createProduct

Use:

POST /products

instead.


Ignoring Status Codes

Always send proper responses.


Practice Assignment

Try these exercises yourself.


1. Create Express Server

Setup a basic Express application.


2. Create Users Routes

Add routes for:

  • GET

  • POST

  • PUT

  • DELETE


3. Use Route Parameters

Access user ID using:

req.params.id

4. Send Status Codes

Use:

res.status()

inside responses.


5. Design Another Resource

Create REST routes for:

/products

or:

/movies

Final Thoughts

REST APIs are the foundation of modern backend development.

They help applications communicate using:

  • clean routes

  • HTTP methods

  • structured responses

The key idea is:

REST APIs organize backend functionality around resources.

Understanding REST API design is extremely important for:

  • frontend developers

  • backend engineers

  • full-stack development

  • scalable application architecture

As you continue learning Express.js and backend development, REST principles will become a core part of building professional APIs.


And now, you know how REST API design works with Express.js.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!