Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

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

In modern web development, applications need to communicate with each other. For example:

  • A frontend app needs data from a backend server

  • A mobile app needs user information from an API

  • A website needs to save or fetch data from a database

This communication happens through something called an API (Application Programming Interface).

A REST API is one of the most popular ways to build APIs.


What is REST API?

REST stands for Representational State Transfer.

REST API is a way for client and server to communicate using HTTP requests.

In REST:

  • Client sends a request (browser/app)

  • Server processes that request

  • Server sends back a response

REST APIs are widely used because they are simple, scalable, and easy to understand.

API as Communication System

Think of an API like a waiter in a restaurant:

  • You (client) place an order

  • Waiter (API) takes the order to kitchen (server)

  • Kitchen prepares food (data processing)

  • Waiter brings food back (response)

API is the bridge between client and server.


What are Resources in REST?

In REST, everything is treated as a resource.

A resource is any data object like:

  • Users

  • Products

  • Orders

  • Posts

Each resource has a unique URL.

Example:

  • /users → all users

  • /users/1 → user with ID 1


HTTP Methods in REST API

REST uses HTTP methods to perform actions on resources.

1. GET (Read Data)

Used to fetch data from server.

GET /users

Example: Get all users or a single user.

2. POST (Create Data)

Used to create new data.

POST /users

Example: Add a new user to database.

3. PUT or PATCH (Update Data)

Used to update existing data.

PUT /users/1

Example: Update user with ID 1.

4. DELETE (Remove Data)

Used to delete data.

DELETE /users/1

Example: Delete user with ID 1.


Status Codes in REST API

Status codes tell the result of a request.

Common Status Codes:

  • 200 OK → Request successful

  • 201 Created → New data created

  • 400 Bad Request → Invalid input

  • 401 Unauthorized → Login required

  • 404 Not Found → Resource not found

  • 500 Server Error → Something went wrong on server


Simple Example with Users Resource

Get Users

GET /users

Response:

[
  { "id": 1, "name": "Rahul" },
  { "id": 2, "name": "Amit" }
]

Create User

POST /users

Request:

{
  "name": "John"
}

Response:

{
  "message": "User created successfully"
}

REST Route Design Principles

Good REST APIs follow clean naming rules:

Good Practice:

  • /users

  • /users/1

  • /products

  • /orders

Bad Practice:

  • /getUsers

  • /createUser

  • /deleteUser?id=1

REST uses nouns, not verbs

REST Request-Response Flow

Client Request 
      ↓ 
HTTP Method (GET/POST/PUT/DELETE) 
      ↓ 
Server processes that request 
      ↓ 
Database operation (optional) 
      ↓ 
Server sends response 
      ↓ 
Client receives data

Conclusion

REST API is a simple and powerful way for communication between client and server. It uses HTTP methods to perform actions on resources and follows a clean structure for building scalable applications.

REST APIs make web development structured, predictable, and easy to scale