Skip to content

Todo List Endpoints

In order to make our REST API fully functional we will need the following endpoints which we will be available from our client React.js application:

  1. /todo POST - endpoint responsible for adding new todo to the database with title provided by user

  2. /todos GET - endpoint that will server all todos from the database

  3. /todo/:id DELETE - endpoint that will delete the specific todo from the database by id provided from the client application

  4. /todo/:id PUT - endpoint that will toggle is_done status of a todo item

Before creating these endpoints, let's first create mongoose Schema and Model for our todo item.

const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const port = 3001;

app.use(cors({origin: '*'}));

const mongoDBLink = 'mongodb+srv://test_user:Test12345@cluster0.axh68.mongodb.net/todo_list?retryWrites=true&w=majority';

mongoose.connect(mongoDBLink);

const TodoModel = mongoose.model('Todo', new  mongoose.Schema({ title: String, is_done: Boolean }));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
Since Mongo adds unique id to every new item by default, we do not need to define an id for our TodoModel. We will use one created by mongo itself _id, when needed.

Now, we have our mongo database connection setup, we have our Model and Schema create, it is time to start creating our endpoints.


Last update: March 28, 2022