Open In App

MERN Stack

Web development refers to the creating, building, and maintaining of websites. It includes aspects such as web design, web publishing, web programming, and database management. One of the most famous stack that is used for Web Development is MERN stack. This stack provides an end-to-end framework for the developers to work in and each of these technologies play a big part in the development of web applications.



What is MERN Stack?

MERN Stack is a JavaScript Stack that is used for easier and faster deployment of full-stack web applications. MERN Stack comprises of 4 technologies namely: MongoDB, Express, React and Node.js. It is designed to make the development process smoother and easier.



How MERN stack works?

When working with MERN stack, developers create implement View layer using React and Express and Node are used to implement application layer of website then MongoDB is used to implement database layer

How does MERN stack work?

Roadmap to become a MERN Stack Developer

Step 1: Learn basics of HTML, CSS and JavaScript

Step 2: Learn React which is a frontend library for building User Interfaces

Step 3: Learn Node.js which is JavaScript runtime environment

Step 4: Learn Express.js, a framework built upon Node.js to simplify the process of creating web application and API building

Step 5: Learn MongoDB, a NoSQL database to store and retrieve data from database.

Getting Started with MERN Stack:

There are two requirements to start a MERN stack project:

Step 1: Install node on your system depending on your Operating System:

Step 2: You need a code editor to work with so install a code editor, preferably we will use VS Code

Setting Up a MERN Stack Project

To setup MERN stack we need to create a folder structure for both frontend and backend. Then we have to define database schema to store and retrieve data from the database.

Follow the below steps to create a basic structure

Step 1: After the code editor is installed, create a new project folder. Then go to the project folder in command prompt/terminal and type below commands to create folder for frontend and backend 

mkdir frontend
mkdir backend

Step 2: Navigate to the frontend folder using the command

cd frontend

Step 3: Initialize a React Project using the command

npx create-react-app

Step 4: Now navigate to the backend folder using the command

cd..
cd backend

Step 5: Initialize the project backend using the command

npm init -y

Step 6: Install Express and other backend dependencies using the command

npm i mongodb express cors dotenv 

After following all the above steps a basic structure for MERN Stack application is created.

Getting to know MERN Stack components:  

1. MongoDB: Cross-platform Document-Oriented Database 

MongoDB is a NoSQL database where each record is a document comprising of key-value pairs that are similar to JSON (JavaScript Object Notation) objects. MongoDB is flexible and allows its users to create schema, databases, tables, etc. Documents that are identifiable by a primary key make up the basic unit of MongoDB. Once MongoDB is installed, users can make use of Mongo shell as well. Mongo shell provides a JavaScript interface through which the users can interact and carry out operations (eg: querying, updating records, deleting records).

Why use MongoDB? 

use database_name;
db.createCollection("collection_name");
db.collection_name.insert
(
{
"id" : 1,
"Name" : "Klaus",
"Department": "Technical",
"Organization": "Geeks For Geeks"
}
);
db.collection_name.find({Name : "Klaus"}).forEach(printjson);

2. Express: Back-End Framework: 

Express is a Node.js framework. Rather than writing the code using Node.js and creating loads of Node modules, Express makes it simpler and easier to write the back-end code. Express helps in designing great web applications and APIs. Express supports many middlewares which makes the code shorter and easier to write.

Why use Express? 

npm init
npm install express --save




const express=require('express'),
http=require('http');
const hostname='localhost';
const port=8080;
const app=express();
  
app.use((req, res)=> {
        console.log(req.headers); 
        res.statusCode=200; 
        res.setHeader('Content-Type', 'text/html'); 
        res.end('<html><body><h1>This is a test server</h1></body></html>');
});
const sample_server=http.createServer(app);
  
sample_server.listen(port, hostname, ()=> {
        console.log(`Server running at http: //${hostname}:${port}/`);});

npm start

3. React: Front-End Library

React is a JavaScript library that is used for building user interfaces. React is used for the development of single-page applications and mobile applications because of its ability to handle rapidly changing data. React allows users to code in JavaScript and create UI components. 

Why use React? 

npm install create-react-app --global

OR 

yarn global add create-react-app
create-react-app app_name




ReactDOM.render(
    <h1>Hello DEVELOPERS!!</h1>,
    document.getElementById('root')
);

npm start

or 
 

yarn start

4. Node.js: JS Runtime Environment 

Node.js provides a JavaScript Environment which allows the user to run their code on the server (outside the browser). Node pack manager i.e. npm allows the user to choose from thousands of free packages (node modules) to download. 

Why use Node.JS? 

npm init

Example: A basic Node.js example to compute the perimeter & area of a rectangle.




let rectangle= {
    perimeter: (x, y)=> (2*(x+y)), area: (x, y)=> (x*y)
};
  
function Rectangle(l, b) {
    console.log("A rectangle with l = "
        l + " and b = " + b);
  
    if (l <=0 || b <=0) {
        console.log("Error! Rectangle's length & "
        + "breadth should be greater than 0: l = " 
        + l + ", and b = " + b);
    }
  
    else {
        console.log("Area of the rectangle: " 
            + rectangle.area(l, b));
        console.log("Perimeter of the rectangle: " 
            + rectangle.perimeter(l, b));
    }
}
  
Rectangle(1, 8);
Rectangle(3, 12);
Rectangle(-6, 3);

npm start


Article Tags :