Mean Stack Questions
Mean Stack Questions
ffffjhjhjhjhj
1-In Node.js, Modules are the blocks of encapsulated code that communicate with an
external application on the basis of their related functionality. Modules can be a single
file or a collection of multiple files/folders. The reason programmers are heavily reliant
on modules is because of their reusability as well as the ability to break down a complex
piece of code into manageable chunks.
Core Modules
local Modules
Third-party Modules
Core Modules: Node.js has many built-in modules that are part of the platform and come with
Node.js installation. These modules can be loaded into the program by using the required
function.
Syntax:
The require() function will return a JavaScript type depending on what the particular module
returns. The following example demonstrates how to use the Node.js http module to create a web
server.
javascript
res.end();
}).listen(3000);
In the above example, the require() function returns an object because the Http module returns its
functionality as an object. The function http.createServer() method will be executed when
someone tries to access the computer on port 3000. The res.writeHead() method is the status
code where 200 means it is OK, while the second argument is an object containing the response
headers. The following list contains some of the important core modules in Node.js:
Core
Modules Description
process provides information and control about the current Node.js process.
Local Modules: Unlike built-in and external modules, local modules are created locally in your
Node.js application. Let’s create a simple calculating module that calculates various operations.
Create a calc.js file that has the following code:
Filename: calc.js
javascript
return x + y;
};
return x - y;
};
return x * y;
};
return x / y;
};
Since this file provides attributes to the outer world via exports, another file can use its exported
functionality using the require() function.
Filename: index.js
javascript
+ calculator.add(x, y));
console.log("Subtraction of 50 and 10 is "
+ calculator.sub(x, y));
+ calculator.mult(x, y));
+ calculator.div(x, y));
Step to run this program: Run the index.js file using the following command:
node index.js
Output:
Addition of 50 and 10 is 60
Subtraction of 50 and 10 is 40
Division of 50 and 10 is 5
Note: This module also hides functionality that is not needed outside of the module.
Third-party modules: Third-party modules are modules that are available online using the
Node Package Manager(NPM). These modules can be installed in the project folder or globally.
Some of the popular third-party modules are Mongoose, express, angular, and React.
Example:
Virtual DOM:
Performance:
Extension:
Conditional Statements:
Components:
Simplicity:
Let’s understand each of them in detail:
1. JSX(JavaScript Syntax Extension):
JSX is a combination of HTML and JavaScript. You can embed
JavaScript objects inside the HTML elements. JSX is not
supported by the browsers, as a result, Babel compiler
transcompile the code into JavaScript code. JSX makes codes
easy and understandable. It is easy to learn if you know HTML
and JavaScript.
const name="GeekforGeeks";
const ele = <h1>Welcome to {name}</h1>;
2. Virtual DOM:
DOM stands for Document Object Model. It is the most
important part of the web as it divides into modules and
executes the code. Usually, JavaScript Frameworks updates
the whole DOM at once, which makes the web application
slow. But react uses virtual DOM which is an exact copy of
real DOM. Whenever there is a modi昀椀cation in the web
application, the whole virtual DOM is updated 昀椀rst and 昀椀nds
the di昀昀erence between real DOM and Virtual DOM.
As shown in the above diagram, data can 昀氀ow only from top
to bottom.
4. Performance:
As we discussed earlier, react uses virtual DOM and updates
only the modi昀椀ed parts. So , this makes the DOM to run
faster. DOM executes in memory so we can create separate
components which makes the DOM run faster.
5. Extension:
React has many extensions that we can use to create full-
昀氀edged UI applications. It supports mobile app development
and provides server-side rendering. React is extended with
Flux, Redux, React Native, etc. which helps us to create good-
looking UI.
6. Conditional Statements:
JSX allows us to write conditional statements. The data in the
browser is displayed according to the conditions provided
inside the JSX.
Syntax:
Multiple components
8. Simplicity:
React.js is a component-based which makes the code
reusable and React.js uses JSX which is a combination of
HTML and JavaScript. This makes code easy to understand
and easy to debug and has less code.
Steps to Create React Application:
Step 1: Create a react application by using the following
command:
cd foldername
Project Structure:
Example: This example uses props data to demonstrate
condiotional rendering in react
Javascript
Javascript
Javascript
Javascript
// Filename - index.js
document.getElementById('root'));
npm start
Output: If you give the value of isPass={true} in index.js,
then it will give the following output:
show dbs
This command returns a list of all the existing MongoDB
databases.
db.createCollection('Student');
createCollection() in MongoDB
Explanation:
In the above example, Student Collection is created using
createCollection() method.
insertOne()
insertMany()
Let’s understand each of these methods with examples.
insertOne() Method
insertOne() method is used to insert only one document in
the collection.
Example:
db.myNewCollection1.insertOne( { name:"geeksforgeeks" } )
Here, we create a collection named as “myNewCollection1”
by inserting a document that contains a “name” 昀椀eld with its
value in it using insertOne() method.
insertMany() method
insertMany() method is used to insert many documents in
the collection
Example:
db.myNewCollection2.insertMany([{name:"gfg",
country:"India"},
{name:"rahul", age:20}])
Here, we create a collection named as myNewCollection2 by
inserting two documents using insertMany() method.
show collections
Example
This command returns a list of all the existing collections in
the gfgDB database.
Express.js Tutorial
Last Updated :444 15 Apr, 2024
Node
app.listen(PORT, () => {
});
Explanation:
Expresson() Function
express.raw() Function
express.Router() Function
express.static() Function
express.text() Function
express.urlencoded() Function
app.locals Property
app.mountpath Property
Mount Event
app.all() Function
app.delete() Function
app.disable() Function
app.disabled() Function
app.enable() Function
app.enabled() Function
req.app Property
req.baseUrl Property
req.body Property
req.cookies Property
req.fresh Property
req.accepts() Function
req.acceptsCharsets() Function
req.acceptsEncodings() Function
req.acceptsLanguages() Function
res.app Property
res.headersSent Property
res.locals Property
res.append() Function
res.attachment() Function
res.cookie() Function
res.clearCookie() Function
res.download() Function
res.end() Function
router.all() Function
router.METHOD() Function
router.param() function
router.route() Function
router.use() Function
Node vs Express
Middlewares in Express
5
As an asynchronous event-driven JavaScript runtime, Node.js is
designed to build scalable network applications. In the following
"hello world" example, many connections can be handled
concurrently. Upon each connection, the callback is 昀椀red, but if
there is no work to be done, Node.js will sleep.
CJSMJS
Node.js being designed without threads doesn't mean you can't take
advantage of multiple cores in your environment. Child processes
can be spawned by using our child_process.fork() API, and are designed
to be easy to communicate with. Built upon that same interface is
the cluster module, which allows you to share sockets between
processes to enable load balancing over your cores.
It is a non-relational, document-oriented
It is a Relational database management
database management system and works
system and works on relational databases.
on document-based database
MongoDB uses a document structure to The table structure is used to store the data in
store the data. RDBMS
In MongoDB, the database can be scaled In RDBMS, we can scale the database
horizontally. vertically.
Authorization
Encryption on data
db.students.createIndex({“item”:1})
In this example, we are creating a single index on the item
昀椀eld and 1 here represents the 昀椀led is in ascending order.
A compound index in MongoDB contains multiple single
昀椀led indexes separated by a comma. MongoDB restricts the
number of 昀椀elds in a compound index to a maximum of 31.
db.students.createIndex({“item”: 1, “stock”:1})
Here, we create a compound index on item: 1, stock:1
6.2 Multi-Key
When indexing a 昀椀led containing an array value, MongoDB
creates separate index entries for each array component.
MongoDB allows you to create multi-key indexes for arrays
containing scalar values, including strings, numbers, and
nested documents.
Syntax:
router.METHOD(path, [callback, ...] callback)
You can visit the link to Install the express module. You can
install this package by using this command.
After that, you can just create a folder and add a 昀椀le, for
example, index.js. To run this 昀椀le you need to run the
following command.
node index.js
Project Structure:
// Single route
res.end();
});
app.use(router);
if (err) console.log(err);
Make sure you have installed the express module using the
following command:
node index.js
Output:
Console Output:
javascript
// Multiple routes
res.end();
});
res.end();
});
})
app.use(router);
if (err) console.log(err);
});
node index.js
Output:
Prerequisites
What is DOM ?
The browser parses the HTML to 昀椀nd the node with this id.
Public Cloud
Private Cloud
Hybrid Cloud
Community Cloud
Multi-Cloud
Public Cloud
The public cloud makes it possible for anybody to access
systems and services. The public cloud may be less secure as
it is open to everyone. The public cloud is one in which cloud
infrastructure services are provided over the internet to the
general people or major industry groups. The infrastructure in
this cloud model is owned by the entity that delivers the
cloud services, not by the consumer. It is a type of cloud
hosting that allows customers and users to easily access
systems and services. This form of cloud computing is an
excellent example of cloud hosting, in which service
providers supply services to a variety of customers. In this
arrangement, storage backup and retrieval services are given
for free, as a subscription, or on a per-user basis. For
example, Google App Engine etc.
Public Cloud
Advantages of the Public Cloud Model
Private Cloud
Hybrid Cloud
Advantages of the Hybrid Cloud Model
You can mix and match the best features of each cloud
provider’s services to suit the demands of your apps,
workloads, and business by choosing di昀昀erent cloud
providers.
Reduced Latency: To reduce latency and improve user
experience, you can choose cloud regions and zones that
are close to your clients.
Privacy: Privacy tells about what data you gather for the
model.
Each model has some advantages and some disadvantages,
and the selection of the best is only done on the basis of your
requirement. If your requirement changes, you can switch to
any other model.
Public Community
Factors Cloud Private Cloud Cloud Hybrid Cloud
Scalability
High High Fixed High
and Flexibility
React Components
12Apr, 2024
Components in React serve as independent and reusable
code blocks for UI elements. They represent di昀昀erent parts of
a web page and contain both structure and behavior. They
are similar to JavaScript functions and make creating and
managing complex user interfaces easier by breaking them
down into smaller, reusable pieces.
What are React Components?
React Components are the building block of React
Application. They are the reusable code blocks containing
logics and and UI elements. They have the same purpose as
JavaScript functions and return HTML. Components make
the task of building UI much easier.
A UI is broken down into multiple individual pieces called
components. You can work on components independently
and then merge them all into a parent component which
will be your 昀椀nal UI.
Components promote e昀케ciency and scalability in web
development by allowing developers to compose, combine,
and customize them as needed.
You can see in the below image we have broken down the UI
of GeeksforGeeks’s homepage into individual components.
Components in React return a piece of JSX code that tells
what should be rendered on the screen.
Types of Components in React
In React, we mainly have two types of components:
Functional Components
function demoComponent() {
return (<h1>
Welcome Message!
</h1>);
}
Example: Create a function component called welcome.
Javascript
function welcome() {
}
Class Component in React
The class components are a little more complex than the
functional components. A class component can show
inheritance and access data of other components.
Class Component must include the line “extends
React.Component” to pass data from one class component
to another class component. We can use JavaScript ES6
classes to create class-based components in React.
Syntax:
Javascript
render() {
}
The components we created in the above two examples are
equivalent, and we also have stated the basic di昀昀erence
between a functional component and a class component. We
will learn about more properties of class-based components
in further tutorials.
Functional Component vs Class
Component
A functional component is best suited for cases where the
component doesn’t need to interact with other components
or manage complex states. Functional components are ideal
for presenting static UI elements or composing multiple
simple components together under a single parent
component.
While class-based components can achieve the same result,
they are generally less e昀케cient compared to functional
components. Therefore, it’s recommended to not use class
components for general use.
Rendering React Components
Rendering Components means turning your component
code into the UI that users see on the screen.
React is capable of rendering user-de昀椀ned components. To
render a component in React we can initialize an element
with a user-de昀椀ned component and pass this element as the
昀椀rst parameter to ReactDOM.render() or directly pass the
component as the 昀椀rst argument to the ReactDOM.render()
method.
The below syntax shows how to initialize a component to an
element:
javascript
// Filename - src/index.js:
};
ReactDOM.render(
<Welcome />,
document.getElementById("root")
);
Output: This output will be visible on the
http://localhost:3000/ on the browser window.
Explanation:
Let us see step-wise what is happening in the above
example:
Javascript
function Message(props) {
return <h2>{props.text}</h2>;
Javascript
// Filename - src/index.js:
};
ReactDOM.render(
<Welcome />,
document.getElementById("root")
);
The above code will give the same output as other examples
but here we have called the Greet component inside the
Welcome Component.