Build REST APIs using Express and MongoDB

Spandan Pyakurel
3 min readSep 8, 2020

Today I am going to build a simple API using express and MongoDB. To show the simple use-case of docker, I have dockerized the application as well. Let's take todo, a simple application for the demonstration. In todo, APIs needed are as follows:

1. GET api/todos
2. POST api/todos
3. GET api/todos/:id
4. PATCH api/todos/:id
5. DELETE api/todos/:id

Let’s start coding by creating a new application. Create a new directory todo. Inside the directory, run the command npm init to initialize the app. You can see package.json created inside your directory.

Now, let's install dependencies for our app. The dependencies required are
1. Express: https://expressjs.com/
2. Body- Parser: https://www.npmjs.com/package/body-parser
3. Mongoose: https://mongoosejs.com/
To install dependencies run the command npm install express body-parser mongoose

Now that we have installed the required dependencies, we deep dive into the code. Let’s get started with a schema for the todo.
We use mongoose to define our schema and model. Let us look at the defined entity for Todo

Todo entity with attributes

Now all you have to do is map the entity defined with schema in mongoose. Here I assumed freshly created Todo to be just started, and hence I gave a default value of completed to be false.

Now let’s get started with index.js to start our server. First, we try to connect to MongoDB. In this tutorial, I have started MongoDB inside docker(later seen in docker-compose). If someone wishes to use the local environment, the application works fine. We then start our server using express in the port 8080. For easy access to data, body-parser, middleware is used to parse the request body as JSON. Our API is redirected to routes, where we define callback functions for our API requests.

Now let's define routes used by the API. We use the router defined from express. It provides the functions {get, post, patch, put, delete} to define our routes. In our get /todos , we simply fetch all the todos available in our database collection. This is easily done using the mongoose model we defined earlier. Similarly, for post /todos , we use the request body to create Todo and simply save the Todo. In this way, other APIs can also be defined.

Now let's write dockerfile to build the application. We use a node image of version 10 to build our application. Then we create an app directory to hold the application code inside the image. Next, we copy the package.json file. Then we run npm install to install dependencies inside the container. We then copy our app’s source code to the docker image. Since our app is using port 8080, we expose the port. In the end, we set command node index.js to run our application.

We now write docker compose.yml to provide MongoDB environment. Since our API is running at port 8080 inside the docker network, we map an external port 8080 to the internal docker network port 8080. Also, we define a container for mongo using image mongo.

Now to run the application, run the command docker-compose up
Make an API request to see if it’s working. I have put together the application in my Github repo. Feel free to access it.

--

--