Full Stack Module -3 Final
Full Stack Module -3 Final
VI SEMESTER -2024-25
Module 3: Introduction to MERN
MERN components
What’s MERN?
Any web application is built using multiple technologies. The combinations of
these technologies is called a “stack,”
As web development matured and their interactivity came to the fore, Single Page
Applications (SPAs) became more popular.
MongoDB, a very popular NoSQL database, was used for persistent data storage.
This stack was arguably the most popular stack for any new web application until a
few years back
Introduction
React, an alternate front-end technology created by Facebook, has been gaining
popularity and offers an alternative to AngularJS. It thus replaces the “A” with an “R” in
MEAN, to give us the MERN Stack.
I said “not exactly” since React is not a full-fledged MVC framework. It is a JavaScript
library for building user interfaces, so in some sense, it’s the View part of the MVC.
GitHub - vasansr/pro-mern-stack-2: Code listing for the book Pro MERN Stack, 2nd Edition
MERN Components
Express
Unlike AngularJS, React is not a framework. It is a library. Thus, it does not, by itself,
dictate a framework pattern such as the MVC pattern. You use React to render a view (the
V in MVC)
Not just Facebook itself, but there are many other companies that use React in production
like Airbnb, Atlassian, Bitbucket, Disqus, Walmart, etc.
Declarative
React views are declarative: What this really means you don’t worry about
transitions or mutations in the DOM caused by changes to the view’s state.
React takes care of this using its Virtual DOM technology. When things change,
React builds a new virtual DOM based on the new truth (state) and compares it
with the old (before things changed) virtual DOM.
React then computes the differences in the old and the changed Virtual DOM, then
applies these changes to the actual DOM
MERN Components
Component-Based
The fundamental building block of React is a component that maintains its own
state and renders itself.
A component encapsulates the state of data and the view, or how it is rendered.
This makes writing and reasoning about the entire application easier, by splitting
it into components and focusing on one thing at a time.
Components talk to each other by sharing state information in the form of read-
only properties to their child components and by callbacks to their parent
components.
MERN Components
No Templates
Many web application frameworks rely on templates to automate the task of creating
repetitive HTML or DOM elements. The templating language in these frameworks is
something that the developer will have to learn and practice. Not in React.
There is an intermediate language to represent a Virtual DOM, and that is JSX (short for
JavaScript XML), which is very much like HTML. This lets you create nested DOM
elements in a familiar language rather than hand-construct them using JavaScript functions.
Note that JSX is not a programming language; it is a representational markup like HTML. It’s
also very similar to HTML so you don’t have to learn too much.
MERN Components
Isomorphic
That’s what isomorphic means: the same code can run on the server and the
browser. This allows you to create pages on the server when required
MERN Components
Node.js
The creators of Node.js just took Chrome’s V8 JavaScript engine and made it run
independently as a JavaScript runtime. If you are familiar with the Java runtime that runs
Java programs, you can easily relate to the JavaScript runtime: the Node.js runtime runs
JavaScript programs.
Although you may find people who don’t think Node.js is fit for production use, claiming it
was meant for the browser, there are many industry leaders who have chosen Node.js.
Netflix, Uber, and LinkedIn are a few companies that use Node.js in production, and that
should lend credibility as a robust and scalable environment to run the back-end of any
application
MERN Components
Node.js Modules In a browser, you can load multiple JavaScript files, but you need an
HTML page to do all that.
You cannot refer another JavaScript file from one JavaScript file. But for Node.js, there is
no HTML page that starts it all. In the absence of the enclosing HTML page, Node.js uses its
own module system based on CommonJS to put together multiple JavaScript files.
Modules are like libraries. You can include the functionality of another JavaScript file
Node.js ships with a bunch of core modules compiled into the binary. These modules provide
access to the operating system elements such as the file system, networking, input/output,
etc.
MERN Components
Node.js and npm
npm is the default package manager for Node.js. You can use npm to install third-
party libraries (packages) and manage dependencies between them. The npm registry
(www.npmjs.com) is a public repository of all modules published by people for the
purpose of sharing.
Express is a framework that simplifies the task of writing the server code.
The Express framework lets you define routes, specifications of what to do when an
HTTP request matching a certain pattern arrives. The matching specification is
regular expression (regex) based and is very flexible, like most other web application
frameworks.
Express parses the request URL, headers, and parameters for you. On the response
side, it has, as expected, all functionality required by web applications. This includes
determining response codes, setting cookies, sending custom headers, etc
MERN Components
• you can write Express middleware, custom pieces of code that can be inserted in
any request/response processing path to achieve common functionality such as
logging, authentication, etc.
• Express does not have a template engine built in, but it supports any template
engine of your choice such as pug, mustache, etc.
• But, for an SPA, you will not need to use a server-side template engine. This is
because all dynamic content generation is done on the client, and the web server
only serves static files and data via API calls
MERN Components
MongoDB
Not only do many modern companies (including Facebook and Google) use
MongoDB in production, but some older established companies such as SAP and
Royal Bank of Scotland have adopted MongoDB
MERN Components
NoSQL
NoSQL stands for “non-relational,” no matter what the acronym expands to. It’s essentially
not a conventional database where you have tables with columns and rows and strict
relationships among them. I find that there are two attributes of NoSQL databases that
differentiate them from the conventional.
• The first is their ability to horizontally scale by distributing the load over multiple servers.
They do this by sacrificing an important (for some) aspect of the traditional databases:
strong consistency.
• The second, and according to me more important, aspect is that NoSQL databases are not
necessarily relational databases. The difference between the representation in the
application (objects) and on disk (rows in tables) is sometimes called impedance
mismatch
MERN Components
Document-Oriented
Compared to relational databases where data is stored in the form of relations, or
tables, MongoDB is a document-oriented database.
Imagine the storage structure of an invoice, with the customer name, address, etc. and
a list of items (lines) in the invoice. If you had to store this in a relational database, you
would use two tables, say, invoice and invoice_lines, with the lines or items referring
to the invoice via a foreign-key relation.
MERN Components
Schema-Less
This means that, especially during early stages of development, you don’t
need to add/rename columns in the schema. You can quickly add fields in your
application code without having to worry about database migration scripts.
MERN Components
JavaScript Based
MongoDB’s language is JavaScript. For relational databases, we had a query language called
SQL. For MongoDB, the query language is based on JSON.
You create, search for, make changes, and delete documents by specifying the operation in a
JSON object. The query language is not English-like (you don’t SELECT or say WHERE), and
therefore much easier to construct programmatically.
Data is also interchanged in JSON format. In fact, the data is natively stored in a variation of
JSON called BSON (where B stands for Binary) in order to efficiently utilize space. When you
retrieve a document from a collection, it is returned as a JSON object.
MongoDB comes with a shell that’s built on top of a JavaScript runtime like Node.js.
Server-Less Hello World
• The React library is available as a JavaScript file that we can include in the HTML
file using the <script> tag.
The first is the React core module, the one that is responsible for dealing with
React components, their state manipulation, etc
The second is the ReactDOM module, which deals with converting React
components to a DOM that a browser can understand.
Server-Less Hello World
These two libraries can be found in unpkg, a Content Delivery Network (CDN) that
makes all open-source JavaScript libraries available online.
Let's use the development (as opposed to production) version of the libraries from the
following URLs:
React: https://unpkg.com/react@16/umd/react.development.js
ReactDOM: https://unpkg.com/react-dom@16/umd/react-dom.development.js
Server-Less Hello World
To create the React element, the createElement() function of the React module
needs to be called.
These applications help you create a bunch of issues or bugs, assign them to
people, and track their statuses.
CRUD applications
• The user should be able to view a list of issues, with an ability to filter the list by
various parameters.
• The user should be able to add new issues, by supplying the initial values of the
issue’s fields.
• The user should be able to edit and update an issue by changing its field values.
These classes can then be reused within other components, handle events, and
so much more
React classes are created by extending React.Component, the base class from
which all custom classes must be derived.
• render() is one that must be present, otherwise the component will have no
screen presence
• In essence, the JSX element is now returned from the render() method of the
component class called Hello World. The brackets around the JSX representation
of the Hello World element are not necessary, but it is a convention that is
normally used to make the code more readable, especially when the JSX spans
multiple lines.
Composing Components
Component composition is one of the most powerful features of React.
This way, the UI can be split into smaller independent pieces so that each
piece can be coded and reasoned in isolation, making it easier to build and
understand a complex UI.
A component takes inputs (called properties) and its output is the rendered
UI of the component
Composing Components
Here are a few things to remember when composing components:
• Let’s use a Fragment component like this in the IssueList’s render() method
• let’s instantiate the IssueList class, which we will place under the contents div.
Lab Program -5
Build a React application to track issues. Display a list of issues (use
static data). Each issue should have a title, description, and status
(e.g., Open/Closed). Render the list using a functional component.
Ubuntu + Node.js Setup
Install Windows Subsystem for Linux (WSL) on Windows
Next,
• Restart the computer when prompted.
• Again in search bar -Microsoft store-type ubuntu
• Select ubuntu app-click on get.
• Then install->takes a will to install.
• Open click once done it ask for user name & password.
• Ready to work ,better to check installed properly by
checking some UNIX commands.
April, 2025 Department of ISE, GAT, Bangalore, India 43
Ubuntu + Node.js Setup
nvm –version
function App() {
return <IssueList />;
}
export default App;
April, 2025 Department of ISE, GAT, Bangalore, India 50
Check index.js->Make sure src/index.js looks like:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
const root =
ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
April, 2025 Department of ISE, GAT, Bangalore, India 51
Check index.html
If Error then,
Confirm App.js renders something
Confirm IssueList.js exports properly
Confirm index.js attaches to #root
Restart dev server
Test with simple text
April, 2025 Department of ISE, GAT, Bangalore, India 54
Passing Data Using Properties
• Props (short for "properties") are read-only attributes used to pass data
from one component to another, typically from a parent component to a
child component.
return (
<div>
<Child name="Alice" age={25} onClick={handleClick} />
</div>
); }
Child Component receives and uses props:
function Child({ name, age, onClick }) {
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
<button onClick={onClick}>Click Me</button>
</div>
); }
Passing Data Using Properties
Passing Different Types of Data via Props
• There is another way to pass data to other components, using the contents of the
HTML-like node of the component.
• In the child component, this can be accessed using a special field of this.props
called this. props.children.
• we nested a<h1> element inside a <div>. When the components are converted
to HTML elements, the elements nest in the same order. React components can
act like the and take in nested elements.
Dynamic Composition
• In this section, we’ll replace our hard-coded set of IssueRow components with a
programmatically generated set of components from an array of issues.
const issues = [
{ id: 1, status: New', owner: 'Ravan', effort: 5, created: new Date('2018-
08-15'), due: undefined, title: 'Error in console when clicking Add', },