REST API Design Made Simple with Express.js
Building APIs is one of the most essential skills for any developer today. Whether you're creating a web app, mobile backend, or microservices architecture, understanding how to design a clean and efficient REST API is crucial.
In this blog, we’ll simplify REST API design using Express.js so you can build scalable and production-ready APIs with confidence.
What is a REST API?
REST (Representational State Transfer) is an architectural style for designing networked applications.
In simple terms: A REST API allows clients (frontend, mobile apps) to communicate with a server using HTTP requests.
Basic HTTP Methods
Each REST API revolves around standard HTTP methods:
| Method | Purpose | Example |
|---|---|---|
| GET | Fetch data | Get all users |
| POST | Create new data | Add a user |
| PUT | Update full data | Update user details |
| PATCH | Update partial | Update user email |
| DELETE | Remove data | Delete a user |
Setting Up Express.js
First, install Express:
npm init -y
npm install express
Create a basic server:
const express = require("express");
const app = express();
app.use(express.json());
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Designing a Simple REST API
Let’s build a User API.
Sample Data
let users = [
{ id: 1, name: "Kanishka" },
{ id: 2, name: "Rahul" }
];
1. GET – Fetch All Users
app.get("/users", (req, res) => {
res.json(users);
});
2. GET – Fetch Single User
app.get("/users/:id", (req, res) => {
const user = users.find(u => u.id == req.params.id);
if (!user) return res.status(404).json({ message: "User not found" });
res.json(user);
});
3. POST – Create User
app.post("/users", (req, res) => {
const newUser = {
id: users.length + 1,
name: req.body.name
};
users.push(newUser);
res.status(201).json(newUser);
});
4. PUT – Update User
app.put("/users/:id", (req, res) => {
const user = users.find(u => u.id == req.params.id);
if (!user) return res.status(404).json({ message: "User not found" });
user.name = req.body.name;
res.json(user);
});
5. DELETE – Remove User
app.delete("/users/:id", (req, res) => {
users = users.filter(u => u.id != req.params.id);
res.json({ message: "User deleted" });
});
Best Practices for REST API Design
1. Use Proper Naming
- Use nouns, not verbs ✔️
/users❌/getUsers
2. Use Status Codes Correctly
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 400 | Bad Request |
| 404 | Not Found |
| 500 | Server Error |
3. Keep APIs Stateless
Each request should contain all required info
Server should not store client state
4. Use Middleware
app.use((req, res, next) => {
console.log(`\({req.method} \){req.url}`);
next();
});
5. Error Handling
app.use((err, req, res, next) => {
res.status(500).json({ message: err.message });
});
6. Version Your API
/api/v1/users
Bonus: Add Basic Validation
if (!req.body.name) {
return res.status(400).json({ message: "Name is required" });
}
Real-World Structure
For scalable apps:
project/
│
├── routes/
├── controllers/
├── models/
├── middleware/
└── server.js
Conclusion
Designing a REST API with Express.js is simple once you understand the core principles:
Use proper HTTP methods
Follow clean URL structure
Handle errors properly
Keep it stateless
With these basics, you can build powerful backends for:
Web apps
Mobile apps
SaaS platforms
Final Tip
Always design your API from the client’s perspective — make it intuitive, predictable, and easy to use.
