Build a Todo CRUD API Using Express.js
Aug 12,2026

After learning the basics of Express.js, the next step is to build a real application.

One of the best beginner projects is a Todo CRUD API.

In this project, we'll build a REST API using Node.js and Express.js.

Our API will support the following operations:

  • Create a Todo
  • Get All Todos
  • Get Todo by ID
  • Update a Todo
  • Delete a Todo

We'll keep the data in memory for now instead of using a database.

By the end of this article, you'll understand how Express.js simplifies routing, request body handling, HTTP methods, parameters, and JSON responses.

Let's get started!


Project Structure

Create the following project structure:

todo-api/

│── server.js
│── package.json

Initialize the project:

npm init -y

Now install Express.js:

npm install express

Unlike the previous version using Node.js's built-in HTTP module, Express.js will handle many of the common HTTP server tasks for us.


Step 1: Import Express.js

Open server.js.

const express = require("express");

Create an Express application:

const app = express();

We'll also need middleware to parse JSON request bodies:

app.use(express.json());

So the initial application looks like this:

const express = require("express");

const app = express();

app.use(express.json());

Step 2: Create Sample Data

Since we're not using a database yet, let's store todos in memory.

let todos = [
  {
    id: 1,
    title: "Learn Node.js",
    completed: false,
  },
  {
    id: 2,
    title: "Learn Express.js",
    completed: true,
  },
];

Later, we can replace this array with MongoDB or another database.


Step 3: Create the Express Server

Now create the server.

const express = require("express");

const app = express();

app.use(express.json());

let todos = [
  {
    id: 1,
    title: "Learn Node.js",
    completed: false,
  },
  {
    id: 2,
    title: "Learn Express.js",
    completed: true,
  },
];

app.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Run the application:

node server.js

You should see:

Server running at http://localhost:3000

Unlike the Node.js HTTP module, Express gives us methods such as app.get(), app.post(), app.put(), and app.delete() for defining routes.


Step 4: Get All Todos

Let's create our first API.

The endpoint will be:

GET /todos

Add:

app.get("/todos", (req, res) => {
  res.json(todos);
});

That's it.

Express automatically handles converting the JavaScript array into a JSON response.

Visit:

http://localhost:3000/todos

Output:

[
  {
    "id": 1,
    "title": "Learn Node.js",
    "completed": false
  },
  {
    "id": 2,
    "title": "Learn Express.js",
    "completed": true
  }
]

Notice how much simpler this is compared with manually using:

res.end(JSON.stringify(todos));

Express's res.json() handles that for us.


Step 5: Get Todo by ID

Now let's create an endpoint to get a specific Todo.

The endpoint will be:

GET /todos/:id

Here, :id is a route parameter.

For example:

GET /todos/1

Create the route:

app.get("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const todo = todos.find((todo) => todo.id === id);

  if (!todo) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  res.json(todo);
});

Let's understand this step by step.

Get the ID

const id = Number(req.params.id);

If the request is:

GET /todos/2

then:

req.params.id

contains:

"2"

We convert it to a number using:

Number()

Find the Todo

const todo = todos.find((todo) => todo.id === id);

If the Todo doesn't exist:

if (!todo) {
  return res.status(404).json({
    message: "Todo Not Found",
  });
}

Otherwise:

res.json(todo);

returns the Todo.

Visit:

GET /todos/1

Output:

{
  "id": 1,
  "title": "Learn Node.js",
  "completed": false
}

Step 6: Create Todo

Now let's create a new Todo.

The endpoint will be:

POST /todos

The client will send data in the request body:

{
  "title": "Learn Express Middleware"
}

Create the route:

app.post("/todos", (req, res) => {
  const { title } = req.body;

  const todo = {
    id: Date.now(),
    title,
    completed: false,
  };

  todos.push(todo);

  res.status(201).json(todo);
});

Let's understand what happens here.

First, we get the title:

const { title } = req.body;

Because we already added:

app.use(express.json());

Express automatically parses the JSON request body.

We then create a new Todo:

const todo = {
  id: Date.now(),
  title,
  completed: false,
};

Add it to the array:

todos.push(todo);

Finally, return the created Todo:

res.status(201).json(todo);

We use HTTP status code 201 because a new resource was created.


Step 7: Update Todo

Now let's update an existing Todo.

The endpoint will be:

PUT /todos/:id

Example request:

PUT /todos/1

Body:

{
  "title": "Master Node.js",
  "completed": true
}

Implementation:

