Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Updated
•View as Markdown
REST API Design Made Simple with Express.js

By now, you’ve learned how to build servers, handle requests, manage authentication, and work with files.

The next step is to structure your backend in a way that is clean, predictable, and scalable.

This is where REST API design comes in.


What is a REST API

An API is a way for two systems to communicate.

In web applications:

  • the client (browser or app) sends a request

  • the server processes it

  • the server sends a response

REST is a design style for building APIs.

It defines how you structure your endpoints and interact with resources.


Understanding Resources in REST

In REST, everything is treated as a resource.

A resource is simply data.

For example:

  • users

  • products

  • orders

Instead of thinking in terms of actions, you think in terms of resources.


HTTP Methods in REST

REST uses HTTP methods to define actions.

GET

Used to retrieve data.

app.get("/users", (req, res) => {
  res.send("Get all users");
});

POST

Used to create new data.

app.post("/users", (req, res) => {
  res.send("Create user");
});

PUT

Used to update existing data.

app.put("/users/:id", (req, res) => {
  res.send("Update user");
});

DELETE

Used to remove data.

app.delete("/users/:id", (req, res) => {
  res.send("Delete user");
});

CRUD Mapping

Create → POST  
Read   → GET  
Update → PUT  
Delete → DELETE

This is the foundation of REST APIs.


Designing Routes Using REST Principles

Instead of writing action-based routes like:

/getUsers  
/createUser  
/deleteUser

REST encourages resource-based routes:

/users  
/users/:id

The HTTP method defines the action.

This keeps your API clean and predictable.


Example: Users Resource

Let’s design a simple users API.

app.get("/users", (req, res) => {
  res.send("Get all users");
});

app.get("/users/:id", (req, res) => {
  res.send("Get single user");
});

app.post("/users", (req, res) => {
  res.send("Create user");
});

app.put("/users/:id", (req, res) => {
  res.send("Update user");
});

app.delete("/users/:id", (req, res) => {
  res.send("Delete user");
});

This follows REST principles.


Status Codes Basics

Status codes tell the client what happened.

Some common ones:

200 → Success  
201 → Created  
400 → Bad Request  
404 → Not Found  
500 → Server Error

Example:

res.status(201).send("User created");

REST Request-Response Flow

Client → HTTP Request → Server → Process → HTTP Response → Client

Each request is independent and contains all the information needed.


Why REST Matters

REST helps you:

  • design clean APIs

  • maintain consistency

  • scale applications

  • collaborate with frontend teams

It is widely used in real-world applications.


Real-World Thinking

A good API is not just about working code.

It is about:

  • clarity

  • predictability

  • structure

When your API is well-designed, it becomes easier to use and maintain.


Final Thought

REST is not a framework.

It is a way of thinking.

Instead of focusing on functions, you focus on resources and actions.

Once you understand this, your backend starts looking like a real system.

API DESIGN

Part 1 of 1

API Design with Express.js is a practical series focused on building clean, scalable, and maintainable REST APIs. You’ll learn how to design routes, structure endpoints, use proper HTTP methods, handle responses, and follow best practices used in real-world backend systems.