Professional Documents
Culture Documents
Unit 2.2
Unit 2.2
ADVANCE WEB
DEVELOPMENT
By:Divya Thakur
UID:28300
Contents
• JSON
• BUFFER
• STREAM
• COMPRESS/DECOMPRESS USING ZLIB
JSON
// Concatenating buffers
const concatenatedBuffer = Buffer.concat([buffer1,
buffer2]);
console.log(concatenatedBuffer.toString('utf-
8')); // Output: Hello World
Writing and Reading Data:
• You can write data into the buffer and read it back:
Ex: https://www.google.com/search?q=Value+of+pi
A typical URL has the following
components:
• Protocol: This indicates the protocol used to access the resource, such as
HTTP, HTTPS, FTP, etc.
• Domain: This specifies the domain name or IP address of the server where
the resource is hosted.
• Path: This is the specific location of the resource on the server's file system.
• Query String: This is optional and is used to pass parameters to the resource.
It starts with a question mark (?) and consists of key-value pairs separated by
ampersands (&).
• Fragment: Also optional, this specifies a specific section within the resource,
typically used in HTML documents to navigate to a specific part of a webpage.
https://www.example.com/products?
id=123&page=1#overview
• Protocol: https://
• Domain: www.example.com
• Path: /products
• Query String: id=123&page=1
• Fragment: overview
Processing URLs:
const url = require('url');
http.createServer((req,res)=>{
let parsedURL=url.parse(req.url,true);
console.log(parsedURL);
console.log(parsedURL.query.name);
if(parsedURL.pathname=='/'){
let readableStream=fs.createReadStream('public/index.html');
readableStream.pipe(res);
}else if(parsedURL.pathname=='/submit' && req.method=="GET"){
let writableStream = fs.createWriteStream('form_data.txt');
let query = parsedURL.query;
writableStream.write(query.name+'\n');
writableStream.write(query.email);
writableStream.on('finish',() => {
console.log("Form has been saved Successfully");
});
writableStream.end();
res.end("Data has been successfully saved");
}
}).listen(4000);
Whaaat is this, Stream?
Streams are sequences of data made
available over time. Rather than reading or
writing all the data at once, streams allow
you to process data piece by piece, which is
particularly useful when dealing with large
datasets or when real-time processing is
required.
So, Stream Module….
const fs = require('fs');
// Listen for the 'data' event, which indicates that a chunk of data is available
readableStream.on('data', (chunk) => {
console.log('Received chunk of data:');
console.log(chunk);
});
// Listen for the 'end' event, which indicates that all data has been read
readableStream.on('end', () => {
console.log('Finished reading data from the file.');
});
// Listen for the 'error' event, in case of any errors during reading
readableStream.on('error', (err) => {
console.error('Error reading data:', err);
});
Writing Data
const fs = require('fs');
writableStream.on('finish', () => {
console.log('Data has been written to the file.');
});
• rstream.on('error',(err)=>
•{
• console.log(err);
• res.end("file doesnot exist");
• })
Piping Streams
The process of connecting the output of
one stream to the input of another
stream. This allows you to easily transfer
data from one stream to another without
manually handling the data flow
const fs = require('fs');
// Pipe the data from the readable stream to the writable stream
readableStream.pipe(writableStream);
http.createServer((req,res)=>{
if(req.url=='/'){
readStream=fs.createReadStream('example.txt',
{highWaterMark:8});
writeStream=fs.createWriteStream('example2.txt');
readStream.pipe(writeStream);
res.end('Doneeeee');
}
}).listen(5000);
Compressing and
Decompressing Data using
Zlib
zlib is a built-in Node.js module that
provides compression and decompression
functionalities using the zlib library, which is
a general-purpose data compression library.
It supports various compression algorithms.
• Here are some key features and functionalities of the zlib module:
// Example data
const input = 'Hello, world!';
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip' // Setting the content encoding to gzip
});