Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Published
5 min read
REST API Design Made Simple with Express.js

Modern web and mobile applications constantly communicate with servers to send and receive data.

Whenever you:

  • fetch users

  • create posts

  • update profiles

  • delete products

you are usually interacting with a REST API.

REST APIs are one of the most widely used ways to build backend services, and Express.js makes creating them simple and powerful.

In this article, we’ll understand what REST APIs are, how REST architecture works, HTTP methods, status codes, and how to design clean RESTful routes using Express.js.


What is a REST API?

REST stands for:

Representational State Transfer

A REST API is a structured way for clients and servers to communicate over HTTP.

The client sends requests, and the server sends responses.

Example:

Frontend App  →  REST API  →  Database

The API acts as a bridge between the frontend and backend.

Understanding APIs in Simple Words

An API is like a waiter in a restaurant.

Customer → Waiter → Kitchen
  • Customer = Client

  • Waiter = API

  • Kitchen = Server/Database

The client requests data through the API, and the server processes and returns a response.

Resources in REST Architecture

REST APIs are built around resources.

A resource is simply a piece of data or an entity in the system.

Examples:

  • users

  • products

  • posts

  • orders

In REST, resources are usually represented using nouns.

Good examples:

/users
/products
/orders

Bad examples:

/getUsers
/createProduct
/deleteOrder

REST focuses on resources, not actions.


HTTP Methods in REST APIs

REST APIs use HTTP methods to define actions.

These methods map directly to CRUD operations.

CRUD Operation HTTP Method Purpose
Create POST Add new data
Read GET Fetch data
Update PUT Update existing data
Delete DELETE Remove data

GET Request

Used to fetch data.

Example:

GET /users

Express example:


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

Fetch a single user:


GET /users/1

POST Request

Used to create new data.

Example:

POST /users

Express example:


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

PUT Request

Used to update existing data.

Example:

PUT /users/1

Express example:

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

DELETE Request

Used to remove data.

Example:

DELETE /users/1

Express example:

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

REST Route Design Principles

REST APIs follow clean and predictable route naming conventions.

Use Nouns Instead of Verbs

Correct:

/users
/products

Incorrect:

/getUsers
/addProduct

The HTTP method already defines the action.

Use Plural Resource Names

Preferred:

/users
/posts
/orders

This makes APIs consistent and scalable.

Keep URLs Clean and Predictable

Good examples:

/users
/users/1
/users/1/posts

Avoid deeply nested complicated routes.

Example REST API for Users Resource

Here’s a clean RESTful design for users.

Action Method Route
Get all users GET /users
Get single user GET /users/:id
Create user POST /users
Update user PUT /users/:id
Delete user DELETE /users/:id

This structure becomes very intuitive once you follow REST principles consistently.

REST Request-Response Lifecycle

A REST API request typically follows this flow:


Client Request
      ↓
Express Route
      ↓
Business Logic
      ↓
Database Operation
      ↓
JSON Response

Status Codes Basics

HTTP status codes help clients understand the result of a request.

Common Success Codes

Status Code Meaning
200 Success
201 Resource Created
204 No Content

Example:

res.status(201).json({
    message: "User created"
});

Common Error Codes

Status Code Meaning
400 Bad Request
401 Unauthorized
404 Not Found
500 Internal Server Error

Example:

res.status(404).json({
    message: "User not found"
});

Returning JSON Responses

REST APIs commonly return JSON data.

Example:

res.json({
    id: 1,
    name: "Rohit"
});

Typical response:

{
  "id": 1,
  "name": "Rohit"
}

Building a Simple REST API in Express

Example:


import express from 'express';

const app = express();

app.use(express.json());

let users = [];

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

app.post("/users", (req, res) => {
    users.push(req.body);

    res.status(201).json({
        message: "User created"
    });
});

app.listen(3000);

This demonstrates basic REST principles in action.


Conclusion

REST APIs provide a clean and standardized way for clients and servers to communicate.

By understanding:

  • resources

  • HTTP methods

  • route design

  • status codes

  • request-response flow

you can build scalable and maintainable backend applications using Express.js.

Once you follow REST principles consistently, your APIs become easier to understand, easier to document, and much easier to scale in real-world applications.