Unit-2 Node.js: Working withJSON, Using the Buffer Module to Buffer Data, Using the Stream Module to Stream Data, Accessing the File System from Node.js- Opening, Closing, Writing, Reading Files and Other File System Tasks. Implementing HTTP Services in Node.js- Processing URLs, Processing Query Strings and Form Parameters, Understanding Request, Response, and Server Objects, Implementing HTTP Clients and Servers in Node.js, Implementing HTTPS Servers and Clients. Using Additional Node.js Modules-Using the os Module, Using the util Module, Using the dns Module, Using the crypto Module.
2.
Node.js: Working withJSON • Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. • JSON(JavaScript Object Notation) is a simple and text-based format for exchanging data between different applications. • Similar to XML, it’s a commonly used method for web applications and APIs to communicate and share information. • Below are the different methods to read and write JSON files • Require module and fs module
3.
Require module • Astraightforward way to read a JSON file in a Node JS file is by using the `require()` method to include it. • Syntax: • const data = require('path/to/file/filename'); • Example: Create a users.json file in the same directory where index.js file present. Add following data to the users.json file and write the index.js file code. { "name": "John", "age": 21, "language": ["JavaScript", "PHP", "Python"] } { "name": "Smith", "age": 25, "language": ["PHP", "Go", "JavaScript"] }
4.
Using the fsmodule: • Another approach to read a file in Node JS is by utilizing the fs module. • The fs module provides the file content as a string, requiring us to convert it into JSON format using the built-in method JSON.parse(). const fs = require("fs"); // Read users.json file fs.readFile("users.json", function(err, data) { // Check for errors if (err) throw err; // Converting to JSON const users = JSON.parse(data); console.log(users); // Print users });
5.
• Writing toa JSON file • We can write data into a JSON file by using the nodejs fs module. We can use writeFile method to write data into a file. • Syntax: • fs.writeFile("filename", data, callback); • Example: We will add a new user to the existing JSON file, we have created in the previous example. This task will be completed in three steps: Read the file using one of the above methods. Add the data using .push() method. Write the new data to the file using JSON.stringify() method to convert data into string.
6.
const fs =require("fs"); // STEP 1: Reading JSON file const users = require("./users"); // Defining new user let user = { name: "New User", age: 30, language: ["PHP", "Go", "JavaScript"] }; // STEP 2: Adding new data to users object users.push(user); // STEP 3: Writing to a file fs.writeFile( "users.json", JSON.stringify(users), err => { // Checking for errors if (err) throw err; // Success console.log("Done writing"); });
7.
Special features ofnodejs • Node is easy to get started • Superfast & highly scalable services • Large ecosystem of open-source libs • JavaScript everywhere
Using the BufferModule to Buffer Data • The Buffer object is a global object in Node.js, and it is not necessary to import it using the require keyword. • The syntax for creating an empty Buffer of the length 15: • var buf = Buffer.alloc(15); • Using the Stream Module to Stream Data • There are two types of streams: readable and writeable. • An example of a readable stream is the response object you get when working with the http.createServer() method. • An example of a writable stream is the request object you get when working with the http.createServer() method. • Syntax • Some methods returns a readable/writable stream object, like http.createServer(), and if that is the case, you do not have to include the stream module. • Otherwise, the syntax for including the Stream module in your application: • var stream = require('stream');
12.
Accessing the FileSystem from Node.js- Opening Introduction to Node.js: Node. js is not a programming language; it is a runtime environment allowing you to execute JavaScript code on the server side, outside a web browser. Role of Node.js in Full Stack Development: Node.js is a runtime environment that's popular for full-stack development because it allows developers to use JavaScript for both front-end and back-end tasks.
13.
Introduction to theFile System: Node.js provides powerful built-in modules for interacting with the file system. Developers can read, write, append, and delete files and directories, making it a crucial tool for full-stack web development. Accessing of files includes: 1. Reading Files 2. Writing Files 3. Appending to Files 4. Deleting Files
14.
1.Reading Files: Synchronous Reading Usethe `fs.readFileSync()` method to read files synchronously. This is suitable for small files, but can block the event loop for larger files. Asynchronous Reading The `fs.readFile()` method reads files asynchronously, allowing the event loop to continue processing other tasks while the file is being read. Stream Reading The `fs.createReadStream()` method reads files in chunks, making it efficient for handling large files without consuming too much memory.
15.
2.Writing Files: Synchronous Writing Usethe `fs.writeFileSync()` method to write files synchronously. This is suitable for small files, but can block the event loop for larger files. Asynchronous Writing The `fs.writeFile()` method writes files asynchronously, allowing the event loop to continue processing other tasks while the file is being written. Streaming Writes The `fs.createWriteStream()` method writes files in chunks, making it efficient for handling large files without consuming too much memory.
16.
3.Appending to Files: SynchronousAppend Use the `fs.appendFileSync()` method to append data to a file synchronously. Asynchronous Append The `fs.appendFile()` method appends data to a file asynchronously. Streaming Append The `fs.createWriteStream()` method with the `{ flags: 'a' }` option can be used to append data to a file using streams.
17.
4.Deleting Files: Sync Delete Use`fs.unlinkSync()` to delete a file synchronously. Async Delete Use `fs.unlink()` to delete a file asynchronously. Recursive Delete Use `fs.rmdirSync()` or `fs.rmdir()` to delete a directory and its contents recursively.
18.
Working with Directories: CreatingDirectories: •Synchronous: fs.mkdirSync() •Asynchronous: fs.mkdir() Reading Directories: •Synchronous: fs.readdirSync() •Asynchronous: fs.readdir()
19.
Best Practices: Error Handling Alwayswrap file system operations in try-catch blocks to handle errors gracefully. Asynchronous Approach Prefer asynchronous file system methods to avoid blocking the event loop. Security Considerations Validate and sanitize file paths to prevent directory traversal attacks.
20.
Handling File Paths AbsolutePaths Use `path.resolve()` to convert relative paths to absolute paths. Relative Paths Use `path.join()` to construct relative paths by combining multiple path segments. Normalization Use `path.normalize()` to clean up and standardize file paths, removing unnecessary segments like ".." or "//". Parsing Use `path.parse()` to break down a file path into its components (root, dir, base, name, ext).
21.
Understanding Request, Response,And Server Objects Full stack development involves both front-end and back-end technologies. It requires an understanding of how clients and servers communicate over the internet. The request and response objects are crucial for this interaction in web applications.
22.
What is aRequest Object? The request object represents the client's request to the server. It contains information such as the HTTP method, headers, and body data. Understanding the request object is essential for processing user input and delivering the correct responses.
23.
Types of HTTPRequests There are several types of HTTP requests, including GET, POST, PUT, and DELETE. Each type serves a different purpose, such as retrieving data or submitting new information. Knowing how to handle different request types is key in full stack development.
24.
Exploring the ResponseObject The response object represents the server's reply to the client's request. It includes status codes, headers, and the body content, which often contains data in JSON format. Properly constructing the response object ensures the client receives the expected information.
25.
HTTP Response Codes HTTPresponse codes inform the client about the status of their request. Common status codes include 200 (OK), 404 (Not Found), and 500 (Internal Server Error). Understanding these codes helps developers debug issues and improve user experience.
26.
The Role ofthe Server Object The server object is responsible for handling incoming requests and sending back responses. It listens for incoming connections and routes requests to the appropriate handlers. Efficient server management is essential for maintaining performance and reliability in applications.
27.
Implementing HTTPS serversand clients Introduction to HTTPS • HTTPS, or Hypertext Transfer Protocol Secure, is the secure version of the standard HTTP protocol used for web communication. • It provides encrypted and authenticated connections between clients and servers, ensuring the confidentiality and integrity of data transmitted over the internet.
28.
Importance of HTTPS Privacy HTTPSencrypts data, preventing eavesdropping and protecting sensitive information like login credentials and financial data. Integrity HTTPS ensures that data is not tampered with during transit, safeguarding the authenticity of the information being exchanged. Trust HTTPS helps build user trust by verifying the identity of the website, reducing the risk of phishing and man- in-the-middle attacks.
29.
HTTPS Protocol Overview 1SSL/TLS Handshake The client and server establish a secure connection by negotiating encryption algorithms and exchanging digital certificates. 2 Encryption Data is encrypted using symmetric-key algorithms, ensuring confidentiality during transmission. 3 Authentication The server's identity is verified using digital certificates, providing assurance that the user is communicating with the correct website.
30.
Implementing HTTPS Servers ServerConfiguration Servers must be configured to support HTTPS by obtaining a valid SSL/TLS certificate and enabling HTTPS in the web server software. Certificate Management Maintaining SSL/TLS certificates, including renewing them before expiration, is crucial for ensuring secure HTTPS connections. Performance Considerations HTTPS adds some overhead, so server resources and configuration should be optimized to maintain acceptable performance.
31.
Implementing HTTPS Clients 1 Browser Configuration Clients,typically web browsers, must be configured to trust the server's SSL/TLS certificate and enforce the use of HTTPS. 2 Certificate Validation Clients should verify the server's certificate to ensure the authenticity of the connection and protect against man- in-the-middle attacks. 3 Handling Errors Clients should be able to handle and report HTTPS-related errors, such as expired or untrusted certificates, to the user.
32.
Securing HTTPS Communication StrongEncryption Use the latest and most secure encryption algorithms and protocols, such as TLS 1.3, to protect data. Certificate Validation Ensure that SSL/TLS certificates are valid and issued by a trusted Certificate Authority. Secure Key Management Implement best practices for generating, storing, and rotating cryptographic keys to prevent compromise.
33.
Troubleshooting HTTPS Issues Certificate Errors Diagnoseand resolve issues related to expired, untrusted, or mismatched SSL/TLS certificates. Connectivity Problems Identify and fix network-related problems that prevent successful HTTPS connections. Performance Bottlenecks Optimize server and client configurations to address performance issues caused by HTTPS overhead.
34.
Node js ModuleAnd OS – Module • Node.js is an open source server environment • Node.js is free • Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • Node.js uses JavaScript on the server. • Node.js uses asynchronous programming!
35.
Node.js Modules 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.
36.
Types of node.jsmodules : 1.) 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 : const module = require('module_name’); • 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. Code : const http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/html' }); res.write('Welcome to this page!'); res.end();
37.
2.) 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 : exports.add = function (x, y) { return x + y; }; exports.sub = function (x, y) { return x - y; }; exports.mult = function (x, y) { return x * y; }; exports.div = function (x, y) { return x / y; };
38.
Since this fileprovides attributes to the outer world via exports, another file can use its exported functionality using the require() function. // Filename: index.js const calculator = require('./calc'); let x = 50, y = 10; console.log("Addition of 50 and 10 is " + calculator.add(x, y)); console.log("Subtraction of 50 and 10 is “ + calculator.sub(x, y)); console.log("Multiplication of 50 and 10 is “ + calculator.mult(x, y)); console.log("Division of 50 and 10 is "+ calculator.div(x, y)); Output : Addition of 50 and 10 is 60 Subtraction of 50 and 10 is 40 Multiplication of 50 and 10 is 500 Division of 50 and 10 is 5
39.
Using util module Theutil module in Node.js provides various utility functions that help developers perform common tasks more easily. It includes functions for debugging, formatting, and working with objects and other data types. While some of its functions are now considered legacy and have modern alternatives, the util module remains a useful tool for specific use cases.
40.
1. Loading theutil Module To use the util module in your Node.js application, you first need to require it: Basic Information About the util Module:
41.
2. Core Functionsof the util Module util.format(): • Formats a string similarly to how printf works in C. • It replaces placeholders (like %s, %d) in a string with corresponding values.
util.inspect(): • Used forprinting and inspecting JavaScript objects, making them easier to read and debug. • It can display hidden properties and manage how deep the inspection goes. output:-
44.
util.deprecate(): • Marks functionsas deprecated and issues a warning when they are used, signaling to developers that they should use alternative methods. output:-
45.
util.inherits(): • Allows forclassical inheritance by setting up the prototype chain. • Typically used to inherit from the built-in EventEmitter. output:-
46.
util.callbackify(): • Converts aPromise-based function back into a callback- based function. • Useful for working with older code that relies on callbacks.
47.
Use Cases • Debugging:util.inspect() is commonly used to print complex objects during debugging. • Migration: util.promisify() and util.callbackify() help in migrating between callback and Promise-based code. • Deprecation Warnings: util.deprecate() is used to inform developers that certain functions or features will be removed or should be avoided. • Type Checking: Although basic type checking can be done using typeof, the util module provides more specific checks (e.g., util.types.isDate()).
48.
3.) Third-party modules Third-partymodules 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: • npm install express , npm install mongoose , npm install -g @angular/cli Create a Simple Express Server: Code : const express = require('express’); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!');}); app.listen(port, () =>{ console.log(`Example app listening at http://localhost:${port}`); });
49.
Built-in modules • Node.jshas a set of built-in modules which you can use without any further installation.Here is a list of the built-in modules of Node.js version 6.10.3: Module Description assert Provides a set of assertion tests buffer To handle binary data child_process To run a child process cluster To split a single Node process into multiple processes crypto To handle OpenSSL cryptographic functions dgram Provides implementation of UDP datagram sockets dns To do DNS lookups and name resolution functions domain Deprecated. To handle unhandled errors events To handle events
50.
fs To handlethe file system http To make Node.js act as an HTTP server https To make Node.js act as an HTTPS server. net To create servers and clients os Provides information about the operation system path To handle file paths punycode Deprecated. A character encoding scheme querystring To handle URL query strings readline To handle readable streams one line at the time string_decoder To decode buffer objects into strings timers To execute a function after a given number of milliseconds url To parse URL strings v8 To access information about V8 (the JavaScript engine) vm To compile JavaScript code in a virtual machine zlib To compress or decompress files
51.
OS - Module The OS module is a built-in module in NodeJS that provides operating system-related functionality. It allows you to access information about the underlying operating system on which the NodeJS application is running. The OS module is useful for tasks that require information about operating system. This can include tasks such as determining the platform on which the application is running, retrieving information about system resources, or working with network interfaces. It provides functions to interact with the operating system. It provides the hostname of the operating system and returns the amount of free system memory in bytes
52.
How to Usethe OS Module: The OS module provides various functions and properties to access information about the operating system. Some of the commonly used functions and properties include: • os.arch(): Returns the CPU architecture of the operating system (e.g., ‘x64’, ‘arm’). • os.cpus(): Provides an array of objects describing each CPU/core installed. • os.freemem(): Returns the amount of free system memory in bytes. • os.homedir(): Returns the path to the current user’s home directory. • os.hostname(): Returns the hostname of the operating system. • os.networkInterfaces(): Returns a list of network interfaces and their details. • os.platform(): Returns the operating system platform (e.g., ‘linux’, ‘darwin’).
53.
• os.release(): Returnsthe operating system release. • os.totalmem(): Returns the total amount of system memory in bytes. • os.uptime(): Returns the system uptime in seconds. Features : • System Information: Provides insights into the system’s hardware and software environment. • Resource Monitoring: Helps in monitoring memory usage and system performance. • Platform Detection: Identifies the platform and version of the operating system.
54.
Example 1: Thisexample uses Node.js os module to fetch and display system details and memory info. a.js : const os = require('os’); console.log("CPU architecture: " + os.arch()); console.log("Free memory: " + os.freemem()); console.log("Total memory: " + os.totalmem()); console.log('List of network Interfaces: ' + os.networkInterfaces()); console.log('OS default directory for temp files : ' + os.tmpdir()); Output :