Thuta Learning
ProjectsWeb Developmentintermediate

Mini Project — Part 2: Adding CRUD Features

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

What you'll walk away with

  • Put Mini Project — Part 2: Adding CRUD Features to work in a real project
  • Write the code yourself and run it
  • Build out the whole project step by step

Let's think this through for a second

Since Part 1 can already read the users list, this part is about referencing the Create User, Update User, and Delete User lessons to add POST/PUT/DELETE requests. The auth header pattern is already solid from Part 1, so nothing changes there — what changes is the HTTP method and the request body. For Create/Update, you'll need a Content-Type: application/json header and a body built with JSON.stringify(); for Delete, no body is needed — the :id in the URL does the work. Once you've written these functions one by one, you'll wire them up to the CLI's command line arguments (process.argv) so a single command can manage user data.

Let's actually build it

In api.js, write createUser(name, email) as POST /v1/users, updateUser(id, data) as PUT /v1/users/:id, and deleteUser(id) as DELETE /v1/users/:id. Display the returned user object nicely with console.table(). Create a cli.js and parse process.argv so commands like node cli.js create "Alice" alice@example.com, node cli.js update u_1 email=alice2@example.com, and node cli.js delete u_1 all work. Run through Create → Read (getUsers) → Delete in order to confirm the data flow.

Code Example

javascript
// api.js (ဆက်ထည့်)
async function createUser(name, email) {
  const res = await fetch(`${API_BASE_URL}/users`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${API_TOKEN}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ name, email }),
  });
  if (!res.ok) throw new Error(`Create failed: ${res.status}`);
  return res.json();
}

async function deleteUser(id) {
  const res = await fetch(`${API_BASE_URL}/users/${id}`, {
    method: 'DELETE',
    headers: { Authorization: `Bearer ${API_TOKEN}` },
  });
  return res.status; // 204 No Content ဆိုရင် success
}

// cli.js
const [, , cmd, ...args] = process.argv;
if (cmd === 'create') createUser(args[0], args[1]).then(console.log);
if (cmd === 'delete') deleteUser(args[0]).then((s) => console.log('status:', s));
You should see
Running node cli.js create "Alice" alice@example.com returns a new user object with an auto-generated id, and running delete returns a 204 status.

5-Minute Try-It

In 5 minutes: create a user, then try running the update command with the returned id to see if you can change the email.

A Quick Word of Caution

Delete operations can't be undone, so don't test with a production token — only try out the CRUD flow in a test/sandbox environment.

Easy traps

  • Forgetting the Content-Type: application/json header on POST/PUT requests, so the server can't parse the body
  • Using an id that doesn't exist for Delete/Update and crashing because the 404 error isn't caught

Now Try It Yourself

In 5 minutes: create a user, then try running the update command with the returned id to see if you can change the email.

You'll know it worked when: Running node cli.js create "Alice" alice@example.com returns a new user object with an auto-generated id, and running delete returns a 204 status.

Mini Project — Part 2: Adding CRUD Features | Thuta Learning