REST API Design Made Simple with Express.js

Like to study,fitness freak,my looks is my first priority, hardworking person, like discipline and love to learn new thing
π Introduction
In modern web development, APIs are everywhere.
π They allow different systems (frontend, backend, mobile apps) to communicate with each other
One of the most popular styles of building APIs is:
π REST (Representational State Transfer)
Letβs understand REST in the simplest way possible π₯
π§ What Is a REST API?
A REST API is:
π A way for a client (browser/app) to communicate with a server using HTTP
π‘ Simple Idea
Client β Request β Server β Response
π Client asks β Server responds
π¦ What Are Resources in REST?
In REST, everything is treated as a resource
Examples:
Users π€
Products π¦
Orders π§Ύ
π Each resource has its own URL
Example:
/users
/products
βοΈ HTTP Methods (CRUD Operations)
REST uses HTTP methods to perform actions:
| Operation | Method | Example |
|---|---|---|
| Read | GET | /users |
| Create | POST | /users |
| Update | PUT | /users/1 |
| Delete | DELETE | /users/1 |
π This is called CRUD mapping
π Diagram Idea: CRUD Mapping
GET β Read
POST β Create
PUT β Update
DELETE β Delete
βοΈ Designing Routes (Users Example)
Letβs build a simple REST API for users
1οΈβ£ GET All Users
app.get("/users", (req, res) => {
res.send("Get all users");
});
2οΈβ£ GET Single User
app.get("/users/:id", (req, res) => {
res.send(`Get user ${req.params.id}`);
});
3οΈβ£ Create User
app.post("/users", (req, res) => {
res.send("User created");
});
4οΈβ£ Update User
app.put("/users/:id", (req, res) => {
res.send(`User ${req.params.id} updated`);
});
5οΈβ£ Delete User
app.delete("/users/:id", (req, res) => {
res.send(`User ${req.params.id} deleted`);
});
π’ Status Codes (Basics)
Status codes tell the client what happened:
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 400 | Bad Request |
| 404 | Not Found |
| 500 | Server Error |
Example:
res.status(201).send("User created");
π Diagram Idea: Request Lifecycle
Client β Request β Route β Logic β Response
π§ REST Design Principles
π Keep URLs clean and meaningful:
β Bad:
/getUsers
/createUser
β Good:
/users
/users/1
π Use HTTP methods instead of action names
π§© Real-World Example
/users β GET all users
/users/101 β GET user
/users β POST new user
/users/101 β PUT update
/users/101 β DELETE
π§ Conceptual Understanding
π REST = Structured way to design APIs π Resources + Methods = Clean API design
π Conclusion
REST APIs make your backend:
Organized
Scalable
Easy to understand
β¨ Final Tip
If your API URLs look like actions β rethink them as resources π
