REST API Design with Express.js

Have you ever wondered what happens when you enter your email and password on a website and click “Login”?
Behind the scenes, your browser sends a request to a server, and the server responds with the required data. This communication happens through something called an API.
In this blog, we’ll understand what REST APIs are and learn how to design them using Express.js in a simple and practical way.
How Client–Server Communication Works
In web applications, there are two main components:
Client → The frontend (browser, mobile app)
Server → The backend that processes requests and sends responses
The client and server communicate with each other using APIs.
What is an API?
API stands for Application Programming Interface.
An API acts as a bridge between the client and the server. It allows them to communicate with each other.
Example:
When you click the login button, the client sends a request to the server through an API, and the server responds with success or failure.
Types of APIs
There are different types of APIs based on architecture:
REST – Most commonly used, based on HTTP
GraphQL – Allows flexible data fetching
WebSocket – Enables real-time communication
gRPC – High-performance communication (by Google)
In this blog, we will focus on REST APIs.
What is a REST API?
REST stands for Representational State Transfer.
A REST API is an architectural style used to design networked applications. It allows clients to communicate with servers using HTTP methods like GET, POST, PUT, and DELETE.
It was introduced by Roy Fielding in 2000 and is widely used in modern web development.
Resources in REST Architecture
In REST, everything is treated as a resource.
A resource represents any data or entity in your application.
Examples:
users
products
orders
Each resource is identified using a URL.
Example:
GET /users → Get all users
GET /users/1 → Get a specific user
HTTP Methods
HTTP methods define what action should be performed on a resource.
GET → Retrieve data
POST → Create new data
PUT → Update existing data
DELETE → Remove data
Status Codes Basics
Status codes are 3-digit numbers sent by the server to indicate the result of a request.
Categories:
1xx → Informational
2xx → Success (e.g., 200 OK)
3xx → Redirection
4xx → Client errors (e.g., 404 Not Found)
5xx → Server errors (e.g., 500 Internal Server Error)
Designing routes using REST principles
Let’s take a single resource: users
RESTful Routes:
GET /users → Get all users
GET /users/:id → Get a specific user
POST /users → Create a user
PUT /users/:id → Update a user
DELETE /users/:id → Delete a user
Building a Simple REST API with Express.js
Step 1: Initialize Project
npm init -y
Step 2: Install Express
npm install express
Step 3: Create server.js
Conclusion
REST APIs are the backbone of modern web applications. By understanding resources, HTTP methods, and proper route design, you can build scalable and clean backend systems.
With Express.js, creating REST APIs becomes simple and efficient.




