FSD Unit - 2 - Part-2
FSD Unit - 2 - Part-2
Unit -2 Part-2
Full Stack Development
By B. Lokesh Joel
https://www.softsuave.com/blog/key-trends-in-full-stack-development/
Unit-2: Node.js
• Working with JSON, • Implementing HTTP Clients and Servers in
the Stream Module to Stream Data, • Implementing HTTPS Servers and Clients.
• Accessing the File System from Node.js- • Using Additional Node.js Modules-
• Opening, Closing, Writing, Reading Files and • Using the os Module,
other File System Tasks. • Using the util Module,
▪ The url module in Node.js helps parse and manipulate URL strings into
objects.
http://host:port/path:parameters?query#fragment
Creating a URL Object
▪ The url.parse() method is used to convert a URL string into a URL
object.
url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
– urlStr: The URL string.
– parseQueryString: If true, parses the query string into an object.
– slashesDenoteHost: If true, treats //host/path as a hostname and path.
Resolving URL Components
▪ The url.resolve() method resolves a URL relative to a base URL.
Syntax: url.resolve(from, to)
– from: Original URL.
– to: New relative path or URL part.
Processing Query Strings and Form Parameters
▪ In Node.js, query strings in URLs and form parameters (data sent in the
body of POST requests) are typically simple key-value pairs.
– To work with these values, they need to be parsed into a JavaScript object.
– Node.js provides the querystring module to handle this conversion.
• options: An object that defines details for the request (e.g., hostname, path, method).
• callback: A function called when the server responds, with an IncomingMessage object as a
parameter.
http.ServerResponse Object
▪ The ServerResponse object is created by the Node.js HTTP server
when a request event is received and is passed as the second
argument in the request handler.
– It allows you to formulate and send a response to the client. Since it
implements a Writable stream, it includes functionality for writing data
and sending responses.
▪ Key Features:
– Writable Stream: Can use methods like write() to send data to the client.
– Used in Request Handlers: It's used to handle HTTP responses within the
request handler.
http.ServerResponse Object
▪ Events and Properties of ServerResponse:
– close: Emitted when the connection to the client is closed before the
response.end() finishes.
– headersSent: A Boolean indicating whether headers have been sent
(true) or not (false).
– sendDate: A Boolean that, when set to true, automatically sends the Date
header with the response.
– statusCode: Allows setting the status code (e.g., 200, 404, 500) without
needing to write headers explicitly.
http.IncomingMessage Object
▪ The IncomingMessage
object represents incoming
requests on the server and
responses from the client.
– It can act as both a Readable
stream, allowing data to be
read in chunks, and as a source
of request/response metadata.
http.Server Object
▪ The Server object in Node.js is
the foundation for
implementing an HTTP
server.
– It handles incoming
connections, accepts requests,
and sends responses.
Starting an HTTP Server:
▪ To create and start a server, you use the following methods:
1. http.createServer([requestListener]):
• This method creates a new server object. The optional requestListener is
the callback executed for each incoming request.
Starting an HTTP Server:
2. listen(port, [hostname], [backlog], [callback]):
– Binds the server to a specific port and optional hostname.
• Parameters:
• port: The port number (e.g., 8080).
• hostname: Optional, the server’s IP address (defaults to all available addresses).
• backlog: The maximum number of pending connections.
• callback: Function executed once the server starts listening.
▪ Client-Side Interaction:
– The server also acts as a client when it sends a request to the OpenWeatherMap API to retrieve the
weather information based on the city name provided by the user.
▪ Key Concepts:
– Encryption: HTTPS uses a combination of long-term public/private keys and
short-term session keys to encrypt data.
– Authentication: HTTPS verifies the identity of the server using SSL/TLS
certificates to prevent attacks.
Implementing HTTPS Servers and Clients
▪ Steps for Implementing HTTPS:
▪ Generate Private Key and Certificate:
– Use OpenSSL to generate a private key and a certificate for encryption.
– Generate a private key:
openssl genrsa -out server.pem 2048
– Create a certificate signing request (CSR):
openssl req -new -key server.pem -out server.csr
– Create a self-signed certificate for testing purposes:
openssl x509 -req -days 365 -in server.csr -signkey server.pem -out server.crt
– For production, use a third-party Certificate Authority (CA) to sign the certificate.
Creating an HTTPS Client
▪ Creating an HTTPS client in Node.js is similar to creating an HTTP
client, with the key difference being the need for additional security-
related options, such as specifying SSL/TLS certificates and keys.
– This allows the client to securely communicate with an HTTPS server by
encrypting the data exchanged.
the Stream Module to Stream Data, • Implementing HTTPS Servers and Clients.
• Accessing the File System from Node.js- • Using Additional Node.js Modules-
• Opening, Closing, Writing, Reading Files and • Using the os Module,
other File System Tasks. • Using the util Module,
72