3.5 RESTful API Design with Express — A Beginner’s Guide for Future Dev Legends

Table of Contents

Hey there, little coder! 👋
Ever wondered how apps like Instagram or WhatsApp talk to their servers to fetch data? That’s where APIs come in.

And today, I’m going to show you how to build your own RESTful API using Node.js + Express, step by step 🛠️
Let’s make it fun, easy, and super useful — just like chatting with your favorite cousin 👨‍💻👧


🌐 What is a RESTful API?

Imagine a waiter in a restaurant 🍽️.
You ask them for a menu, place your order, update your choice, or even cancel it.

The waiter = API
The kitchen = Database

That’s what a RESTful API does — it follows rules (REST principles) and helps apps communicate with a backend server.


🧱 The Structure of a REST API

A REST API uses URLs to point to resources (like users, posts, products).
It uses HTTP methods to decide what to do with them.

ActionHTTP MethodExample URL
Get dataGET/users
Add dataPOST/users
Update dataPUT/users/123
Delete dataDELETE/users/123

Let’s build one with Express.js now! ⚙️


🛠️ Setting Up Express

First, we need to set up a Node.js project.

1. Init project

bashCopyEditmkdir my-api
cd my-api
npm init -y
npm install express

2. Create your first server

jsCopyEdit// index.js
const express = require('express');
const app = express();
const PORT = 3000;

app.use(express.json());

app.listen(PORT, () => {
  console.log(`🚀 Server running on http://localhost:${PORT}`);
});

👉 Visit http://localhost:3000 and see the magic!


🧑‍🍳 Designing RESTful Endpoints (CRUD Style)

Let’s say we’re building a simple User API.
Here’s how we structure our routes using Express:

🔍 1. GET – Fetch All Users

jsCopyEditlet users = [{ id: 1, name: "Vikas" }];

app.get('/users', (req, res) => {
  res.status(200).json(users);
});

➕ 2. POST – Add New User

jsCopyEditapp.post('/users', (req, res) => {
  const newUser = req.body;
  users.push(newUser);
  res.status(201).json({ message: "User added!", user: newUser });
});

🛠️ 3. PUT – Update a User

jsCopyEditapp.put('/users/:id', (req, res) => {
  const id = parseInt(req.params.id);
  const updatedData = req.body;

  users = users.map(user => user.id === id ? { ...user, ...updatedData } : user);
  res.status(200).json({ message: "User updated!" });
});

❌ 4. DELETE – Remove a User

jsCopyEditapp.delete('/users/:id', (req, res) => {
  const id = parseInt(req.params.id);
  users = users.filter(user => user.id !== id);
  res.status(200).json({ message: "User deleted!" });
});

📝 That’s a full CRUD API! Simple, right?


🔎 Using Query Parameters and Request Bodies

Let’s add a filter to search users by name.

jsCopyEditapp.get('/users/search', (req, res) => {
  const name = req.query.name;
  const result = users.filter(user => user.name.toLowerCase().includes(name.toLowerCase()));
  res.json(result);
});

✅ Try visiting: /users/search?name=vikas


📦 Handling JSON Responses & Status Codes

Your API should always return clear status codes and messages.

Status CodeMeaning
200OK
201Created
400Bad Request
404Not Found
500Server Error

💡 Always return responses in JSON:

jsCopyEditres.status(404).json({ message: "User not found" });

📊 Visual Example of Full User Flow (Infographic)

1 Pjrjgw1Vj 1Mjhtqfzazra
3.5 Restful Api Design With Express — A Beginner’s Guide For Future Dev Legends 2

🎯 Recap — What Did We Learn?

✅ What RESTful APIs are
✅ How to use GET, POST, PUT, DELETE
✅ Working with query parameters & body
✅ Returning JSON responses
✅ Writing clean Express routes


🧠 Pro Tip!

🔁 Use tools like Postman or Thunder Client in VSCode to test your APIs easily!

📁 Also, separate your routes in different files for bigger projects.


📢 Follow for More Dev Tips & Tutorials!

If you found this guide helpful (and I hope you did 😄), don’t forget to subscribe and follow us:

▶️ YouTube: Web Codder
📸 Instagram: @web_codder_official
💬 Join WhatsApp: webcodder.dev/whatsapp

Let’s build the future of the web — one API at a time! 🌐💡

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts