Express Application with MongoDB
Build an Express Application that uses MongoDB and Docker
Updated: 03 September 2023
Built with tons of help from:
Setting up Mongo
# Adding Mongo to your PATH
If you have just downloaded and installed MongoDB, it may not be defined as a system variable in your PATH, you can do that finding the Mongo installation directory and adding this as a system environment variable, the directory should be like the following for Windows
This will give us access to both mongo
and monod
commands
# Create a Data Directory
Next we will need a place to store our data, we can create this directory anywhere we want, in this case I’ll make it inside of my app directory
# Running the DB Server
Next we can run our database server with the following command inside of our mongo
directory that we just created
If we see an output with
We know that the server is running
# Creating and Viewing Elements
In a new terminal window we open the Mongo Shell with
# Connect to a Database
Next we need to connect to our database to access data, we can do this from the Mongo Shell with
If successful we will see the output
# Insert Data
Next we can try to insert an element with the following
Mongo uses BSON (basically JSON with some sprinkles) for data storage
We can also insert data as follows
# View Data
We can view our inserted data with
Which will output the following
Building the Express App
This can all be found in the server.js
file
The Express app will do a few things:
- Serve the necessary static files for the app
- Get and Insert data into Mongo
- Build and send the Mongo content to the frontend
# Importing the Necessary Libraries
I’m making use of the following libraries to
- Read environmental variables
- Create my server
- Parse JSON body from the create form
# Configure the Database
I’m using monk to easily interface with our database
I’ve used the default value of mongo:27017
for when the application is run in k8s in order to interface with a mongo instance more easily. If running locally we can set the MONGO_ENDPOINT
in a .env
file in the project root directory as follows
# Middleware
Configure express middleware for the following
- Use static files
- Parse JSON and Form data from request
- Make DB accessible to the app
# View Comments
Next I define a /comments
that will retrieve content from the comments
collection, render it with the base.card
and base.content
functions and send that as a response
# Creeate Comment
To create a comment I’ve used a simple form in the frontend, which can be seen below
And an express route to handle the post and insert the new comment to the database
Deploy on k8s
Once we are done we can push this as a Docker image and deploy it on a Kubernetes Cluster as follows
# Building the Image
From the application directory run
# Deploying on Kubernetes
Once logged into a kubernetes cluster we can make use of the express.yaml
to deploy the express app, and the mongo.yaml
file to deploy Mongo
This will create a deployment as well as a service for both the Express App and Mongo. The deployment configs are as follows
express.yaml
mongo.yaml
Running Locally
If you’d like to run this application on a local Kubernetes cluster, take a look at the page on Deploying an Express App that Uses Mongo on k8s Locally