Skip to content

Kumar-Aryan-NIET/MERN-Stack-Reference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MERN-Stack-Reference

What is MERN Stack ?

MERN Stack is a collection of powerful technologies and robust, used to develop scalable master web applications comprising backend, front-end, and database components.

It is JavaScript that is used for the faster and easier development of full-stack web applications.

MERN Stack is a technology that is a user-friendly full-stack JavaScript framework for building applications and dynamic websites.

Documentation Link

  • MongoDB: A cross-platform document-oriented database program
  • Express.js: A web application framework for Node.js
  • React: JavaScript library for building user interfaces
  • Node.js: An open source, cross-platform, JavaScript run-time environment that executes JavaScript code outside of a browser
  • Why should we choose MERN Stack for building Mobile and Web applications?

    Cost-effective:

    • MERN (MongoDB, Express.js, React.js, and Node.js) technologies are used in MERN Stack is built on JavaScript which provide less cost investment with better output.

    SEO friendly:

    • SEO (Search Engine Optimization) friendly means that search engines can search each page on the website efficiently and easily, interpret and correlate the content effectively with the searched text and easily index it in their database.

    Better performance:

    • Better performance refers to the faster response between backend and front-end and database, which ultimately improves the website speed and yields better performance, thus providing a smooth user experience.

    Improves Security:

    • It mainly concerns the security of applications generated using MERN; here web application security refers to various processes, methods or technologies used for protecting web servers and various web applications, such as APIs (Application user interface) from the attack by internet-based threats.
    • Generally, secured hosting providers can easily integrate applications created using the MERN stack.
    • For more or better security Mongo DB and Node.js security tools are also used.

    Provide the fastest delivery:

    • Any Web applications and mobile applications created by using MERN Stack are built much faster, which also helps to provide faster delivery to our clients.

    Provides faster Modifications:

    • MERN stack technologies supports quick modifications as per the client's request in the mobile and web applications.

    Open Source:

    • All the four technologies that are involved in MERN are open-source.
    • This feature allows developers to get solutions to queries that may evolve from the open portals during development. As a result, it will be ultimately beneficial for a developer.

    Easy to switch between client and server:

    • MERN is very simple and fast because it is written in only one language. And also, it is very easy to switch between client and server.

    Architectural Structure of MERN Stack and its working ?

    MERN has a 3-tier Architecture system mainly consisting of 3 layers -

    These layers are as follows:

    1. Web as front-end tier
    2. Server as the middle tier
    3. Database as backend tier

    MERN's Four Technologies role

    MongoDB :

    • Mongo DB is the most popular NoSQL (NoSQL or Non Structured Query Language) database, an open-source document-oriented database.

    • 'NoSQL' typically means a non-relational database that does not require a fixed schema or proper relational tables to store the necessary data in it.

    • The storage format in which the data is stored is known as BSON, which stands for Binary JavaScript Object Notation; its binary structure encodes length and type of information, which allows it to be parsed much more quickly.

    • MongoDB uses BSON when storing documents in collections.

    • It allows a highly scalable and flexible document structure.

    • It is very faster as compared to RDBMS due to its efficient storage and indexing techniques.

    • In MongoDB, complex join operations are not available; hence, it cannot support complex transactions.

    • MongoDB uses JavaScript for coding as a language which is one of the great advantages.

    • It is Schemaless as any data stored which is stored in a separate document.

    • MongoDB is a NoSQL database that scales by adding more and more servers and increases productivity with its flexible document model.

    Features

    • Schema-less Database
    • Indexing
    • Document Oriented
    • Faster
    • Scalability
    • High Performance
    • Replication and High Available
    • Aggregation
    • Simple Environment Setup

    Express

    • Express is a JavaScript server-side framework that runs within node.js used to build fast, maintainable, and robust productions web applications..

    • It provides the developer with a platform to create and maintain robust servers.

    • Express is used for building and designing web and mobile applications easily and quickly by providing server-side logic.

    • It allows developers to spin up robust APIs (Application Programming Interface) and web servers much easier and simpler.

    • Express makes robust web servers easier to organize your application's functionality with routing and middleware.

    • It also adds helpful functionalities to Node.js HTTP (HyperText Transfer Protocol) objects.

    Features

    • Based on HTTP methods and URLs, Express allows you to define the routes of your application.

      • [ Routing: describe code that needs to be run in response to any request received by a server ]
    • To perform additional tasks and functions on any request and response, you can easily use various middleware modules present in Express.

      • [ Middleware: used to add functionalities or augment the behaviour of the webserver during the lifecycle of request or response in the form of code ]
      • Error can easily handle by using error handling middleware.
    • Express facilitates you to create a REST API (Representational State Transfer Application Programming Interface)

      • [ REST API: conforms to the constraints of REST architectural style, and it also allows for interaction with RESTful web services.]
      • Advantage of REST API is that it provides great flexibility; it uses HTTP requests to access and use data.
    • The data flow into a website structure can easily facilitate by using the two template engines, EJS and Jade, provided by Express.

    • Express has a gigantic suite of third-party add-ons so that developers can use it to provide better functionality, helps to increase the security level, and improve speed.

    • ne can easily access it from anywhere and use it simultaneously on different systems, and very fast.

    • With its built-in router, it promotes code reusability.

    • Single-threaded and Asynchronous.

    • biggest community for Node.js.

    React

    • React is one of the most popular open-source front-end JavaScript libraries used for building Web applications UI developed by Facebook.

    • It is not a JavaScript framework.

    • It was first created by software engineer Jordan Walke, who works for Facebook.

    • React was first deployed in the Facebook news feed.

    • Prerequisites: you must download Node packages in your system with their latest versions. Also, you must have an understanding of HTML, CSS and JavaScript.

    • React is also used for making a grip over the view layer for mobile and web applications.

    • It allows us to create reusable UI ( User Interface ) components.

    • It allows developers to create large web applications that can easily change the data of the page even without reloading the page.

    • It is very fast, simple and scalable.

    Features

    • Easy to Learn
    • Simple
    • Data Binding
    • Native Approach
    • Performance
    • Testability

    Node.js

    • node.js is an open-source server environment, and it is a cross-platform runtime environment for executing JavaScript code outside a browser.

    • It is often used for building and developing numerous backend services like net applications, mobile applications.

    • It may be a free ASCII text file platform and may be utilized by anybody.

    • It is incredibly simple to urge started with it and may even be used for agile development and prototyping.

    • It provides extremely ascendable and really quick services to the users.

    • It is incredibly consistent and may be used as an ASCII text file cleaner.

    • It continuously uses JavaScript; thus, it's ultimately helpful for a computer user to quickly create any net service or any net or mobile application.

    • It provides a massive system for any ASCII text file library.

    • It contains a Non-blocking or, can say, Asynchronous nature.

    Features

    • Easy Scalability
    • Fast
    • Easy to learn and debug code
    • Real-time web apps
    • Caching Advantage
    • Data Streaming
    • Object-Oriented Approach
    • Event-Driven
    • Corporate Support

    Learning the MERN stack will help you become a full-stack developer. The demand for MERN stack developers is also high. However, it takes time to be a good MERN stack developer. So, before learning technologies like React or Node.js, it is important to ensure that your fundamentals are strong. A lot of developers fail to be good MERN stack developers because they lack the fundamental knowledge of JavaScript.

    Server setup with Express.js and Node.js

    npm package initialization

    Enter the folder through the terminal, then run $ npm init. Then, it will ask you some questions about the package name, version, entry point, and more.

    Select yes, and you’re ready to go. This will create a file named package.json in same folder.

    Installing the dependencies

    We’ll add some dependencies with

    $ npm i express mongoose body-parser config.

    • body-parser: Allows us to get the data throughout the request
    • express: Is our main framework
    • mongoose: Is used to connect and interact with MongoDB
    • config: This lets you define default parameters for your application

    Install nodemon with

    $ npm i -D nodemon.

    To use nodemon, add "server": "nodemon server.js" to your scripts tag under the package.json file.

    Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.

    After that, your package.json should look like this:

    Setting the entry point:

    create a file named server.js for our entry point.

    Then paste the following code:

    // server.js

    const express = require('express');

    const app = express();

    app.get('/', (req, res) => res.send('Hello world!'));

    const port = process.env.PORT || 2025;

    app.listen(port, () => console.log(Server running on port ${port}));

    After that, run the

    $ node server.js

    At this point, if we change anything, we’ll need to restart the server manually. But, if we set up nodemon, then we don’t have to restart it every time. Nodemon will watch if there is any change and restart the server automatically.

    you can run your project using the

    $ npm run server

    You will see Server running on port 2025. You can also check it from the browser by opening the browser and entering http://localhost:2025.

    Database management with MongoDB

    For simplicity, we will use MongoDB Atlas. First, create an account here. After creating an account, you will see something like this:

    Click the Project 0 section (top left), and you will see a button for Creating a New Project. Create a project and select the project. Now, click the Build a Database button from the project you have created. It will show you all the information.

    At the bottom, you will see a section called Cluster Name, click that and enter a name for the database, then hit the Create Cluster button. After two to three minutes, if everything goes well, you will find something like this:

    Click the CONNECT button if you filled the username and password for your database at time of creation it will show you like this:

    Now, if you follow the CONNECT button or the Choose a connection method button, you will see some different methods. Select accordingly:

    In this case, select the Connect Your Application section. Now, you will get your database link, which we will use in our next step:

    Adding the database to our project

    Our database is ready, and we need to add it to our project. Inside the project folder, create another folder named config and create two files named default.json and db.js.

    Add the following code: // default.json

    { "mongoURI": "mongodb+srv://mern123:@mernatoz-9kdpd.mongodb.net/test?retryWrites=true&w=majority" } /* Replace with your database password */

    // db.js

    const mongoose = require('mongoose'); const config = require('config'); const db = config.get('mongoURI');

    const connectDB = async () => { try { mongoose.set('strictQuery', true); await mongoose.connect(db, { useNewUrlParser: true, });

    console.log('MongoDB is Connected...');
    

    } catch (err) { console.error(err.message); process.exit(1); } };

    module.exports = connectDB;

    We need a little change in our server.js file to connect to the database. Update your server.js by adding this: const connectDB = require('./config/db');

    // Connect Database connectDB();

    Now, you can run the project using the

    $ npm run server

    You should see the following:

    About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published