Introduction

In this lesson, we'll dive into the practical aspects of using Mongoose with Express to create and view a todo item via an API. By the end of this lesson, you'll know how to define a Mongoose schema and model, create a sample todo item, and view it.

What You'll Learn

In this lesson you'll learn:

  • How to set up an Express server with Mongoose.
  • How to define a Mongoose schema and model with additional fields.
  • How to create a sample todo item.
  • How to view the created todo items via the API.
Step 1: Importing Libraries and Setting Up Express Server

We'll start by importing the required libraries, initializing our Express application, and setting up middleware to parse JSON request bodies.

Here, we:

  1. Import express and mongoose.
  2. Create an Express application instance.
  3. Use middleware to allow our Express application to understand and parse JSON bodies from incoming requests.
Step 2: Connecting to MongoDB

Next, we'll establish a connection to our MongoDB database.

This code:

  1. Connects to a MongoDB database named todo-app running locally.
  2. Logs a success message upon successful connection.
  3. Catches and logs any connection errors.
Step 3: Defining Mongoose Schema and Model

Now, let's create a schema and a model for our todo items.

Here we:

  1. Define a schema with two fields: task, which is a required string, and status, which is also a required string.
  2. Create a Todo model from the schema, which we will use to interact with the todos collection in MongoDB.

Understanding Automatically Added Fields: _id and __v:

Mongoose automatically adds some fields to each document, which you may notice in the API responses:

  • _id: Mongoose adds an _id field to each document, which uniquely identifies the document within the collection. This is similar to a primary key in a relational database and is essential for distinguishing individual documents.
  • __v: Mongoose also adds a __v field to track the version of the document. This is useful for handling concurrency and ensuring that updates to documents are applied correctly.

Now, let's move on to creating API routes to handle creating and viewing todo items.

Step 4: Creating a Sample Todo Item via API Route

To ensure our schema and model work correctly, let's create an API route to handle the creation of todo items.

In this snippet:

  1. We define a POST route /todos to create new todo items.
  2. Extract task and status from the request body.
  3. Create a new Todo document with the task description and status, defaulting to "In Progress" if no status is provided.
  4. Attempt to save the new document to the database.
  5. If successful, we send the created todo item back to the client with a 201 status code.
  6. If there's an error, we catch it and respond with a 400 status code and an error message.
Step 5: Viewing Todo Items via API Route

Next, let's create an API route to get all todo items.

In this snippet:

  1. We define a GET route /todos to retrieve all todo items from the database.
  2. Attempt to find all documents in the todos collection.
  3. If successful, we send the retrieved todo items back to the client with a 200 status code.
  4. If there's an error, we catch it and respond with a 400 status code and an error message.
Step 6: Starting the Express Server

Finally, we start the server and listen on the defined port.

This code starts the Express server and logs a message indicating that the server is running. To make the model available for import in other files, we need to export it.

Conclusion

In this lesson, we learned how to set up an Express server with Mongoose, define a schema and model with additional fields, create a sample todo item via an API route, and view the created todo items via another API route. By following these steps, you can effectively manage your data using Mongoose and expose functionality through a RESTful API.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal