Skip to main content

Command Palette

Search for a command to run...

REST API Design Made Simple with Express.js

Updated
β€’4 min read

Building APIs is a core skill for any backend or full-stack developer. If you're working with Node.js, understanding how to design clean and scalable REST APIs using Express.js is essential.

This guide breaks it down in a simple, practical way.


πŸš€ What is a REST API?

A REST API (Representational State Transfer) is a way for different systems (usually a client and a server) to communicate over HTTP.

Think of it like this:

  • The client (browser, mobile app) sends a request

  • The server processes it

  • The server sends back a response

πŸ‘‰ Example:

  • You open Instagram β†’ app requests data β†’ server sends posts

So essentially:

APIs are the communication bridge between client and server.


πŸ“¦ What are Resources in REST?

In REST architecture, everything is treated as a resource.

A resource is simply:

Any data you want to expose via your API

Examples:

  • Users β†’ /users

  • Products β†’ /products

  • Orders β†’ /orders

Each resource is accessed via a URL endpoint.


πŸ”§ HTTP Methods (Core of REST)

REST APIs use HTTP methods to define actions on resources.

1. GET β†’ Fetch Data

Used to retrieve data from the server.

GET /users

πŸ‘‰ Fetch all users


2. POST β†’ Create Data

Used to create a new resource.

POST /users

πŸ‘‰ Create a new user


3. PUT β†’ Update Data

Used to update an existing resource (usually full update).

PUT /users/1

πŸ‘‰ Update user with ID = 1


4. DELETE β†’ Remove Data

Used to delete a resource.

DELETE /users/1

πŸ‘‰ Delete user with ID = 1


πŸ“Š Status Codes Basics

Status codes tell the client what happened with the request.

Common ones you should know:

  • 200 OK β†’ Request successful

  • 201 Created β†’ Resource created successfully

  • 400 Bad Request β†’ Client error

  • 404 Not Found β†’ Resource doesn’t exist

  • 500 Internal Server Error β†’ Server failed

πŸ‘‰ Example:

res.status(200).json(users);

πŸ›£οΈ Designing RESTful Routes

A good REST API follows clean and predictable naming conventions.

Rules:

  • Use nouns, not verbs

  • Keep URLs plural

  • Use IDs for specific resources


πŸ‘¨β€πŸ’» Example: Users Resource

Let’s design a simple users API using Express.js.

Setup

const express = require("express");
const app = express();

app.use(express.json());

Routes

Get all users

app.get("/users", (req, res) => {
  res.status(200).json({ message: "Get all users" });
});

Get single user

app.get("/users/:id", (req, res) => {
  res.status(200).json({ message: `Get user ${req.params.id}` });
});

Create user

app.post("/users", (req, res) => {
  res.status(201).json({ message: "User created" });
});

Update user

app.put("/users/:id", (req, res) => {
  res.status(200).json({ message: `User ${req.params.id} updated` });
});

Delete user

app.delete("/users/:id", (req, res) => {
  res.status(200).json({ message: `User ${req.params.id} deleted` });
});

🧠 Key Design Principles (Keep This in Mind)

  • Keep routes consistent and predictable

  • Use correct HTTP methods

  • Return meaningful status codes

  • Separate logic (routes vs controllers in real apps)

  • Think in terms of resources, not actions


πŸ”š Final Thoughts

REST API design is not about complexity β€” it's about clarity and consistency.

If your API:

  • Uses proper HTTP methods

  • Has clean route naming

  • Returns correct status codes

πŸ‘‰ You're already ahead of most beginners.

Once you're comfortable with this, the next step is:

  • Adding authentication (JWT)

  • Connecting databases (MongoDB)

  • Structuring scalable backend systems

More from this blog