MERN EP02: Developing the Back-End with MongoDB, NodeJS and Express

Gonzalo P.
Jul 23, 2017 · 4 min read

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):

Installing NodeJS and NPM

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 init

Write 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 --save

Next, we are going to create the folders of the app:

mkdir src
mkdir src/models
mkdir src/routes

Then, let’s create the files that we are going to use:

touch app.js
touch src/models/TodoList.js
touch src/routes/todoListRoutes.js

Building 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.js

The Routes

Now we are defining the routes that the Express server will be attending. a basic CRUD.

src/routes/todoListRoutes.js

The App

Finally, we define the application and set up all the configuration:

app.js

Starting 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 up

You 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.

NEXT

MERN EP03: Developing the Front-End with NodeJS, ReactJS and Bootstrap 3

PREVIOUS

MERN EP01: Setting up a development environment with Docker

Codebase

Software development obsession. Good practices, tips and interesting stuff about coding

Gonzalo P.

Written by

Codebase

Codebase

Software development obsession. Good practices, tips and interesting stuff about coding

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade