DEV Community

Abdullah Jawed
Abdullah Jawed

Posted on

Express API's

Let’s Make Express APIs Easy (GET, POST, PUT, DELETE Explained)

If you’re learning backend development and feeling confused about APIs, trust me you’re not alone.
When I first started with Express, even simple words like routes, endpoints, methods felt scary.

But once you understand four simple methods, everything becomes clear:

👉 GET
👉 POST
👉 PUT
👉 DELETE

That’s literally all you need to build a basic backend.

So let’s break it down in the simplest, rawest way possible.

What Even Is an API?

Think of an API like a waiter in a restaurant.

  1. You (the client) ask something →
  2. Waiter (API) takes your request →
  3. Kitchen (server) sends the food back.

Express just makes it easy to be that “waiter.”

1. GET — “Give me the data”
GET is the easiest.
When the frontend wants something, it uses GET.

Example:


app.get(”/users”, (req, res) => {
    let alluser = readDB()
    res.status(200).json(alluser);
});

Enter fullscreen mode Exit fullscreen mode

You’re basically saying:

“Hey server, give me all the users.”

No body, no extra stuff — just fetch data.

Real use cases:

  • Show all products
  • Get user profile
  • Display a dashboard

2. POST — “I want to create something”
POST is used when you’re sending data to the backend.

Example:

  const { name, email } = req.body;
  if (!name || !email) {
    return res.status(400).json({ success: false, message: “name and email required” });
  }
  const id = users.length ? users[users.length - 1].id + 1 : 1;
  const newUser = { id, name, email };
  users.push(newUser);
  res.status(201).json({ success: true, data: newUser });
});Frontend sends something like:
{
  “name”: “Abdullah”,
  “email”: “abdullah@gmail.com”
}
Enter fullscreen mode Exit fullscreen mode

And backend stores it.

Real use cases:

  • Register a user
  • Submit a form
  • Add a blog post

3. PUT — “Replace the whole thing”
PUT is for updating — but remember this:

👉 PUT replaces the entire object, not just one part.

Example:


router.put(”/users/:id”, (req, res) => {
  const id = Number(req.params.id);
  const { name, email } = req.body;
  const idx = users.findIndex(u => u.id === id);
  if (idx === -1) return res.status(404).json({ success: false, message: “User not found” });

  // replace object (PUT semantics)
  users[idx] = { id, name: name ?? users[idx].name, email: email ?? users[idx].email };
  res.json({ success: true, data: users[idx] });
});
Enter fullscreen mode Exit fullscreen mode

If you update a user using PUT, you’re basically saying:

“Here is the full updated user object. Replace everything with this.”

Real use cases:

  • Edit full profile
  • Update product details
  • Replace blog post content

4. DELETE — “Remove this immediately”

DELETE does exactly what the name says.

Example:


router.delete(”/users/:id”, (req, res) => {
  const id = Number(req.params.id);
  const idx = users.findIndex(u => u.id === id);
  if (idx === -1) return res.status(404).json({ success: false, message: “User not found” });

  const removed = users.splice(idx, 1)[0];
  res.json({ success: true, data: removed });
});
Enter fullscreen mode Exit fullscreen mode

Use it when you want to remove something:

  • Delete an account
  • Remove a product
  • Delete a comment
  • Remove a file

Top comments (0)