
MERN EP02: Developing the Back-End with MongoDB, NodeJS and Express
Now that we got our Docker Environment set, we are going to create our backend application that will work as a Middleware between the React JS app and the MongoDB database.
First, let’s see the folder tree of the project:
Installing NodeJS & NPM
In this post we are going to start using NodeJS and NPM. So, please read the following post for installing it (it’s very simple):
Now we can continue :)
Initializing the project
We are currently working with the backend, so let’s go to the “node-backend” folder and initialize a Node JS project in there:
cd node-backend
npm initWrite all the information requested or just press enter until the confirmation. You should be seeing something like this:

Now, let’s install Express (Web server package for NodeJS), Mongoose (MongoDB plugin for NodeJS) and body-parser (body parsing) and cors (Cross origin requests):
sudo npm install express --save
sudo npm install mongoose --save
sudo npm install body-parser --save
sudo npm install cors --saveNext, we are going to create the folders of the app:
mkdir src
mkdir src/models
mkdir src/routesThen, let’s create the files that we are going to use:
touch app.js
touch src/models/TodoList.js
touch src/routes/todoListRoutes.jsBuilding the service
Let’s build the service! fill the files created before with the following content:
The Model
Here we are using Mongoose (MongoDB plugin) to define the model of the item with we will work in the Backend. Just a single valued item (desc) of type string.
src/models/TodoList.jsThe Routes
Now we are defining the routes that the Express server will be attending. a basic CRUD.
src/routes/todoListRoutes.jsThe App
Finally, we define the application and set up all the configuration:
- Will launch with the base path “/Todo”
- Using port 6200
- Connection string to Mongodb (The service name of the Docker Container works as a domain in the containers network, so the path to the container with MongoDB running is “mongodb”. This was configured in the last post, on the “docker-compose.yml” file)
- Using url encoding and json as response
app.jsStarting the service
The last part to making the service work, is to configure the command that will launch the server. In the package.json file, add the following configuration:
"start": "nodemon app.js"The file should look like this:
Now, you can start the server and check the endpoints. Execute the following command in the terminal (remember that we were in the “node-backend” folder, so move back to the previous directory first):
cd ../
sudo docker-compose upYou should be seeing something like this:

The container “backend_1” will send the message “Backend Started”, this is the signal that all is working. Now enter to this URL:
http://localhost:6200/todo
This is the endpoint that lists all the task items in the database. Currently is empty, so it’s showing an empty array. It works!
A little test
You can install Postman to test the backend (Link here). Let’s insert a new task:

And now, let’s check again the list of task (but now with postman).

Congrats! it’s working now!
Check the full code of this app on the following GitHub repository:
https://github.com/gonzalompp/todolist-reactjs-example
Now let’s wait for the front-end part :)
Happy Coding!
Gonzalo.

