Thuta Learning
ProjectsWeb Developmentbeginner

GET All Items

Relax. We'll talk through this in plain words — no textbook voice.

One of the most commonly used API endpoints is the GET all route, which returns the entire data list — for example, all users, all products, or all posts.

javascript
const express = require('express');
const app = express();

let users = [
  { id: 1, name: 'Aung Aung' },
  { id: 2, name: 'Mya Mya' }
];

app.get('/api/users', (req, res) => {
  res.json({
    count: users.length,
    data: users
  });
});

app.listen(3000, () => console.log('API running on port 3000'));

app.get('/api/users') is called by the client, it returns the users array as JSON. Including count makes it easy for the frontend to know the total number of items.

You should see
GET /api/users { "count": 2, "data": [ { "id": 1, "name": "Aung Aung" }, { "id": 2, "name": "Mya Mya" } ] }

Info

You could return just an array as the API response too. But using an object structure like { count, data } makes it easy to add things like pagination, message, or status later on.

Easy traps

  • You can return an object with res.send() too, but for API data, using res.json() is clearer.

Exercise

This GET all pattern is used to show a users list on an admin dashboard, a products list on a shop page, and a posts list on a blog page.

You'll know it worked when:

GET All Items | Thuta Learning