app.put("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const todo = todos.find((todo) => todo.id === id);

  if (!todo) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  const { title, completed } = req.body;

  todo.title = title;
  todo.completed = completed;

  res.json(todo);
});

First, we get the ID:

const id = Number(req.params.id);

Then find the Todo:

const todo = todos.find((todo) => todo.id === id);

If it doesn't exist, return:

404

Otherwise, update the fields:

todo.title = title;
todo.completed = completed;

Response:

{
  "id": 1,
  "title": "Master Node.js",
  "completed": true
}

Step 8: Delete Todo

Now let's implement the DELETE operation.

The endpoint will be:

DELETE /todos/:id

Implementation:

app.delete("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const index = todos.findIndex(
    (todo) => todo.id === id
  );

  if (index === -1) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  todos.splice(index, 1);

  res.json({
    message: "Todo Deleted Successfully",
  });
});

First, find the Todo index:

const index = todos.findIndex(
  (todo) => todo.id === id
);

If the Todo doesn't exist:

if (index === -1) {
  return res.status(404).json({
    message: "Todo Not Found",
  });
}

Otherwise, remove it:

todos.splice(index, 1);

Response:

{
  "message": "Todo Deleted Successfully"
}

Step 9: Handle Unknown Routes

What happens if someone requests:

GET /products

but our API doesn't have a /products route?

We should return a 404 response.

Express makes this simple with a final middleware:

app.use((req, res) => {
  res.status(404).json({
    message: "Route Not Found",
  });
});

This should be placed after all your API routes.


Step 10: Complete Application

Now let's put everything together.

const express = require("express");

const app = express();

app.use(express.json());

let todos = [
  {
    id: 1,
    title: "Learn Node.js",
    completed: false,
  },
  {
    id: 2,
    title: "Learn Express.js",
    completed: true,
  },
];

// GET ALL TODOS
app.get("/todos", (req, res) => {
  res.json(todos);
});

// GET TODO BY ID
app.get("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const todo = todos.find(
    (todo) => todo.id === id
  );

  if (!todo) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  res.json(todo);
});

// CREATE TODO
app.post("/todos", (req, res) => {
  const { title } = req.body;

  const todo = {
    id: Date.now(),
    title,
    completed: false,
  };

  todos.push(todo);

  res.status(201).json(todo);
});

// UPDATE TODO
app.put("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const todo = todos.find(
    (todo) => todo.id === id
  );

  if (!todo) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  const { title, completed } = req.body;

  todo.title = title;
  todo.completed = completed;

  res.json(todo);
});

// DELETE TODO
app.delete("/todos/:id", (req, res) => {
  const id = Number(req.params.id);

  const index = todos.findIndex(
    (todo) => todo.id === id
  );

  if (index === -1) {
    return res.status(404).json({
      message: "Todo Not Found",
    });
  }

  todos.splice(index, 1);

  res.json({
    message: "Todo Deleted Successfully",
  });
});

// UNKNOWN ROUTE
app.use((req, res) => {
  res.status(404).json({
    message: "Route Not Found",
  });
});

app.listen(3000, () => {
  console.log(
    "Server running on http://localhost:3000"
  );
});

Testing the API

You can test the API using:

  • Postman
  • Thunder Client
  • Insomnia
  • cURL

Let's test each endpoint.


GET All Todos

GET /todos

Response:

[
  {
    "id": 1,
    "title": "Learn Node.js",
    "completed": false
  },
  {
    "id": 2,
    "title": "Learn Express.js",
    "completed": true
  }
]

GET Todo by ID

GET /todos/1

Response:

{
  "id": 1,
  "title": "Learn Node.js",
  "completed": false
}

If the Todo doesn't exist:

{
  "message": "Todo Not Found"
}

with status:

404 Not Found

Create Todo

POST /todos

Body:

{
  "title": "Learn Express Middleware"
}

Response:

{
  "id": 1723456789012,
  "title": "Learn Express Middleware",
  "completed": false
}

Status:

201 Created

Update Todo

PUT /todos/1

Body:

{
  "title": "Master Express.js",
  "completed": true
}

Response:

{
  "id": 1,
  "title": "Master Express.js",
  "completed": true
}

Delete Todo

DELETE /todos/1

Response:

{
  "message": "Todo Deleted Successfully"
}

How This API Works

The request flow is now much simpler because Express handles many low-level HTTP details for us.

Client Request
      │
      ▼
Express Application
      │
      ▼
