0% found this document useful (0 votes)
4 views61 pages

Full Stack Que

The document provides an overview of the MEAN Stack, which consists of MongoDB, Express.js, Angular, and Node.js, all utilizing JavaScript for efficient web application development. It explains the benefits of using the MEAN Stack, including single language usage, high performance, and cloud compatibility, as well as the architecture and workflow of the stack. Additionally, the document covers various JavaScript concepts such as let vs const, arrow functions, template literals, destructuring, and promises, among others, highlighting their importance in modern JavaScript programming.

Uploaded by

dabotir635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views61 pages

Full Stack Que

The document provides an overview of the MEAN Stack, which consists of MongoDB, Express.js, Angular, and Node.js, all utilizing JavaScript for efficient web application development. It explains the benefits of using the MEAN Stack, including single language usage, high performance, and cloud compatibility, as well as the architecture and workflow of the stack. Additionally, the document covers various JavaScript concepts such as let vs const, arrow functions, template literals, destructuring, and promises, among others, highlighting their importance in modern JavaScript programming.

Uploaded by

dabotir635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

1. What is MEAN Stack?

The MEAN Stack is a popular collection of JavaScript-based technologies used


to develop web applications—both frontend and backend. "MEAN" is an
acronym for four key technologies:

 M: MongoDB – NoSQL database


 E: Express.js – Backend web framework for Node.js
 A: Angular – Frontend framework developed by Google
 N: Node.js – JavaScript runtime environment

All the technologies in the MEAN Stack use JavaScript, making development
efficient and consistent across the entire application—from client to server to
database.

2. Describe Different Technologies of MEAN in Short

1. MongoDB:
o A NoSQL database that stores data in a flexible, JSON-like format
(BSON).
o Schema-less and suitable for handling large volumes of
unstructured data.
o It allows dynamic querying and easy scalability.
2. Express.js:
o A lightweight web application framework built on top of Node.js.
o It simplifies the process of creating APIs and managing server-side
routing and middleware.
o Known for its speed, flexibility, and minimalism.
3. Angular:
o A powerful frontend framework developed by Google.
o It supports two-way data binding, component-based architecture,
and client-side routing.
o Angular applications are modular, maintainable, and scalable.
4. Node.js:
o A JavaScript runtime environment that executes JavaScript code
outside the browser.
o Built on Chrome’s V8 engine and uses an event-driven, non-
blocking I/O model.
o Ideal for building scalable and high-performance backend
applications.
3. Explain Benefits of Using MEAN Stack

 Single Language Across Stack: JavaScript is used throughout the


application (client-side, server-side, and database interaction), reducing
context switching and making it easier for developers to work across the
stack.
 Open Source and Free: All components are open source and have large
communities, ensuring ongoing support and free resources.
 High Performance and Scalability: Node.js enables high-performance
backend development, and MongoDB scales horizontally to handle large
data sets.
 Flexible and Fast Development: JSON flows seamlessly across all
components (Angular ↔ Node.js ↔ MongoDB), simplifying data
handling and improving speed of development.
 Cloud Compatibility: MEAN applications are easily deployable on
cloud platforms, and MongoDB is especially well-suited for cloud
integration.
 Support for MVC Architecture: Express and Angular together support
Model-View-Controller (MVC) structure, improving code manageability
and separation of concerns.

4. Explain Architecture of MEAN Stack

The MEAN Stack follows a layered, modular architecture:

1. Angular (Frontend)

 The user interface is built using Angular.


 Responsible for handling user input, validating forms, displaying data,
and communicating with the backend via HTTP services (typically using
Angular’s HttpClient).

2. Express.js and Node.js (Backend)

 The Node.js server handles client requests.


 Express.js is used to define routes, controllers, and middleware functions.
 It processes the logic and sends requests to the database.

3. MongoDB (Database Layer)

 MongoDB stores the application data in JSON-like documents.


 It communicates with the backend via the MongoDB Node.js driver or
Mongoose (an ODM library).

4. Workflow Overview

1. A user interacts with the Angular frontend.


2. Angular makes a REST API call to the backend (Express.js).
3. Express routes the request and processes business logic.
4. If needed, Express interacts with MongoDB to fetch or update data.
5. The response is sent back through Express to Angular, which updates the
UI.

Architecture Diagram (Optional: I can generate a diagram if needed.)

Chapter 2

1. Explain the difference between let and const. Provide examples of


scenarios where you would use each.

In JavaScript, both let and const are used to declare variables with block
scope, meaning they are only accessible within the block where they are
defined. However, the key difference is that let allows reassignment of values,
whereas const does not. Use let when the value of a variable needs to
change during execution, such as in a loop or when tracking a dynamic value.
Use const when the value should remain constant after assignment, which
improves code readability and helps prevent accidental reassignments.

javascript
CopyEdit
let counter = 0;
counter = counter + 1; // This is valid

const PI = 3.14159;
// PI = 3.14; // This will throw an error: Assignment
to constant variable.
2. Rewrite the following function using arrow function syntax:

javascript
CopyEdit
function multiply(a, b) {
return a * b;
}

The arrow function syntax provides a concise way to define functions and
automatically binds the context (this). Here's the arrow function version:

javascript
CopyEdit
const multiply = (a, b) => a * b;

This is especially useful for inline operations or when passing functions as


arguments.

3. What are template literals? Provide an example of their usage.

Template literals in ES6 are enclosed by backticks (`) instead of single or


double quotes. They allow for interpolation (inserting variables and
expressions inside a string) using ${} and support multi-line strings without
the need for concatenation or escape characters.

javascript
CopyEdit
const name = "Alice";
const age = 25;
const message = `My name is ${name} and I am ${age}
years old.`;
console.log(message); // Output: My name is Alice and
I am 25 years old.

This makes code cleaner and easier to read compared to traditional string
concatenation.

4. Demonstrate the destructuring assignment for an object and an array.


Destructuring is a convenient way of extracting values from arrays or properties
from objects into distinct variables.

Array Destructuring:

javascript
CopyEdit
const [a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20

Object Destructuring:

javascript
CopyEdit
const user = { name: "Bob", age: 30 };
const { name, age } = user;
console.log(name); // Bob
console.log(age); // 30

It improves code readability and reduces redundancy when accessing values.

5. How does the spread syntax work in JavaScript? Provide an example of


using spread to merge two arrays.

The spread syntax (...) allows an iterable (like an array or object) to be


expanded in places where multiple elements or arguments are expected. It's
commonly used for cloning or merging arrays and objects.

javascript
CopyEdit
const arr1 = [1, 2, 3];
const arr2 = [4, 5];
const merged = [...arr1, ...arr2];
console.log(merged); // [1, 2, 3, 4, 5]

Spread syntax is useful for writing clean, concise code when combining or
copying data structures.

6. Explain the concept of modules in ES6. Provide an example of exporting


and importing a function from one module to another.
ES6 modules allow developers to break code into reusable, maintainable files.
Each file can export variables, functions, or classes and import them in other
files. This promotes modular architecture.

math.js – exporting a function:

javascript
CopyEdit
export function add(x, y) {
return x + y;
}

main.js – importing the function:

javascript
CopyEdit
import { add } from './math.js';
console.log(add(5, 3)); // 8

Modules must usually be run in environments or tools (like Node.js or bundlers)


that support the ES6 module system.

7. What are symbols in JavaScript? How can they be used to create unique
keys in objects?

Symbols are a new primitive type introduced in ES6. Each symbol is unique
and immutable. Symbols can be used as object property keys to avoid name
collisions, especially in scenarios involving extension or metaprogramming.

javascript
CopyEdit
const id = Symbol('id');
const user = {
name: 'Tom',
[id]: 101
};
console.log(user[id]); // 101

Even if another symbol is created with the same description, it will not equal the
original, ensuring uniqueness.
8. Explain the purpose of iterators and generators in ES6. Provide an
example of a generator function.

Iterators are objects that define a sequence and potentially a return value upon
completion. They follow the iterator protocol, which includes a next()
method. Generators are special functions defined using function* syntax and
can pause/resume execution using yield.

javascript
CopyEdit
function* countToThree() {
yield 1;
yield 2;
yield 3;
}
const counter = countToThree();
console.log(counter.next().value); // 1
console.log(counter.next().value); // 2
console.log(counter.next().value); // 3

Generators simplify working with sequences and asynchronous data streams.

9. What are Map and Set in JavaScript? Provide examples of their usage.

 Map is a collection of key-value pairs where keys can be any data type.
 Set is a collection of unique values, with no duplicates.

Map Example:

javascript
CopyEdit
const map = new Map();
map.set('name', 'Jane');
map.set('age', 25);
console.log(map.get('name')); // Jane

Set Example:

javascript
CopyEdit
const set = new Set([1, 2, 2, 3]);
console.log(set); // Set {1, 2, 3}
Maps are useful for flexible key-value storage; Sets are ideal for lists of unique
items.

10. Define what a pure function is and why it is important in functional


programming.

A pure function is one that, given the same input, always returns the same
output and has no side effects (it does not modify external state). This makes
pure functions predictable, testable, and easier to reason about—core principles
in functional programming.

javascript
CopyEdit
function add(a, b) {
return a + b; // No side effects
}

In contrast, impure functions might depend on or modify external variables.


Using pure functions helps in writing reliable, maintainable software.

11. Provide an example of a higher-order function and explain its


characteristics.

A higher-order function is a function that either takes another function as an


argument or returns a function as a result. This concept is central to functional
programming, enabling abstraction and code reuse. Higher-order functions are
useful when you want to apply behavior dynamically or repeatedly. For
example, the built-in map() function in JavaScript is a higher-order function—
it takes a function as an argument and applies it to each element of an array.

javascript
CopyEdit
function double(x) {
return x * 2;
}
const numbers = [1, 2, 3];
const doubled = numbers.map(double); // [2, 4, 6]
In this example, map() is a higher-order function, and double is passed to it.
The power of higher-order functions lies in their ability to operate on behavior,
not just data.

12. Explain the concept of currying. Provide an example of a curried


function.

Currying is a functional programming technique where a function with


multiple arguments is transformed into a sequence of functions, each taking one
argument. This allows for the creation of specialized functions by partially
applying arguments. Currying improves code modularity and reuse by allowing
you to preset arguments for future calls.

javascript
CopyEdit
function multiply(a) {
return function(b) {
return a * b;
};
}
const double = multiply(2);
console.log(double(5)); // 10

Here, multiply(2) returns a new function that multiplies any given number
by 2. Currying is helpful when building pipelines or reusable computation
structures, and it's a common pattern in modern JavaScript frameworks and
libraries.

13. Why is immutability important in functional programming?


Demonstrate how to create an immutable object or array.

Immutability refers to the principle that data should not be changed once
created. Instead of modifying the original object or array, you create a new one
with the updated data. This is a key aspect of functional programming because it
eliminates side effects, simplifies debugging, and improves predictability,
especially in concurrent or asynchronous systems. To create an immutable
array, for instance, you can use the spread operator:

javascript
CopyEdit
const arr = [1, 2, 3];
const newArr = [...arr, 4]; // arr remains unchanged
console.log(newArr); // [1, 2, 3, 4]

For objects:

javascript
CopyEdit
const person = { name: "Alice" };
const updatedPerson = { ...person, age: 25 };
console.log(updatedPerson); // { name: "Alice", age:
25 }

By using techniques like object and array spreading or libraries like


Immutable.js, you maintain purity in your functions and reduce bugs caused by
shared mutable state.

14. What is a Promise in JavaScript? Provide an example of creating and


using a Promise.

A Promise in JavaScript represents the eventual completion (or failure) of an


asynchronous operation and its resulting value. Promises simplify asynchronous
code by providing cleaner syntax compared to deeply nested callbacks. A
promise can be in one of three states: pending, fulfilled, or rejected. Here is an
example of creating and using a promise:

javascript
CopyEdit
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data received!");
}, 1000);
});
};

fetchData().then(data => {
console.log(data); // Output after 1 second: Data
received!
});
In this example, the fetchData function returns a promise that simulates data
fetching. When the operation completes, the resolve function is called, and
the .then() method handles the result. Promises make asynchronous code
easier to structure and read.

15. Explain how async/await works in JavaScript. Provide an example of


an asynchronous function using 'async/await'.

async/await is syntactic sugar built on top of Promises that allows you to


write asynchronous code in a synchronous-looking style. An async function
always returns a promise, and within it, you can use await to pause execution
until a promise is resolved or rejected. This makes the code easier to read and
debug, especially with complex chains of asynchronous operations.

javascript
CopyEdit
const fetchData = () => {
return new Promise((resolve) => {
setTimeout(() => resolve("Data fetched!"), 1000);
});
};

async function getData() {


const result = await fetchData();
console.log(result);
}
getData(); // Output: Data fetched! (after 1 second)

In this example, await pauses the execution inside getData() until the
promise returned by fetchData() is resolved. This approach avoids callback
hell and provides a cleaner control flow for asynchronous tasks.

16. Describe what a callback function is and how it is used in asynchronous


programming.

A callback function is a function passed as an argument to another function


and is executed after the parent function completes its task. In asynchronous
programming, callbacks are commonly used to handle operations that take time,
such as reading files or making network requests. Once the operation is
complete, the callback is executed with the result.
javascript
CopyEdit
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
}

fetchData(function(result) {
console.log(result); // Output: Data loaded
});

While callbacks were the standard way of handling async operations in earlier
JavaScript, they can lead to deeply nested and hard-to-maintain code, known as
"callback hell." This led to the evolution of Promises and async/await as cleaner
alternatives.

17. Explain the purpose of generators in asynchronous programming.


Provide an example of a generator function.

Generators are functions that can pause and resume their execution using the
yield keyword. They are defined using the function* syntax. In
asynchronous programming, generators were used before async/await to
manage asynchronous flows with libraries like co. Generators allow you to
pause the function at yield and resume later, enabling controlled execution.

javascript
CopyEdit
function* asyncFlow() {
console.log("Step 1");
yield;
console.log("Step 2");
}
const gen = asyncFlow();
gen.next(); // Step 1
gen.next(); // Step 2

Though async/await has mostly replaced generators for async tasks,


generators are still useful for implementing iterators or custom control flows,
especially when combined with other utility libraries.
18. What are classes in JavaScript? Provide an example of defining and
using a class.

A class in JavaScript is a blueprint for creating objects with shared properties


and methods. Introduced in ES6, classes provide a more familiar syntax for
object-oriented programming compared to traditional prototypes. A class
typically includes a constructor method and one or more methods.

javascript
CopyEdit
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hi, I'm ${this.name} and I'm
${this.age} years old.`);
}
}
const person1 = new Person("John", 28);
person1.greet(); // Hi, I'm John and I'm 28 years
old.

Classes enhance code organization, especially in large applications, and are


widely used in frameworks like Angular and TypeScript.

19. Explain the concept of inheritance in object-oriented programming.


Provide an example of a base class and a derived class.

Inheritance allows one class (called a child or derived class) to inherit


properties and methods from another class (the parent or base class). This
promotes code reuse and enables hierarchical relationships between objects.
JavaScript supports class inheritance using the extends keyword and allows
calling the parent constructor using super().

javascript
CopyEdit
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog("Rex");
dog.speak(); // Rex barks.

Here, Dog inherits from Animal and overrides the speak method,
demonstrating method overriding, a form of polymorphism.

20. What is encapsulation? How can encapsulation be achieved in


JavaScript using ES6 features?

Encapsulation is the concept of bundling data (properties) and methods that


operate on that data into a single unit, usually a class. It also involves restricting
direct access to some of the object's components, which is achieved using
access modifiers or conventions. In JavaScript, true private fields are supported
using the # syntax introduced in ES6.

