Make an Express API using MongoDB via Docker

This tutorial will show how to set up an ExpressJS API that connects to the MongoDB database hosted locally via Docker. ExpressJS is a framework that runs on top of NodeJS and simplifies many of the boilerplate code used when working the HTTP module of NodeJS, thus saving time and headaches. NodeJS is the runtime engine built using Chromium’s V8 JavaScript engine with additional APIs to interact with the operating system in ways browser Javascript does not.

MongoDB is a document store where items are stored using binary JSON in related collections. Docker is what we will use to host a container, an instance of a MongoDB image.

First, we will install the latest version of Node.js. You may confirm that Node and NPM are installed by typing in your terminal
node --version
and
npm --version

In addition, we need to install Docker, the Docker extension for VS Code, Postman, and the MongoDB for VS Code extension. The purpose of the extensions is to allow us to interface with Docker and Mongo.

Next, in the terminal, make a directory
mkdir your-app-name
Then in that directory, run
npm initand
keep pressing enter until you get to the “entry point,” and enterapp.js this changes your main script to app.js. Then keep pressing enter until you get to “Is this OK?” then type and enter yes.

After, in the terminal run
npm install express body-parser mongoose
This command installs the ExpressJS framework. It also installs Body Parser, “Node.js body parsing middleware.” We also get Mongoose, which “is a MongoDB object modeling tool designed to work in an asynchronous environment.”

Then enter in the
touch .gitignore
and type node_modules inside of it.

Enter
touch app.js in the terminal

In app.js, we must get the server up and running. First, we will require express and save the express function in the app variable. Then we will call the listen method of app. The first argument tells the app which port it will run on (3000 in our case). We will start up the server by entering
node app.js
in the terminal. Your console should have “Server started on port 3000” printed:

 




Next, we need to ensure that our app runs in the browser. We will do this by calling the get method. The first parameter will be a slash, indicating the main route, and the second parameter will be an anonymous function with a response as its second parameter. We will call the send method on the response with “Hello World!” as the argument. If you open up http://localhost:3000, you should see “Hello World!” in the browser.



Now, open up the Docker application. We want to get the latest image of MongoDB. To do so in your terminal type:
docker pull mongo:latest
Then you should see the mongo in the images panel:



To create the Mongo container enter
docker run -d -p 27017:27017 mongo
If it’s working, you’ll see something like this:



Then you should see mongo in the CONTAINERS panel:




After, go to your MongoDB extension and right-click where it says “localhost,” and click “Connect”:



If it’s connected, it should look like this:




In our app.js file, we will require mongoose and connect it to our port (as seen in lines 5 to 9 below):




Then, we will install nodemon by
running npm install -g
into the terminal, and it will be available globally. “nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.” Now, if you enter
nodemon app.js
you should see “connected to database” in the terminal.



After, we will create our Item model in the Item.js file. Type
touch Item.js
in the terminal and press return. Then we will require mongoose and create our Item model:




In app.js, we will create an item post route that will send back a message to the browser (lines 15 through 17 below):




To check if the post method is working correctly, go to Postman and select the “Post” request and type “http://localhost:3000/item” in the bar and click “Send,” and you should see “Connected to POST /item” as the output:




We will require “body-parser” as middleware, which sits between the endpoint and the function that processes the incoming requests. Then call the JSON method so we can receive JSON data from requests (lines 4 to 6 below):




Below we should see if they are correctly receiving request data. In the post request on the “/item” route, we will log the request body to the console (as seen on line 19):




Now, we will send the request JSON data from Postman. Click on the “Body” section, click on the “raw” radio button, then select the JSON format. In the body, we will create an Item in JSON format:




Once you click “Send”, your terminal should have the request body printed to the console:




After, we will require the Item model from Item.js (lines 10 and 11 below):



Next, in the post method, we will create a new item object from the request data, create a new Item object then save that data to MongoDB (lines 21–40):




To see if you persisted the item into MongoDB, go to the MongoDB extension, select the “Item” database, then your “items” collection, and you should see the document inside:



The document should look like this:




Next, we will get the items from the database and return them to the browser (lines 42 to 50):




Your browser should look like this:





Now you learned how to:

instantiate an ExpressJS application,
handle different types of requests on a per route basis
use the MongoDB and Docker VSCode extensions
set up a database with MongoDB and connect to it via Docker
set up a model using Mongoose
learn the purpose and how to use middleware in ExpressJS
utilize Postman for API testing

Resources:
Github Repository
Learning Microservices With Express.js & MongoDB
Getting started with APIs with ExpressJS and NodeJS

Credit Source: Jonathan Koss


Previous Post Next Post