Thuta Learning
ProjectsWeb Developmentbeginner

Mini Project: User API

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

In this mini project, we'll build a Simple User API with Express.js — viewing all users, finding one user, and adding a new user. There's no database yet, but you'll see the full basic flow of a REST API.

javascript
const express = require('express');

const app = express();
const port = 3000;

app.use(express.json());

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.get('/api/users/:id', (req, res) => {
  const id = Number(req.params.id);
  const user = users.find((item) => item.id === id);

  if (!user) {
    return res.status(404).json({ message: 'User not found' });
  }

  res.json({ data: user });
});

app.post('/api/users', (req, res) => {
  const name = req.body.name;

  if (!name || name.trim() === '') {
    return res.status(400).json({ message: 'Name is required' });
  }

  const newUser = { id: users.length + 1, name: name.trim() };
  users.push(newUser);

  res.status(201).json({ message: 'User created', data: newUser });
});

app.use((req, res) => {
  res.status(404).json({ message: 'Route not found' });
});

app.listen(port, () => {
  console.log(`Simple User API running at http://localhost:${port}`);
});

This project code brings together everything from previous lessons — server setup, middleware, GET routes, POST route, validation, and even a fallback 404 route. app.use((req,res)=>...) is placed at the very bottom so it only returns 404 once none of the routes above it match.

You should see
Run: node app.js Test endpoints: GET http://localhost:3000/api/users GET http://localhost:3000/api/users/1 POST http://localhost:3000/api/users

Info

Watch out for route order. If you put the 404 fallback at the top, the routes below it will never be reached.

What's Next

Once you understand this project, try writing PUT/PATCH and DELETE routes yourself to complete the full CRUD set.

Easy traps

  • Every time the server restarts, the in-memory users data resets to its original state. If you don't want data to disappear, the next step is connecting a database or file storage.

Exercise

You can keep extending this mini project later — connecting MongoDB/PostgreSQL, adding authentication, hooking up a frontend form, and adding search/filter/pagination.

You'll know it worked when:

Mini Project: User API | Thuta Learning