Node.js Exam Notes PDF
Document Details
Uploaded by Deleted User
Tags
Summary
These notes provide an overview of Node.js, including its definition, features, modules, and Express.js. They cover topics such as modules, localhost, client-server protocols, and NoSQL databases. The document also explains important concepts for node.js.
Full Transcript
Node.js Exam Notes Overview of Node.js Definition: Node.js is a JavaScript runtime built on Chrome’s V8 engine, designed for developing server-side and network applications. Features: Event-driven, non-blocking I/O model for efficient, real-time, data-intensive...
Node.js Exam Notes Overview of Node.js Definition: Node.js is a JavaScript runtime built on Chrome’s V8 engine, designed for developing server-side and network applications. Features: Event-driven, non-blocking I/O model for efficient, real-time, data-intensive applications. Runs on a single-threaded event loop, handling asynchronous tasks in the background, ensuring scalability. Node.js Modules Module System: Each JavaScript file is a separate module. Types of Modules: Core/Built-in Modules: Pre-installed with Node.js (e.g., file system, networking). Local Modules: User-defined modules in the project directory. Third-Party Modules: Installed via NPM (Node Package Manager), either globally or locally. Importing Modules: Use the require() function. Localhost Definition: Refers to the local machine or loopback address (127.0.0.1). Purpose: Used for testing web applications during development before deployment. Client-Server Protocol Communication Process: 1. Connection Establishment: Client connects to the server (via TCP). 2. Request/Response: Client sends a request; server responds. 3. Session Phases: Request sent. Server processes request. Response returned with status code and data. Express.js Overview Definition: Express.js is a third-party framework for Node.js, known for simplicity and flexibility. Key Features: Routing: Manages HTTP requests and responses. Static File Serving: Serves files like HTML, CSS, and JS. Error Handling: Customizes error responses. Database Integration: Connects to databases (e.g., MongoDB, MySQL). Session Management: Manages user sessions. Request/Response Handling: Processes requests and sends responses. MEAN Stack: Part of the MEAN stack (MongoDB, Express.js, Angular, Node.js) for dynamic web apps. Express.js Routing Routing: Defines server responses to HTTP requests via URL paths and methods (e.g., GET, POST). Route Handlers: Functions that process requests for matched routes. Middleware: Functions executed sequentially before final request handlers for tasks like authentication or error handling. Handling Requests and Responses Request Object (req): Contains client request details (headers, body, query parameters). Response Object (res): Sends responses to the client (status codes, JSON data). Error Handling in Express.js Error Handling Middleware: Centralized error control for consistent responses. Ensures robustness by managing unexpected scenarios gracefully. Setting Up the Server: Use app.listen() for quick and efficient server setup. Overview of NoSQL Databases Definition: “Not Only SQL,” designed for unstructured/semi-structured data. Characteristics: Non-relational: No fixed schemas. Distributed: Data spread across multiple machines. Open-source: Often free to use. Horizontally Scalable: Scaled by adding servers. ACID Properties in NoSQL Atomic: Transactions are fully completed or rolled back. Consistent: Adheres to defined rules after a transaction. Isolated: Transactions run without interference. Durable: Changes persist despite failures. CAP Theorem (Consistency, Availability, Partition Tolerance) Basic Availability: Guaranteed response, even if inconsistent. Soft-state: Data changes over time without input. Eventual Consistency: Synchronization achieved eventually. Types of NoSQL Databases Key-Value Stores: Key-value pairs (e.g., Redis). Document-Based: Semi-structured data (e.g., MongoDB). Column-Based: Columnar data storage (e.g., Cassandra). Graph-Based: Nodes and edges for relationships (e.g., Neo4j). MongoDB Key Concepts Structure: Databases: Store collections of documents. Collections: Groups of related documents. Documents: Individual records in BSON format. Unique Identifier: _id field automatically assigned as primary key. Collection Creation: Automatic: Created when inserting the first document. Explicit: Use db.createCollection("name"). MongoDB Update Methods Update Parameters: Query: Conditions to locate documents. Fields: Data to update, add, remove, or rename. Options: Additional behaviors (e.g., upsert). Compound Operators: AND: Implicit for multiple conditions. OR: Use $or or $in for flexibility. Mongoose is an Object Data Modeling (ODM) library for MongoDB in Node.js, designed to simplify interactions with MongoDB. It enables developers to: Define schemas for structured data. Manage data relationships. Validate data before saving. Key Components Schema: Blueprint defining the structure, types, and validations for documents in a MongoDB collection. Model Provides methods to perform CRUD operations on documents using the defined schema. Middleware: Logic applied during pre- and post-processing stages, such as validations or transformations. Plugins :Extend Mongoose functionality, adding features like timestamps or custom validations. Mongoose Workflow Install and import the Mongoose module. Define a Schema: Outline the document structure and data types. Create a Model: Represents the MongoDB collection and provides CRUD methods. Connect to the database: Establish a connection to the MongoDB instance.