javascript
CopyEdit
class Counter {
#count = 0;
increment() {
this.#count++;
console.log(this.#count);
}
}
const counter = new Counter();
counter.increment(); // 1
// counter.#count; // SyntaxError: Private field
'#count' must be declared in an enclosing class

By using private fields (#), encapsulation ensures internal class logic is


protected from outside interference, which leads to more secure and
maintainable code.
21. Define polymorphism and provide an example of method overriding in
JavaScript.

Polymorphism is a core concept in object-oriented programming that allows


objects of different classes to be treated as objects of a common superclass. It
enables the same interface to be used for different underlying data types or
classes, and allows methods in child classes to override methods in their parent
classes with their own behavior. In JavaScript, polymorphism is commonly
implemented using method overriding, where a subclass provides its own
implementation of a method that is already defined in its superclass.

javascript
CopyEdit
class Animal {
speak() {
console.log("Animal makes a sound.");
}
}

class Cat extends Animal {


speak() {
console.log("Cat meows.");
}
}

let myAnimal = new Animal();


let myCat = new Cat();

myAnimal.speak(); // Animal makes a sound.


myCat.speak(); // Cat meows.

Here, both Animal and Cat have a method called speak(), but the version
in Cat overrides the one in Animal. This is polymorphism in action—calling
the same method on different objects yields different behavior based on the
object's actual class.

22. Explain what TypeScript is and how it relates to JavaScript.

TypeScript is a statically typed superset of JavaScript developed by Microsoft.


It builds upon JavaScript by adding optional static typing, interfaces, enums,
and other powerful features typically found in strongly typed languages like
Java or C#. TypeScript is compiled (or "transpiled") into plain JavaScript so that
it can run in any environment that supports JavaScript, such as web browsers or
Node.js. Its main purpose is to catch errors during development, improve code
maintainability, and support large-scale applications with better tooling, such as
autocompletion, refactoring, and type checking. Since TypeScript is a superset,
all valid JavaScript code is also valid TypeScript code.

23. List three advantages of using TypeScript over plain JavaScript.

There are several advantages to using TypeScript instead of plain JavaScript.


First, TypeScript offers static typing, which helps developers catch type-
related errors at compile time rather than at runtime, thus improving code
quality and reducing bugs. Second, TypeScript provides better development
tools through IDE support, such as code completion, refactoring tools, and
intelligent navigation, which make developers more productive. Third,
TypeScript enhances code readability and maintainability in large projects
by enabling clear interfaces and strong contracts between components. These
features make TypeScript particularly useful for developing enterprise-level or
large-scale web applications.

24. Provide step-by-step instructions for installing TypeScript using npm.

To install TypeScript using Node.js and npm (Node Package Manager), follow
these simple steps:

1. Ensure Node.js is installed on your system. You can verify it using the
command:

nginx
CopyEdit
node -v

and check for npm:

nginx
CopyEdit
npm -v
2. Install TypeScript globally so it can be used from anywhere on your
system:

nginx
CopyEdit
npm install -g typescript

3. Once installed, verify the installation by checking the version:

nginx
CopyEdit
tsc -v

This command confirms that the TypeScript compiler (tsc) is ready to compile
.ts files into JavaScript. You can now begin writing and compiling TypeScript
code.

25. Write a simple TypeScript file that includes variables, functions, and
classes.

Here’s a basic TypeScript example that demonstrates variables, functions, and a


class:

typescript
CopyEdit
// app.ts

// Variables with types


let name: string = "Alice";
let age: number = 25;
let isStudent: boolean = true;

// Function with type annotations


function greet(person: string, age: number): string {
return `Hello, my name is ${person} and I am ${age}
years old.`;
}

// Class definition
class Student {
constructor(public name: string, public grade:
number) {}
display(): string {
return `${this.name} is in grade ${this.grade}.`;
}
}

let student = new Student("Bob", 10);

console.log(greet(name, age)); // Hello, my


name is Alice and I am 25 years old.
console.log(student.display()); // Bob is in
grade 10.

To run this, you compile it with tsc app.ts to get the corresponding
JavaScript file.

26. Provide examples for basic types, enums, interfaces, classes, and
generics in TypeScript.

Here are examples for different features in TypeScript:

typescript
CopyEdit
// Basic types
let count: number = 100;
let isDone: boolean = false;
let title: string = "TypeScript Basics";

// Enum
enum Direction {
Up,
Down,
Left,
Right
}
let dir: Direction = Direction.Up;

// Interface
interface Person {
name: string;
age: number;
}
// Class
class Employee implements Person {
constructor(public name: string, public age:
number, public position: string) {}

describe(): string {
return `${this.name} is a ${this.position}.`;
}
}

// Generics
function identity<T>(arg: T): T {
return arg;
}

const emp = new Employee("Raj", 30, "Developer");


console.log(emp.describe());
console.log(identity<string>("Hello")); // Output:
Hello

These examples show the power and versatility of TypeScript's type system and
how it supports clean, maintainable code.

27. Explain how modules work in TypeScript. Provide an example of


importing and exporting modules.

In TypeScript, modules are files that contain code that can be exported and
reused in other files by importing them. Modules promote code organization
and reuse, especially in large applications. You can export variables, functions,
interfaces, or classes from a module using the export keyword, and import
them elsewhere using import.

file: math.ts

typescript
CopyEdit
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14;
file: app.ts

typescript
CopyEdit
import { add, PI } from "./math";

console.log(add(5, 10)); // 15
console.log(PI); // 3.14

This modular approach helps split code into meaningful units and improves
maintainability and testability.

28. What are decorators in TypeScript? Provide an example of applying a


decorator to a class.

Decorators in TypeScript are a special kind of declaration that can be attached


to a class, method, accessor, property, or parameter. They provide a way to add
metadata or modify behavior at runtime. To use decorators, you must enable the
"experimentalDecorators" compiler option in tsconfig.json.

typescript
CopyEdit
function Logger(constructor: Function) {
console.log(`Class ${constructor.name} has been
created`);
}

@Logger
class Person {
constructor(public name: string) {}
}

When the Person class is defined, the Logger decorator runs and logs the
class creation. Decorators are widely used in Angular for metadata tagging of
classes like components, services, etc.

29. Explain type narrowing in TypeScript. Provide an example where type


narrowing is used.
Type narrowing refers to the process of refining the type of a variable within a
conditional block. TypeScript automatically narrows down types using
typeof, instanceof, or custom type guards. This allows you to safely
access specific properties or methods relevant to the narrowed type.

typescript
CopyEdit
function printValue(value: string | number) {
if (typeof value === "string") {
console.log(value.toUpperCase()); // Safe string
method
} else {
console.log(value.toFixed(2)); // Safe number
method
}
}

printValue("hello"); // Output: HELLO


printValue(123.456); // Output: 123.46

This example shows how TypeScript uses type narrowing to determine which
method is safe to use based on the runtime type of the variable.

30. Define type guards in TypeScript and provide an example of a user-


defined type guard function.

Type guards are functions that let TypeScript determine the specific type of a
variable within a conditional block. A user-defined type guard is a function that
returns a boolean and has a return type in the format arg is Type. This
helps refine the type of the variable in a type-safe way.

typescript
CopyEdit
interface Cat {
meow(): void;
}

interface Dog {
bark(): void;
}

function isCat(pet: Cat | Dog): pet is Cat {


return (pet as Cat).meow !== undefined;
}

function makeSound(pet: Cat | Dog) {


if (isCat(pet)) {
pet.meow(); // TypeScript knows pet is a Cat
} else {
pet.bark(); // Otherwise, pet is a Dog
}
}

The isCat() function is a user-defined type guard that helps TypeScript


correctly infer the type within if blocks, enhancing both safety and clarity.

Chapter 3

1. What is AngularJS? Provide a brief overview of AngularJS and its main


features.

AngularJS is a powerful, open-source JavaScript framework developed by


Google for building dynamic web applications. It extends HTML with
additional attributes and binds data to HTML using expressions. AngularJS is
based on the Model-View-Controller (MVC) architecture, which allows for
clean separation of concerns and better organization of code. One of the key
strengths of AngularJS lies in its ability to create single-page applications
(SPAs), where the content is dynamically loaded and updated without
refreshing the entire page. Main features include two-way data binding,
dependency injection, reusable components, directives, and routing. These
features make AngularJS an efficient framework for building scalable,
maintainable, and highly interactive web applications.
2. List and explain three key features of AngularJS.

Three key features of AngularJS are:

 Two-Way Data Binding: This feature automatically synchronizes the


data between the model (JavaScript objects) and the view (HTML),
meaning any changes in the UI reflect in the model and vice versa
without the need for manual DOM manipulation.
 Dependency Injection: AngularJS has a built-in dependency injection
system that allows developers to inject services and objects into
components, controllers, and other parts of the application, making the
code more modular, testable, and easier to maintain.
 Directives: These are custom HTML attributes that extend the
functionality of HTML. AngularJS comes with several built-in directives
such as ng-model, ng-repeat, and ng-if, which simplify the
creation of dynamic content and enable powerful behaviors directly in the
HTML templates.

3. Explain the process of creating components in an AngularJS application.


Provide the commands to create three components named home, about,
and dashboard.

In AngularJS, components are used to encapsulate HTML templates, logic, and


styles into reusable units. While AngularJS (the original 1.x version) does not
use the Angular CLI, modern Angular (Angular 2+) does. Assuming you're
referring to modern Angular development, you can create components using the
Angular CLI. First, ensure the Angular CLI is installed, then use the ng
generate component or ng g c command.

bash
CopyEdit
ng generate component home
ng generate component about
ng generate component dashboard

Each command generates a new folder with four files: the TypeScript class,
HTML template, CSS styles, and test specification file. These components can
then be used inside other templates using their selectors.
4. How is routing and navigation implemented in AngularJS? Provide a
step-by-step explanation of the process.

Routing in AngularJS allows you to navigate between different views or


components within a single-page application without reloading the page. To
implement routing:

1. Install and import the RouterModule: In modern Angular, import


RouterModule from @angular/router in the main module file.
2. Define routes: Create an array of route objects, where each route has a
path and a corresponding component.
3. Configure RouterModule: Use
RouterModule.forRoot(routes) in the imports array of your
module.
4. Add <router-outlet>: Insert <router-outlet> in the main
HTML where the routed component should render.
5. Use routerLink: Use [routerLink] directive in anchor tags or
buttons to navigate.

Example:

typescript
CopyEdit
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'dashboard', component: DashboardComponent
}
];
html
CopyEdit
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
<router-outlet></router-outlet>

This setup enables navigation between components without full-page reloads,


making the application feel faster and more interactive.

5. Describe the concept of forms and validation in AngularJS. Provide


examples of different form controls and their usage.
In AngularJS, forms and validation are critical for building robust and
interactive web applications. Angular provides both template-driven and
reactive forms. In template-driven forms, the form logic is defined in the
HTML using directives like ngModel, required, and ngForm, while
reactive forms are defined programmatically using FormBuilder and
FormGroup. Validation can be both built-in (such as required,
minlength, maxlength, email) and custom.

Example of a template-driven form:

html
CopyEdit
<form #userForm="ngForm">
<input name="username" ngModel required />
<input name="email" ngModel email />
<button
[disabled]="!userForm.valid">Submit</button>
</form>

Angular marks fields as valid or invalid automatically, and you can show error
messages accordingly. This system helps ensure that only properly validated
data is submitted, improving the reliability and integrity of the application.

6. What is the significance of the Router outlet in AngularJS? Explain its


role in the application's structure.

In AngularJS (Angular 2+), the <router-outlet> directive is a placeholder


that Angular dynamically fills based on the current route. It acts as a container
for routed components, meaning when the user navigates using a route like
/home, the corresponding HomeComponent is rendered inside the
<router-outlet>. This allows developers to define a master layout (like a
header, footer, and side navigation) and swap only the content part dynamically.
It plays a key role in enabling single-page application behavior, where
navigating between views doesn't require reloading the page, offering a
seamless user experience.

7. Discuss the usage of various pipes in AngularJS, such as async, currency,


date, and decimal pipes. Provide examples of each pipe in action.

Pipes in Angular transform data in templates. Commonly used pipes include:


 Async Pipe: Subscribes to an Observable or Promise and returns the
latest emitted value.

html
CopyEdit
<p>{{ observableData | async }}</p>

 Currency Pipe: Formats a number as currency.

html
CopyEdit
<p>{{ 1234.56 | currency:'USD' }}</p> <!-- Output:
$1,234.56 -->

 Date Pipe: Formats a date value.

html
CopyEdit
<p>{{ today | date:'fullDate' }}</p>

 Decimal Pipe: Formats numbers with decimal precision.

html
CopyEdit
<p>{{ 3.141592 | number:'1.2-2' }}</p> <!-- Output:
3.14 -->

These pipes are used in templates to format and display data more cleanly and
are essential for localization and UI enhancement.

8. How does AngularJS facilitate easy integration through a component-


based framework? Explain the concept of a component repository and its
benefits.

AngularJS uses a component-based architecture, where the UI is broken


down into small, reusable pieces called components. Each component has its
own logic, template, and styles. This modular design makes integration and
collaboration easier, as components can be developed and tested independently.
A component repository refers to a central collection or library of reusable
components (like buttons, forms, charts, etc.) that can be shared across projects.
This promotes consistency, reusability, and faster development since developers
don’t have to build every UI element from scratch. It also helps enforce
standards and reduce duplication across teams and projects.

9. What is the role of a controller in AngularJS? Provide an example of a


controller function and its usage.

In AngularJS (version 1.x), a controller is a JavaScript function that controls


the data of an Angular application. It acts as a glue between the view and the
model, handling the business logic and exposing data via $scope (in
AngularJS 1.x). The controller responds to user input, manipulates data, and
updates the view accordingly.

Example:

javascript
CopyEdit
app.controller('MyController', function($scope) {
$scope.greeting = "Hello, AngularJS!";
$scope.sayHello = function() {
alert($scope.greeting);
};
});

In the HTML:

html
CopyEdit
<div ng-controller="MyController">
<p>{{greeting}}</p>
<button ng-click="sayHello()">Greet</button>
</div>

This structure keeps the logic organized and modular, aligning with the MVC
pattern.

10. What are services in AngularJS? Explain the difference between a


factory and a service.

Services in AngularJS are singleton objects that provide functionality and logic
shared across components or controllers. They are used for tasks like API
communication, data management, or utility functions. AngularJS supports
different ways to create services: using .service(), .factory(), or
.provider().

 Factory returns an object explicitly from a function.

javascript
CopyEdit
app.factory('MathService', function() {
return {
square: function(a) {
return a * a;
}
};
});

 Service uses a constructor function and this to define properties and


methods.

javascript
CopyEdit
app.service('GreetingService', function() {
this.sayHello = function(name) {
return "Hello, " + name;
};
});

In general, factories are more flexible, while services are simpler and closer to
object-oriented programming style.

11. What are filters in AngularJS? Provide examples of different types of


filters and their usage.

Filters in AngularJS (v1.x) are used to format the value of an expression for
display to the user. They can be applied in view templates using the pipe (|)
symbol. Common filters include:

 uppercase and lowercase:

html
CopyEdit
<p>{{ name | uppercase }}</p>
 currency:

html
CopyEdit
<p>{{ amount | currency }}</p>

 date:

html
CopyEdit
<p>{{ currentDate | date:'shortDate' }}</p>

 filter (to filter a list):

html
CopyEdit
<li ng-repeat="item in items | filter:searchText">{{
item.name }}</li>

These filters improve the presentation layer by formatting data without


modifying the underlying model.

12. Briefly explain what AngularJS is and list three of its key features.

AngularJS is a JavaScript framework developed by Google for building


dynamic single-page web applications. It extends the capabilities of HTML by
adding directives, data binding, and dependency injection. Unlike traditional
server-side rendered apps, AngularJS allows for interactive and responsive user
interfaces with seamless navigation. Three of its key features are:

 Two-Way Data Binding: Synchronizes the model and view


automatically.
 Directives: Custom HTML attributes that enhance functionality.
 Dependency Injection: Helps manage and inject services efficiently,
making code modular and testable.

These features make AngularJS a powerful tool for developing modern web
applications.
Chapter 4

1. What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment built


on the Google Chrome V8 JavaScript engine. It allows developers to run
JavaScript code outside the browser, typically on the server side. Node.js uses
an event-driven, non-blocking I/O model, making it lightweight and
efficient—especially for data-intensive real-time applications that run across
distributed devices. Unlike traditional server environments that create a new
thread for each request, Node.js handles multiple requests on a single thread,
using asynchronous callbacks and an event loop. This makes it ideal for
building scalable web servers, APIs, real-time chat applications, streaming
applications, and more.

2. Explain different features of Node.js.

Node.js offers a variety of powerful features that make it a preferred choice for
modern web development. First and foremost, it is asynchronous and event-
driven, meaning that operations like reading files, querying databases, and
handling HTTP requests are non-blocking, improving performance under heavy
loads. Another major feature is fast execution, made possible by the V8 engine,
which compiles JavaScript to native machine code. Node.js also supports
single-threaded but highly scalable architecture, using an event loop to
handle concurrent connections. Additionally, Node.js comes with built-in
modules (such as fs, http, and path) and a vast ecosystem of third-party
modules accessible via NPM (Node Package Manager). It supports cross-
platform development and is ideal for building REST APIs, microservices, and
even desktop applications when used with frameworks like Electron.

3. State different advantages of Node.js.

Node.js brings several advantages that make it ideal for building fast and
scalable server-side applications. The foremost advantage is its asynchronous,
non-blocking nature, which allows handling multiple requests without waiting
for I/O operations to complete. This increases throughput and performance.
Secondly, JavaScript everywhere—both on the client and server side—means
unified development, allowing full-stack development using a single language.
The NPM ecosystem is another strength, providing access to millions of open-
source libraries that speed up development. Node.js is also lightweight and
highly scalable, making it perfect for real-time applications like chat servers,
online gaming, and collaborative tools. Its large community and strong
corporate backing by companies like Google and Microsoft contribute to its
continuous growth and improvement.

4. Explain Traditional Web Server Model.

The traditional web server model, such as that used by Apache or IIS, follows
a multi-threaded approach. Each incoming request from a client (e.g., a
browser) is assigned its own thread on the server. If a server receives 1000
concurrent requests, it spawns 1000 threads, each requiring memory and
processing power. If a thread is waiting for a file read or database operation, it
remains blocked, consuming system resources. This model works well for
applications with low concurrency but struggles with performance and
scalability under heavy loads, as system resources become a bottleneck. In high-
traffic scenarios, spawning too many threads can cause delays and crashes due
to thread exhaustion.

5. Explain Node.js Process Model.

The Node.js process model uses a single-threaded event loop architecture to


handle multiple concurrent client requests efficiently. Unlike traditional web
servers that use separate threads for each request, Node.js operates on a single
thread, utilizing non-blocking I/O operations to handle multiple requests in
parallel. When a request is received, Node.js delegates time-consuming tasks
(like file I/O or database access) to background threads in the libuv thread
pool, allowing the main thread to continue processing other requests. Once the
task is complete, the result is passed back through a callback function, which
the event loop picks up. This design makes Node.js highly scalable and
resource-efficient, suitable for I/O-bound applications and real-time systems.

6. Explain Node.js Event Loop with an Example.


The event loop is the core mechanism in Node.js that enables non-blocking I/O
operations. It continuously monitors the call stack and the callback queue,
executing callbacks as soon as the stack is clear. When Node.js executes
asynchronous functions like fs.readFile() or setTimeout(), they are
sent to the background and upon completion, their callbacks are queued. The
event loop picks these callbacks and executes them sequentially.

Example:

javascript
CopyEdit
console.log("Start");

setTimeout(() => {
console.log("Inside timeout");
}, 2000);

console.log("End");

Output:

sql
CopyEdit
Start
End
Inside timeout

This shows that even though setTimeout is declared early, the event loop
allows the rest of the code to execute first while it waits, demonstrating non-
blocking behavior.

7. What is a Module? Explain different types of modules.

A module in Node.js is a reusable block of code whose existence does not


impact other parts of the program. Node.js uses the CommonJS module system,
where each file is treated as a separate module. Modules can encapsulate
functionality, export objects, and be imported using require(). There are
three types of modules in Node.js:

1. Core Modules: Built-in modules like fs, http, path, and os that
come with Node.js.
2. Local Modules: User-defined modules created within your application
using module.exports.
3. Third-party Modules: Modules installed via NPM such as express,
lodash, or mongoose.

This modular architecture helps in code organization, reusability, and


maintenance.

8. What is Core Module? Explain with an Example.

Core modules are the built-in modules provided by Node.js that require no
installation. These modules are essential for many tasks, such as file handling
(fs), networking (http), and path resolution (path). Since they are compiled
into the Node.js binary, they load faster and are optimized for performance.

Example using the fs core module:

javascript
CopyEdit
const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {


if (err) throw err;
console.log(data);
});

In this example, the fs module is used to read a file asynchronously. This


demonstrates the utility of core modules in simplifying server-side operations
without requiring any additional libraries.

9. What is Local Module? Explain with an Example.

A local module is a user-defined module created within the same project. You
define a local module by placing your code in a separate file and exposing the
desired functions or objects using module.exports. You then import the
module using require().

Example:

math.js (Local module):


javascript
CopyEdit
function add(a, b) {
return a + b;
}

module.exports = { add };

app.js:

javascript
CopyEdit
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5

This shows how code can be modularized and reused across different files using
local modules, improving code maintainability.

10. How to Use Directories as a Module

In Node.js, a directory can be treated as a module if it contains an


index.js file or if a main file is specified in the package.json. When
you require() a directory, Node.js looks for an index.js file in that
directory by default and executes it. This is useful for organizing complex
modules across multiple files.

Structure:

pgsql
CopyEdit
myModule/
├── index.js
├── utils.js

index.js:

javascript
CopyEdit
const util = require('./utils');
module.exports = util;

utils.js:
javascript
CopyEdit
exports.sayHello = () => {
return "Hello!";
};

Using the directory as a module:

javascript
CopyEdit
const myModule = require('./myModule');
console.log(myModule.sayHello()); // Output: Hello!

This approach improves modularity and keeps related functionalities


encapsulated within a directory.

11. Explain module.exports.

module.exports is the object that is actually returned as the result of a


require() call in Node.js. Every JavaScript file in Node.js is treated as a
module, and this object is used to export properties (functions, objects,
variables, etc.) from that module so they can be used in other files. By default,
module.exports is an empty object, but you can add properties or assign it
a value to expose that value to other modules.

Example:

javascript
CopyEdit
// math.js
module.exports.add = function(a, b) {
return a + b;
};

// app.js
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5

In this case, module.exports exposes the add function, making it available


in app.js when imported.

12. Explain different ways of using module.exports.


There are several ways to use module.exports to export data in Node.js:

1. Exporting an Object:
You can export an object with multiple properties or functions.

javascript
CopyEdit
// math.js
module.exports = {
add: function(a, b) { return a + b; },
subtract: function(a, b) { return a - b; }
};
javascript
CopyEdit
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 3)); // Output: 2

2. Exporting a Function:
You can export a single function directly.

javascript
CopyEdit
// greet.js
module.exports = function(name) {
return `Hello, ${name}!`;
};
javascript
CopyEdit
const greet = require('./greet');
console.log(greet('John')); // Output: Hello,
John!

3. Exporting a Value:
You can assign a single value to module.exports, such as a number
or a string.

javascript
CopyEdit
// number.js
module.exports = 42;
javascript
CopyEdit
const num = require('./number');
console.log(num); // Output: 42

13. What is asynchronous programming in Node.js?

Asynchronous programming in Node.js refers to the ability to execute


operations without blocking the execution thread, allowing other tasks to run
concurrently. In traditional, synchronous programming, each operation must
complete before the next begins. In contrast, asynchronous programming
enables non-blocking I/O operations, which improves efficiency and scalability.
In Node.js, asynchronous functions such as fs.readFile(), http.get(),
or database queries do not block the execution of the program. Instead, they
accept a callback function that is executed once the operation completes.

Example using fs.readFile():

javascript
CopyEdit
const fs = require('fs');

fs.readFile('example.txt', 'utf8', function(err,


data) {
if (err) throw err;
console.log(data); // This will execute only after
the file has been read
});
console.log("File read in progress...");

In this example, "File read in progress..." is logged before the file reading
completes, showing the non-blocking behavior.

14. What is a callback function in Node.js?

A callback function is a function that is passed as an argument to another


function and is executed after the completion of that function's task. In Node.js,
callbacks are heavily used in asynchronous operations like reading files, making
HTTP requests, or handling events. This allows the program to continue
executing while waiting for the task to finish, without blocking the main
execution thread.

Example:
javascript
CopyEdit
function greeting(name, callback) {
console.log(`Hello, ${name}!`);
callback();
}

function sayGoodbye() {
console.log('Goodbye!');
}

greeting('Alice', sayGoodbye); // Output: Hello,


Alice! \n Goodbye!

Here, sayGoodbye is the callback function executed after the greeting.

15. Explain promises in Node.js.

A Promise is an object that represents the eventual completion (or failure) of an


asynchronous operation and its resulting value. Promises allow you to work
with asynchronous code in a more structured way than using callbacks.
Promises have three states:

 Pending: The operation is still in progress.


 Fulfilled: The operation completed successfully.
 Rejected: The operation failed.

Promises provide .then() and .catch() methods to handle the fulfilled


and rejected states, respectively.

Example:

javascript
CopyEdit
function getData() {
return new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Data received successfully!");
} else {
reject("Error retrieving data");
}
});
}

getData()
.then(response => console.log(response)) //
Output: Data received successfully!
.catch(error => console.log(error)); // Error:
Error retrieving data

16. How are asynchronous errors handled in Node.js?

In Node.js, asynchronous errors are typically handled by passing them to the


callback function or using Promise rejection or async/await syntax. Since
asynchronous operations don't throw errors directly, the error is often passed as
the first argument to the callback function. In case of promises, errors are
handled in the .catch() block. If using async/await, try-catch blocks can
be used for error handling.

Example:

javascript
CopyEdit
const fs = require('fs');

fs.readFile('nonexistentfile.txt', 'utf8',
function(err, data) {
if (err) {
console.error('Error:', err); // Error handling
for async functions
return;
}
console.log(data);
});

In this example, if the file does not exist, an error is logged in the callback
function.

17. Explain async/await in Node.js.

async and await are modern JavaScript features that simplify working with
Promises and asynchronous code. The async keyword is used to declare a
function as asynchronous, and within this function, you can use await to pause
the execution of the function until a Promise is resolved or rejected.

Example:

javascript
CopyEdit
async function fetchData() {
let data = await getData(); // Wait for the
promise to resolve
console.log(data);
}

fetchData();

function getData() {
return new Promise(resolve => {
setTimeout(() => resolve("Data received!"),
2000);
});
}

In this example, await pauses the execution of fetchData() until


getData() resolves, making the asynchronous flow look synchronous.

18. What is the event loop in Node.js?

The event loop is a fundamental component of Node.js's asynchronous, non-


blocking architecture. It continuously checks the call stack and the callback
queue. If the call stack is empty, it moves events from the callback queue to the
call stack for execution. This allows Node.js to handle numerous I/O operations
concurrently in a single thread, making it highly efficient for I/O-bound tasks.
The event loop runs through several phases, including timers, I/O callbacks,
idle, and check phases, ensuring that asynchronous operations are processed
correctly.

19. What is NPM?

NPM (Node Package Manager) is the default package manager for Node.js,
used for managing JavaScript libraries, tools, and modules. It allows developers
to install, share, and manage dependencies in their Node.js projects. NPM
maintains an extensive registry of open-source packages that can be installed
using commands like npm install <package-name>. Additionally, npm
can be used to publish packages to the registry, manage package versions, and
automate script execution.

20. How to install packages locally?

To install packages locally in a Node.js project, you use the npm install
<package-name> command. This installs the package in the
node_modules directory within your project folder, making it available to
your project. Local installations are saved as dependencies in the
package.json file.

Example:

bash
CopyEdit
npm install express --save

This command installs the express package locally and adds it to the
dependencies section of your package.json file.

21. How to add dependencies in package.json file?

In Node.js, dependencies are libraries or packages that your project needs to


function properly. You can add dependencies to the package.json file by
either manually editing the file or using NPM commands.

To add a dependency manually, you can open your package.json file and
add the dependency under the dependencies section:

json
CopyEdit
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1"
}
}
Alternatively, to add dependencies using the terminal, you can run the following
command:

bash
CopyEdit
npm install express --save

This will add express as a dependency in your package.json file, under


the dependencies section.

22. How to manage dependencies?

Managing dependencies in Node.js is a critical task to ensure that your project


has the correct libraries with the right versions. NPM helps manage
dependencies with several commands:

1. Installing Dependencies:
o Use npm install to install dependencies from the
package.json file.
o For specific packages, use npm install <package-name>.
2. Saving Dependencies:
o By default, npm install saves installed packages in the
node_modules folder and updates package.json to reflect
the installed versions under the dependencies section.
3. Updating Dependencies:
o To update a specific dependency, use npm update
<package-name>.
o To update all dependencies, use npm update.
4. Removing Dependencies:
o To remove a package, use npm uninstall <package-
name>. This will remove the package from the node_modules
folder and the package.json file.
5. Checking for Vulnerabilities:
o Use npm audit to check for vulnerabilities in your
dependencies.

23. What are streams in Node.js?


Streams in Node.js are objects that allow reading from or writing to a
continuous flow of data. Streams are an essential part of Node.js’s architecture,
designed to handle large amounts of data efficiently without loading it entirely
into memory. There are four main types of streams in Node.js: Readable,
Writable, Duplex, and Transform.

1. Readable Streams: Used for reading data. Example:


fs.createReadStream().
2. Writable Streams: Used for writing data. Example:
fs.createWriteStream().
3. Duplex Streams: Streams that can both read and write. Example: TCP
sockets.
4. Transform Streams: A type of duplex stream where the output is
computed based on the input. Example: zlib.createGzip().

24. What are the advantages of using streams?

The primary advantages of using streams in Node.js are:

1. Memory Efficiency: Streams do not require the entire data to be loaded


into memory, making them ideal for handling large files or continuous
data.
2. Performance: Streams allow data to be processed and passed along as it
is received, improving performance and response time.
3. Non-blocking: Streams work asynchronously, allowing Node.js to handle
other tasks while waiting for data.
4. Backpressure Handling: Streams can handle backpressure, ensuring that
data is only processed when the destination is ready to receive more.

25. What are the four main types of streams in Node.js?

Node.js provides four main types of streams:

1. Readable Streams: Streams that allow reading data. For example,


reading from files, HTTP responses, or process outputs.
Example:

javascript
CopyEdit
const fs = require('fs');
const readableStream =
fs.createReadStream('example.txt', 'utf8');
readableStream.on('data', (chunk) => {
console.log(chunk);
});

2. Writable Streams: Streams that allow writing data. For example, writing
to files, HTTP requests, or logging systems.
Example:

javascript
CopyEdit
const fs = require('fs');
const writableStream =
fs.createWriteStream('output.txt');
writableStream.write('Hello, world!\n');
writableStream.end();

3. Duplex Streams: Streams that can read and write. Example: TCP socket
connections.
Example:

javascript
CopyEdit
const net = require('net');
const server = net.createServer((socket) => {
socket.write('Welcome to the server!');
socket.on('data', (data) => {
console.log(data.toString());
});
});
server.listen(8080);

4. Transform Streams: A type of duplex stream that modifies the data as it


is read or written. Example: compression streams (e.g., gzip).
Example:

javascript
CopyEdit
const zlib = require('zlib');
const fs = require('fs');
const input = fs.createReadStream('input.txt');
const output =
fs.createWriteStream('output.txt.gz');
const gzip = zlib.createGzip();

input.pipe(gzip).pipe(output);

26. What events can be used with streams?

Streams in Node.js emit various events during their lifecycle. Some of the key
events are:

1. data: Emitted when there is data to be read from a readable stream.


2. end: Emitted when the stream has finished reading or writing.
3. error: Emitted when an error occurs in the stream.
4. finish: Emitted when a writable stream has finished writing all the
data.
5. close: Emitted when a stream has been closed.

Example:

javascript
CopyEdit
const fs = require('fs');
const readableStream =
fs.createReadStream('example.txt', 'utf8');
readableStream.on('data', (chunk) => {
console.log('Reading:', chunk);
});
readableStream.on('end', () => {
console.log('Stream ended.');
});

27. How can you read data from a file using streams?

To read data from a file using streams in Node.js, you can use the
fs.createReadStream() function. This method allows you to read large
files in chunks without loading the entire file into memory, making it more
memory-efficient.

Example:

javascript
CopyEdit
const fs = require('fs');
const readableStream =
fs.createReadStream('example.txt', 'utf8');
readableStream.on('data', (chunk) => {
console.log(chunk); // Output the chunk being read
});
readableStream.on('end', () => {
console.log('File reading completed.');
});

28. How can you write data to a file using streams?

To write data to a file using streams in Node.js, you use the


fs.createWriteStream() function. This creates a writable stream that
you can use to write data to a file.

Example:

javascript
CopyEdit
const fs = require('fs');
const writableStream =
fs.createWriteStream('output.txt');
writableStream.write('Hello, world!\n');
writableStream.write('Writing data to file...\n');
writableStream.end();

In this example, the writableStream.write() method is used to write


data to the file, and writableStream.end() signifies the end of the
writing operation.

29. What is piping in Node.js streams?

Piping is the process of passing data from one stream to another. Node.js
provides the pipe() method to pass the readable stream's data to a writable
stream, simplifying the process and reducing the need for event listeners.

Example:

javascript
CopyEdit
const fs = require('fs');
const zlib = require('zlib');

const input = fs.createReadStream('input.txt');


const output = fs.createWriteStream('output.txt.gz');
const gzip = zlib.createGzip();

// Piping input data through gzip and into the output


file
input.pipe(gzip).pipe(output);

In this example, the data from input.txt is read, compressed using gzip,
and then written to output.txt.gz.

30. What is chaining of streams?

Chaining of streams refers to the process of connecting multiple streams


together, where the output of one stream is passed as input to another. This is
done using the pipe() method, which can be chained to multiple streams in
sequence.

Example:

javascript
CopyEdit
const fs = require('fs');
const zlib = require('zlib');
const { Transform } = require('stream');

const input = fs.createReadStream('input.txt');


const output = fs.createWriteStream('output.txt.gz');
const gzip = zlib.createGzip();

// Example of stream chaining


input.pipe(gzip).pipe(output);

In this example, the input stream is piped through the gzip transform stream
and then piped to the output writable stream.

31. How can piping and chaining be implemented with streams?


Piping and chaining streams in Node.js is implemented by using the pipe()
method, which allows you to send data from one stream to another. When
multiple streams are involved, you can chain them together by calling pipe()
multiple times.

Example:

javascript
CopyEdit
const fs = require('fs');
const zlib = require('zlib');
const { Transform } = require('stream');

const input = fs.createReadStream('input.txt');


const output = fs.createWriteStream('output.txt.gz');
const gzip = zlib.createGzip();

// Chaining streams to compress and write to a file


input.pipe(gzip).pipe(output);

In this case, the data flows through multiple streams in sequence: first the file is
read, then it’s compressed, and finally, it’s written to a new file.

Chapter 5

1. Describe features of Express JS.

Express.js is a lightweight, fast, and flexible Node.js framework designed to


build web applications and APIs. It simplifies routing, middleware handling,
and the creation of dynamic web pages. Key features of Express.js include:

 Routing: Express provides robust routing mechanisms for HTTP


requests, allowing developers to easily manage different routes and
methods (GET, POST, PUT, DELETE).
 Middleware Support: It offers extensive middleware support, allowing
you to perform tasks like logging, authentication, body parsing, error
handling, etc.
 Template Engines: Express can be integrated with various templating
engines like EJS, Pug, and Handlebars to render dynamic views.
 Request and Response Handling: Simplifies the management of HTTP
requests and responses, providing useful methods to handle parameters,
headers, and responses.
 Modular and Extensible: With its modular design, Express.js supports a
wide variety of third-party middleware packages, helping in building
scalable applications.
 Support for RESTful APIs: Express makes it easy to build RESTful
web services using its route-based structure.
 Error Handling: It offers an error-handling mechanism that allows you
to define middleware to handle different types of errors.

2. Explain routing with an example.

In Express.js, routing refers to defining the paths and handling HTTP requests
made to those paths. Routing defines how an application responds to client
requests for specific URLs.

Example of routing:

javascript
CopyEdit
const express = require('express');
const app = express();

// Define a GET route


app.get('/', (req, res) => {
res.send('Hello World');
});

// Define a POST route


app.post('/submit', (req, res) => {
res.send('Form submitted!');
});

// Start the server


app.listen(3000, () => {
console.log('Server running on port 3000');
});
In this example, when a user visits the root URL (/), they receive a "Hello
World" response, and when they send a POST request to /submit, they get a
confirmation message.

3. What is ExpressJS and how does it relate to Node.js?

ExpressJS is a minimal and flexible Node.js web application framework that


simplifies the process of building server-side applications. It provides powerful
routing capabilities, middleware integration, and support for view templates.
While Node.js provides the core features for building servers and handling
HTTP requests, Express builds on top of Node.js to streamline the creation of
web applications. It abstracts the complexities of working directly with Node.js'
HTTP module and provides a more developer-friendly approach to building
applications and APIs.

4. Explain the significance of using ExpressJS in web development.

ExpressJS significantly improves web development by offering a lightweight


framework that accelerates the development of web applications and APIs. The
major significance of using ExpressJS includes:

 Simplification: Express abstracts the complexities of Node.js, offering a


simple and minimalistic API to work with HTTP requests, responses, and
routing.
 Middleware Integration: With Express, handling requests is easier
through the use of middleware functions. Middleware allows developers
to easily add functionalities such as authentication, logging, data parsing,
etc.
 Rapid Development: Express provides a faster setup for building web
applications, reducing the boilerplate code required for managing HTTP
requests and responses.
 Scalability: Express provides a scalable architecture for building
RESTful APIs, making it a top choice for building microservices and
distributed applications.
 Extensibility: Express supports a wide range of plugins and third-party
libraries, making it highly customizable.

5. Define the MVC pattern in the context of web development.


The MVC (Model-View-Controller) pattern is a software design pattern used
to separate concerns in an application. It divides the application into three main
components:

1. Model: Represents the data or business logic of the application. It is


responsible for interacting with the database, validating data, and
performing any necessary business logic.
2. View: Represents the user interface (UI). It is responsible for rendering
the UI based on the data from the model.
3. Controller: Acts as an intermediary between the Model and View. It
receives user input from the View, processes it (sometimes modifying the
Model), and updates the View.

The MVC pattern helps in organizing code by separating the business logic
(Model), user interface (View), and application flow (Controller), making
applications easier to maintain and scale.

6. How does ExpressJS facilitate the implementation of the MVC pattern?

Express.js facilitates the implementation of the MVC pattern by providing an


easy way to define routes (Controllers), interact with data models (Model), and
render views (View) within an application. In Express, the routes correspond to
the Controller, which processes requests and sends responses. Views can be
rendered using template engines like Pug, EJS, or Handlebars, and models can
be defined using databases such as MongoDB, MySQL, or PostgreSQL.

For example, an ExpressJS application might look like this:

 Model: Interacts with MongoDB to fetch user data.


 View: Renders HTML views using the EJS template engine.
 Controller: Defines routes that handle HTTP requests and use the Model
to interact with the database.

This separation makes it easier to manage different aspects of the application


and ensures a clean architecture.

7. Provide examples of components in the MVC pattern and their roles in


an ExpressJS application.

In the context of Express.js:


1. Model: Represents the application's data structure. For instance, if we are
creating a user management system, the model might interact with a
database to fetch, store, or update user data.
Example:

javascript
CopyEdit
// Model (User.js)
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: String,
email: String,
});
const User = mongoose.model('User', UserSchema);
module.exports = User;

2. View: Represents the presentation layer. In Express.js, views are usually


rendered using template engines like EJS or Pug. The View shows the
data that is passed from the Controller.
Example:

javascript
CopyEdit
// View (EJS template)
<h1>Hello, <%= user.name %>!</h1>

3. Controller: Acts as an intermediary between the Model and the View. It


handles HTTP requests, fetches data from the Model, and sends the data
to the View.
Example:

javascript
CopyEdit
// Controller (UserController.js)
const User = require('./models/User');
const express = require('express');
const router = express.Router();

router.get('/user/:id', async (req, res) => {


const user = await
User.findById(req.params.id);
res.render('user', { user });
});
module.exports = router;

8. What is routing in the context of web applications?

Routing in web applications refers to the mechanism that defines how HTTP
requests (such as GET, POST, PUT, DELETE) are handled by the server. It
determines what code or function should be executed when a particular URL or
endpoint is accessed. Routing helps map URLs to specific actions, making it an
essential part of any web application. It is responsible for handling the incoming
requests and sending appropriate responses back to the client.

9. How does ExpressJS handle routing?

ExpressJS handles routing by defining different route handlers for specific


HTTP methods and URL patterns. Routes in Express are defined using methods
like get(), post(), put(), delete(), etc., which correspond to the
HTTP methods.

Example:

javascript
CopyEdit
const express = require('express');
const app = express();

// Define a GET route


app.get('/home', (req, res) => {
res.send('Welcome to the Home Page');
});

// Define a POST route


app.post('/submit', (req, res) => {
res.send('Form Submitted!');
});

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

In this example, ExpressJS defines a GET route for /home and a POST route
for /submit, handling different HTTP requests accordingly.
10. Explain the difference between route parameters and query parameters
in ExpressJS.

 Route Parameters: These are parts of the URL that act as placeholders
for values. Route parameters are defined in the URL path and are
typically used for identifying a specific resource. They are accessible via
req.params.

Example:

javascript
CopyEdit
app.get('/user/:id', (req, res) => {
const userId = req.params.id; // Accessing
route parameter
res.send(`User ID is ${userId}`);
});

Here, :id is a route parameter.

 Query Parameters: These are part of the URL that comes after a
question mark and are typically used to filter or sort results. Query
parameters are accessed via req.query.

Example:

javascript
CopyEdit
app.get('/search', (req, res) => {
const searchTerm = req.query.term; //
Accessing query parameter
res.send(`Searching for ${searchTerm}`);
});

Here, term is a query parameter.

11. Describe the role of HTTP requests and responses in web development.

HTTP requests and responses form the foundation of communication between


clients (like web browsers) and servers.
 HTTP Requests: These are sent from the client to the server to request
specific resources (e.g., HTML pages, images, data from APIs). They
include information like the HTTP method (GET, POST, PUT,
DELETE), URL, headers, and optionally a body.
 HTTP Responses: After processing the request, the server sends an
HTTP response back to the client. This response includes a status code
(e.g., 200 OK, 404 Not Found), headers, and optionally a body containing
the requested resource (e.g., HTML, JSON data).

The interaction between HTTP requests and responses enables clients to retrieve
and submit data to servers.

12. How does ExpressJS handle different types of HTTP requests (GET,
POST, etc.)?

ExpressJS provides methods like get(), post(), put(), delete(), etc.,


to handle different HTTP request types. These methods define route handlers
for specific HTTP methods, allowing you to perform actions based on the
request type.

For example:

javascript
CopyEdit
const express = require('express');
const app = express();

// Handle GET request


app.get('/home', (req, res) => {
res.send('GET request to the homepage');
});

// Handle POST request


app.post('/submit', (req, res) => {
res.send('POST request to submit data');
});

// Handle PUT request


app.put('/update', (req, res) => {
res.send('PUT request to update data');
});

// Handle DELETE request


app.delete('/delete', (req, res) => {
res.send('DELETE request to remove data');
});

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

Here, the application listens for GET, POST, PUT, and DELETE requests on
different routes and provides responses accordingly.

13. Explain the concept of middleware in the context of processing HTTP


requests and responses.

Middleware in ExpressJS refers to functions that have access to the request


object (req), the response object (res), and the next() function in the
request-response cycle. Middleware can modify the request and response,
perform additional tasks (like logging, validation, or authentication), and pass
control to the next middleware in the stack. Middleware is executed sequentially
for every incoming request.

Example of a middleware:

javascript
CopyEdit
const express = require('express');
const app = express();

// Custom middleware
app.use((req, res, next) => {
console.log('Middleware executed');
next(); // Pass control to the next middleware
});

app.get('/', (req, res) => {


res.send('Hello, world!');
});

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

In this example, the middleware logs every incoming request before the request
reaches the route handler.

14. What is middleware in ExpressJS, and why is it essential?

In ExpressJS, middleware is a function that performs operations on the request


and response objects. Middleware is essential because it allows developers to
handle repetitive tasks such as logging, authentication, error handling, parsing
JSON data, and more in a modular and reusable manner. Middleware functions
are executed in the order they are defined, giving developers control over the
request-response cycle.

Middleware can be used for:

 Error Handling: Catching and handling errors in a structured way.


 Request Validation: Validating user inputs before the route handler
processes them.
 Authentication: Ensuring only authorized users can access specific
routes.
 Logging: Tracking request and response data for debugging purposes.

Example of middleware for logging requests:

javascript
CopyEdit
app.use((req, res, next) => {
console.log(`Request method: ${req.method}, URL:
${req.url}`);
next();
});

15. Provide examples of common middleware functions used in ExpressJS.

Some of the most common middleware functions used in ExpressJS include:

1. Body Parser: To parse incoming request bodies (usually for POST or


PUT requests).

javascript
CopyEdit
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // Parse JSON bodies
app.use(bodyParser.urlencoded({ extended: true
})); // Parse URL-encoded bodies

2. Morgan: A logging middleware that logs HTTP requests to the console.

javascript
CopyEdit
const morgan = require('morgan');
app.use(morgan('dev'));

3. Cookie Parser: To parse cookies sent with the HTTP request.

javascript
CopyEdit
const cookieParser = require('cookie-parser');
app.use(cookieParser());

4. CORS (Cross-Origin Resource Sharing): To enable cross-origin


requests.

javascript
CopyEdit
const cors = require('cors');
app.use(cors()); // Allow all origins

16. How can you create custom middleware in an ExpressJS application?

Custom middleware in ExpressJS can be created by defining a function that


takes three parameters: req, res, and next(). The next() function is
called to pass control to the next middleware in the chain. Custom middleware
can perform actions like logging, request validation, authentication, etc.

Example:

javascript
CopyEdit
// Custom middleware for logging
app.use((req, res, next) => {
console.log(`Incoming request: ${req.method}
${req.url}`);
next(); // Pass to the next middleware or route
handler
});

Custom middleware can be applied globally (using app.use()) or to specific


routes:

javascript
CopyEdit
// Apply middleware to a specific route
app.get('/home', (req, res, next) => {
console.log('Home route accessed');
next();
}, (req, res) => {
res.send('Welcome to the homepage');
});

17. Why is error handling important in web applications?

Error handling is crucial in web applications because it ensures that the


application can respond to unexpected situations gracefully without crashing.
Proper error handling improves the user experience by providing meaningful
feedback when things go wrong (e.g., "Page not found" or "Internal server
error"). It also helps developers troubleshoot issues by logging detailed error
messages.

In ExpressJS, error handling middleware is specifically designed to catch errors


that occur during request processing. It helps in logging errors, sending user-
friendly error responses, and preventing application crashes.

18. How does ExpressJS handle errors by default?

By default, ExpressJS provides basic error handling. When an error is thrown in


a route handler or middleware, Express will skip to the first error-handling
middleware (defined with four parameters: err, req, res, next). If no error
handler is defined, Express will return a generic 500 "Internal Server Error"
response.

Example of error handling in Express:

javascript
CopyEdit
app.get('/error', (req, res) => {
throw new Error('Something went wrong!');
});

// Error-handling middleware
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error stack
res.status(500).send('Something went wrong!');
});

In this example, the application throws an error, which is caught by the error-
handling middleware, and a 500 error response is sent to the client.

19. Explain the use of the next function in error handling middleware.

In ExpressJS, the next function is used to pass control to the next middleware
in the chain. In error-handling middleware, the next() function is typically
used to forward the error to the next error handler, or to a general error handler
when no specific handler is defined. If an error is passed to next(), Express
will skip all remaining route handlers and go directly to the error-handling
middleware.

Example:

javascript
CopyEdit
app.use((req, res, next) => {
const err = new Error('Something went wrong!');
err.status = 500;
next(err); // Pass the error to the next
middleware
});

// Global error handler


app.use((err, req, res, next) => {
res.status(err.status || 500).send(err.message);
});

Here, the error is created and passed to the next() function, which is handled
by the global error handler.

You might also like