Learn REST-API
REST API
A REST API (Representational State Transfer) is a web service that allows different applications to communicate over the internet using standard HTTP methods like GET, POST, PUT, and DELETE. It follows REST principles, making it lightweight, scalable, and easy to use.
REST API Principles
Client-Server Architecture
A REST API follows a client-server architecture, where the client and server are separate. The client sends requests, and the server processes them and responds, allowing different clients like web and mobile apps to communicate with the same backend.
Statelessness
Before understanding statelessness, you need to understand statefulness.
- Statefulness: The server stores and manages user session data, such as authentication details and recent activities.
- Statelessness: The server does not store any information. Each request is independent, and the client must include all necessary data, like authentication details and query parameters, in every request.
This behavior makes the server scalable, reliable, and reduces server load.
Caching
Caching improves performance by storing responses that can be reused, reducing the need for repeated requests to the server. This minimizes response time and server load.
Uniform Interface
A uniform interface ensures consistency by using standard HTTP methods like GET, POST, PUT, and DELETE. This makes the API predictable and easy to use.
Layered System
A layered system divides tasks into separate layers, like security, caching, and user requests. The client only interacts with the top layer, making the system easier to manage and more flexible.
Start To Code
I use Node.js and some popular packages:
- Express: A Node.js web framework used for building web applications and APIs.
- Joi: A package used to validate user input, ensuring data integrity and security.
basic code
const express = require("express");
const app = express();
const joi = require("joi");
app.use(express.json());
//data
customers = [
{name : "user1", id : 1},
{name : "user2", id : 2},
{name : "user3", id : 3}
]
//listen
const port = process.env.PORT || 8080;
app.listen(port, ()=> console.log("listening on ",port));
//function
function validateUserName(customer){
schema = joi.object({
name : joi.string().min(3).required()
});
return schema.validate(customer)
}
GET
GET is used to retrieve data from the server. the response code is 200 if successful.
app.get('/api/customers',(req,res)=>{
res.send(customers);
});
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/get-all.png?w=1024)
get specific user details
app.get('/api/customer/:id', (req,res)=>{
const user_details = customers.find(user => req.params.id == user.id );
if(!user_details){
res.status(404).send("Data Not Found");
}else{
res.status(200).send(user_details)
}
});
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/specific-get.png?w=1024)
POST
The POST method is used to upload data to the server. The response code is 201, and I used the validateUserName
function to validate a username.
app.post('/api/customer/add',(req, res)=>{
const {error} = validateUserName(req.body);
if(error){
res.status(400).send(error.details[0].message);
}
else{
customer = {
name : req.body.name,
id : customers.length + 1
}
customers.push(customer);
res.status(201).send("data inserted successfully");
}
});
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/post.png?w=1024)
Β
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/post-get.png?w=1024)
PATCH
The PATCH method is used to update existing data partially. To update the entire user data, the PUT method should be used.
app.patch('/api/customer/:id', (req, res)=>{
const customer = customers.find(user => user.id == req.params.id);
const {error} = validateUserName(req.body);
if(!customer){
res.status(404).send("Data Not Found");
}
else if(error){
console.log(error)
res.status(400).send(error.details[0].message);
}
else{
customer.name = req.body.name;
res.status(200).send("successfully updated");
}
});
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/patch.png?w=1024)
Β
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/patch-get.png?w=1024)
DELETE
The DELETE method is used to remove user data.
app.delete('/api/customer/:id', (req,res)=>{
const user = customers.find(user => user.id == req.params.id);
index = customers.indexOf(user);
if(!user){
console.log("test")
res.status(404).send("Data Not Found");
}
else{
customers.splice(index,1);
res.status(200).send("successfully deleted");
}
});
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/delete.png?w=1024)
Β
![](https://krishnasubramaniyan.wordpress.com/wp-content/uploads/2025/02/del-get.png?w=1024)
What I Learned
CRUD Operations with REST API
I learned the basics of REST API and CRUD operations, including the uniform methods GET, POST, PUT, PATCH, and DELETE.
Status Codes
REST APIs strictly follow status codes:
- 200 β OK
- 201 β Created successfully
- 400 β Bad request
- 204 β No content
- 404 β Page not found
Joi Package
For server-side validation, the Joi package is used. It helps verify user data easily.
Middleware
Using app.use(express.json())
as middleware ensures that for POST, PATCH, and PUT methods, JSON-formatted user data is parsed into an object accessible via req.body
.
Β