Let's now look at a practical real-world use case.
We have already set up a basic Node.js application called test-app.
Inside test-app, there is a file called server.js which has a basic backend setup with Express and a Mongoose client.
We have created two routes:
GET /get-users — to fetch all users data from our database.POST /add-user — to add a new user to our database.The server is running on port 5050, and this is our basic server setup.
The UI for our test application looks like a basic university website where any student or user can create their account.
Our Node.js application needs to connect with a MongoDB database (college-db).
However, we do not have MongoDB installed on our local system.
Instead, we are going to use MongoDB through Docker containers, and our application will interact with those containers to store and fetch data.
For this setup, we are going to make use of two Docker images:
| Docker Image | Purpose |
|---|---|
mongo | MongoDB database running inside a Docker container |
mongo-express | Web-based UI for MongoDB that allows us to view and manage database data through the browser |
+----------------------+
| Node.js App |
| (test-app) |
| Port: 5050 |
+----------+-----------+
|
|
v
+----------------------+
| MongoDB Container |
| (mongo) |
| Database: |
| college-db |
+----------+-----------+
|
|
v
+----------------------+
| Mongo Express UI |
| (mongo-express) |
| Browser Interface |
+----------------------+college-db database.mongo-express UI in the browser.| Component | Description |
|---|---|
| Node.js Application | Handles API requests and business logic |
| Express | Creates backend routes and server |
| Mongoose | Connects Node.js with MongoDB |
MongoDB Container (mongo) | Stores application data |
Mongo Express (mongo-express) | Provides a graphical interface for MongoDB |
| Docker | Runs MongoDB and Mongo Express inside isolated containers |
GET /get-usersUsed to retrieve all users from the database.
POST /add-userUsed to insert a new user into the database.
Instead of installing MongoDB directly on our machine:
This makes Docker an excellent choice for developing and deploying applications that depend on databases such as MongoDB.