Match HTTP Method
      │
      ▼
Match Route
      │
      ▼
Route Handler
      │
      ▼
Perform CRUD Operation
      │
      ▼
Return JSON Response

For example:

POST /todos
      │
      ▼
app.post("/todos", ...)
      │
      ▼
Read req.body
      │
      ▼
Create Todo
      │
      ▼
res.status(201).json(todo)

This is the basic architecture behind many REST APIs.


What Express.js Simplified

If you've already built the same API using Node.js's built-in HTTP module, the difference becomes clear.

With the HTTP module, you have to manually deal with things such as:

req.method
req.url
JSON.stringify()
res.end()
res.setHeader()
req.on("data")
req.on("end")

With Express.js, the same concepts become much simpler:

app.get()
app.post()
app.put()
app.delete()

req.params
req.body

res.json()
res.status()

For example, reading a request body with the HTTP module requires manually collecting chunks.

With Express:

app.use(express.json());

Then:

req.body

is available directly.

This is one of the main reasons Express.js is popular for building Node.js APIs.


Best Practices

When building Express.js APIs:

Use meaningful routes

Prefer:

GET /todos
GET /todos/:id
POST /todos
PUT /todos/:id
DELETE /todos/:id

instead of routes such as:

GET /getAllTodos
POST /createTodo

Use appropriate HTTP methods

Use:

GET

for retrieving data.

POST

for creating resources.

PUT

for replacing/updating a resource.

PATCH

for partial updates.

DELETE

for deleting resources.


Return appropriate status codes

For example:

200 → Successful request
201 → Resource created
400 → Bad request
404 → Resource not found
500 → Server error

Validate request data

Our example doesn't validate the title field yet.

For example, a client could send:

{}

or:

{
  "title": ""
}

A production API should validate incoming data before creating or updating resources.


Don't store production data in memory

Our example uses:

let todos = [];

This is useful for learning, but the data disappears whenever the server restarts.

A real application should use a database such as MongoDB, PostgreSQL, MySQL, or another suitable database.


Common Beginner Mistakes

Here are some common mistakes when building Express.js APIs.

1. Forgetting express.json()

If you want to read JSON request bodies:

app.use(express.json());

Without it:

req.body

may not contain the parsed JSON data you expect.


2. Not using return after an error response

For example:

if (!todo) {
  return res.status(404).json({
    message: "Todo Not Found",
  });
}

Using return prevents the handler from continuing and potentially sending another response.


3. Forgetting route parameters

For:

GET /todos/1

the route should be:

app.get("/todos/:id", ...)

and the ID can be accessed through:

req.params.id

4. Not checking whether a resource exists

Before updating or deleting a Todo, always check whether it exists.

if (!todo) {
  return res.status(404).json({
    message: "Todo Not Found",
  });
}

What's Next?

This Todo API is intentionally simple.

The next step would be to make it more production-ready by adding:

  • Express Router
  • Controllers
  • Services
  • Data validation
  • Error-handling middleware
  • MongoDB
  • Mongoose
  • Authentication
  • JWT
  • Environment variables
  • Logging
  • API documentation

For example, instead of keeping everything in server.js, we could eventually structure the project like this:

todo-api/

├── controllers/
│   └── todo.controller.js
│
├── routes/
│   └── todo.routes.js
│
├── services/
│   └── todo.service.js
│
├── models/
│   └── todo.model.js
│
├── middleware/
│   └── error.middleware.js
│
├── server.js
└── package.json

That's where Express.js becomes much more useful for building larger applications.


Final Thoughts

Building a Todo CRUD API is a great way to understand Express.js because it brings several concepts together.

In this article, we learned how to:

  • Create an Express.js server
  • Install and configure Express.js
  • Create GET routes
  • Use route parameters
  • Read request bodies using req.body
  • Create Todo resources
  • Retrieve Todos
  • Update Todos
  • Delete Todos
  • Return JSON responses
  • Use HTTP status codes
  • Handle unknown routes

The biggest difference between building this API with Node.js's HTTP module and Express.js is the amount of low-level work we need to handle ourselves.

Express.js provides a cleaner API:

app.get()
app.post()
app.put()
app.delete()

along with:

req.params
req.body
res.json()
res.status()

This allows us to focus more on what our API should do rather than manually handling the underlying HTTP details.

Once you're comfortable with this CRUD API, the natural next step is to move the routes into express.Router(), separate controllers and services, add validation, and connect the API to a real database.

Happy Coding! 🚀