MERN (MongoDB, Express, React, and Node.js) Stack Tutorial 2023

Introduction

Over the last decade, website development has significantly evolved. We now have access to new tech stacks with modern application architecture. One of them is a MERN stack.

MERN stands for MongoDB, Express, React, and Node.js.

  • MongoDB is a NoSQL database.
  • Express is a Node.js framework to develop the backend of websites.
  • React is a JavaScript library to build a front-end user interface.
  • Node.js enables us to execute JavaScript code on the server.

Since its inception, the MERN stack has ignited excitement among the developer community. That’s why many existing companies and new startups are adopting it for their projects.

In turn, it creates new opportunities for developers. I would recommend using the Haystack app to find those opportunities. Basically, it connects employers with job seekers, especially the techies.

For example, let’s say you’re looking for a React App development job. Simply create an account with them for free and the app will intelligently match you with relevant job opportunities.

As now you’re a bit familiar with its scope and earning potential. Let’s create a simple web app using MERN stack.

Create the Backend

At first, create a root folder that will hold the project files.

Shell

Now, move inside that folder.

Shell

Initialize npm to automatically generate a package.json file.

Shell

At this point, we need to install some dependencies for the backend.

Shell

I would even recommend you to install nodemon to automatically restart the server whenever the source code is modified.

Shell

Let’s create an index.js file in the mern_stack_app folder and start writing some code. Basically, this code-snippet is used to setup a Node.js/Express server.

JavaScript

Download and Install MongoDB

  1. At first, go to https://www.mongodb.com/download-center/community?jmp=docs. Then, select the desired version and your OS. Finally, select the MSI file.
  2. Click the download button.
  3. When the .msi file is downloaded, simply run it.
  4. Follow the step by step wizard to complete the installation.
  5. Now you need to create a folder on your computer to store the database. Mostly, on the local machine, we use the C drive. So, let’s create a new folder in the C drive and name it “data”. Move inside that folder and create a sub-folder. This time name it “db”. This is where the actual database will be stored.

Run MongoDB

  1. Go to “C:\Program Files\MongoDB\Server\4.2\bin\”
  2. Open a terminal and type the following command.
Shell

MongoDB will start on PORT 27017.

To start the mongo shell. Open another terminal (Do not close the terminal where mongod is running).

Again go to “C:Program FilesMongoDBServer4.2bin\” and type the following command.

Shell

This command will run the mongo shell.

Inserting data

Let’s create a database and insert data in it.

Type the following command to create a database.

Shell

Let’s insert a few documents in it.

JSON

We created a collection named “details”, and inserted five documents in it. Let’s check.

JSON

Connecting to MongoDB using Mongoose

Earlier, we installed mongoose. Let’s use mongoose to connect the server to MongoDB.

First, we need to import mongoose in our file.

Shell

Then, connect mongoose to the database using the connect() function.

JavaScript

At last, we use the following function to tell us that the connection was successful.

JavaScript

This is how the index.js file looks now.

JavaScript

Creating a schema

Next step is to create a schema.

In the same folder where index.js file is, create a new file “model.js” add the following code in it.

JavaScript

The model is ready and we need to import it in the index.js file. Add the following code in index.js file to do so.

JavaScript

Implementing endpoints

The server is ready and connection with MongoDB is established. Now we will implement the endpoint. As we discussed in the previous part, we will fetch the data from MongoDB. To fetch data, we have to use GET. Let’s create a route handler for it.

But first, we need a router for it. Add the following line before the connection part.

JavaScript

And add this line above app.listen() function.

JavaScript

Now the router part is complete. Let’s create the handler.

JavaScript

In this route handler, we used the find() method to retrieve all the documents from the details collection. The endpoint here is “/getData”. To check the endpoint, we will use the postman tool. You can download this tool from www.getpostman.com.

Design the Frontend

The backend is ready, let’s build the frontend.

Understanding the frontend

So before we start writing code for the frontend part, we need to understand, what we are going to build. Our frontend part will contain a button. When clicked on this button, data the details collection will de display below it. We will display the name and age from every document of the collection.

Creating the react application.

Go to the directory that contains the backend folder. In the same directory, run the following command in a terminal.

Shell

Here, “frontend” is the name of the folder that contains the default react project template and the dependencies are also installed with it. Go to this folder and type the following command to run the react application.

Shell

Adding bootstrap

Well, we are not building an application with many stylings, but still, we will use bootstrap in it. To install the bootstrap, use the following command.

Shell

In the frontend folder, open the src folder. Then, open the App.js file. We need to import bootstrap here.

JavaScript

Moreover, remove all the default code present in the App class.

Adding component

We will have one component in our application, that will contain the button. In the src folder, create a new directory and name it “components”. Go to this folder and create a new file. Name it “details”.

Add the following code in it.

JavaScript

Now, open the app.js file and import this component in it. And then use this component in the App function. This is how the app.js file will look.

JavaScript

Connect Backend With Frontend

Frontend and backend are ready. Let’s connect them.

Installing and using Axios

To connect both of them, we need the Axios library. Run the following command to install the Axios.

Shell

Then, import Axios in the details.js file.

JavaScript

Completing the frontend

We will use the Axios to fetch data from the backend. We will use axios.get method. But first, we need to define a state in the constructor.

JavaScript

data: This will hold the details fetched from the backend. buttonClicked: This will be used to display data when the button is clicked.

This is axios is used.

JavaScript

We used the same endpoint that we defined while creating the backend. When we get the response from the backend, we have to store the data in a state. We will store the data in this.state.data.

To make this work properly, we will add this code in a lifecycle method known as componentDidMount(). This method executes before the first rendering.

JavaScript

Display data

Now, we have all the data. We just need to display it. Remember, the data returned is an array of objects. We have to iterate through it. We will display the data in the following way.

JavaScript

Observe the code. We will iterate through this.state.data using the map method. Now we have to put this inside a condition. This code will only get executed when the button is clicked. And for this, we have this.state.buttonClicked.

JavaScript

This function will be executed when the button is clicked. It will change the value of this.state.buttonClicked to true and then data will get displayed on the screen.

Let’s add everything to the details.js file.

JavaScript

This is how details.js file looks now.

Run the application

The frontend is ready, the backend is ready, and both are connected. Let’s see how it looks in the browser.

Let’s click on the button.

Everything works great!

Conclusion

So we created a simple web application using the MERN stack. First, we created the backend using Node.js, mongoose, and MongoDB. Then, we created the frontend using React. Finally, we connected both of them using the Axios library.

1 thought on “MERN (MongoDB, Express, React, and Node.js) Stack Tutorial 2023”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.