Assignment N0. 03 Submission Due Date: 16 June 2020 Marks 10
Assignment N0. 03 Submission Due Date: 16 June 2020 Marks 10
Web Engineering
Semester 5 Section A (Spring 2020)
Assignment N0. 03
Submission Due Date: 16th June 2020
Marks 10
Question No. 1:
Explain the following Node.js File System modules? Explain all of them with the help of at least one
or two examples.
Node.js basically used in php that allow you to work with the file system in your computer and
require () method is necessary in node.js. if I talk about the file system modules in php all are given
below.
Read Files
This method is used for read files on your computer I mean file is opened for read-only.
And the read file () function reads a file and write it to the output buffer.
Example 1:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return its content:
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Example 2:
var fs = require('fs');
console.log (data);
});
Create Files
The File System module has methods for creating new files:
fs.pen(‘file.txt’,’w’)
This will replace the file named “file.txt” if it is resent as well as the content
inside.
Update Files
Example 1:
Example 2:
Delete Files
Example 1:
2
Example 2:
Rename Files
To rename a file with the File System module, use the fs.rename () method.
Example
var fs = require('fs');
Question No. 2:
Answer the following briefly
Node.js uses a single threaded model in order to support async processing. With async
processing, an application can perform better and is more scalable under web loads. Thus,
Node.js makes use of a single-threaded model approach rather than typical thread-based
implementation.
Node.js is a virtual machine that uses javascript as its scripting language and runs on a v8
environment. It works on a single-threaded event loop and a non-blocking I/O which
provides high rate as it can handle a higher number of concurrent requests. Also, by
making use of the 'HTTP' module, Node.js can run on any stand-alone web server.
Question No. 3:
What is Module in Node.js? Explain any five built-in modules with the help of at least one or
two examples.
Module in Node.js
Node.js has a set of built-in modules which you can use without any further installation.
4
.
Example
var buf = Buffer.from('abc');
console.log(buf);
The Buffer object is a global object in Node.js, and it is not necessary to import it using
the require keyword.
Node.js Cluster Process Module
Example
Run the code three times, the first time is as a master, then as workers:
var cluster = require('cluster');
if (cluster.isWorker) {
console.log('I am a worker');
} else {
console.log('I am a master');
cluster.fork();
cluster.fork();
}
The cluster module provides a way to create child processes which run simultaneously and share the
same port of server. It run in single threaded, which is memory efficient, but it will take multi core
advantage, it allows you to easily create child processes which run on their single thread to handle
load.
5
Node.js runs single threaded programming, which is very memory efficient, but to take advantage of
computers multi-core systems, the Cluster module allows you to easily create child processes that
each runs on their own single thread, to handle the load.
Example
Make an event listener for an event called "scream", then provoke the event:
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('scream', function() {
console.log('A scream is detected!');
});
eventEmitter.emit('scream');
Example
var path = require('path');
var filename = path.basename('/Users/Refsnes/demo_path.js');
console.log(filename);
Example
var readline = require('readline');
var fs = require('fs');
var myInterface = readline.createInterface({
input: fs.createReadStream('demofile1.html')
});
var lineno = 0;
myInterface.on('line', function (line) {
lineno++;
console.log('Line number ' + lineno + ': ' + line);
});
The Readline module provides a way of reading a DataStream, one line at a time.
Question No. 4:
What is Events Module and EventEmmitter in Node.js? Explain with the help of at least one or
two examples.
Node.js Events Module
To handle events. The Events module provides a way of working with events.
In Node.js, all events are an instance of the EventEmitter object
Syntax
6
The syntax for including the Events module, and creating an EventEmitter in your application:
var events = require('events');
var eventEmitter = new events.EventEmitter();
Example
Make an event listener for an event called "scream", then provoke the event:
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('scream', function() {
console.log('A scream is detected!');
});
eventEmitter.emit('scream');
Question No. 5:
What are the main differences between Node.js and JavaScript?
Node.js, on the other hand, is a cross-platform, open source runtime environment which is
helpful in executing javascript code on the server side. If you talk about the earlier versions
of Javascript, it was primarily a client-side scripting language where the code was embedded
inside the web page’s HTML and the engine was inside the browser. Now it is used as a
server-side scripting language to produce a dynamic web-based content before the web page
is rendered onto the user’s web browser. It aims to unify the web application
development experience which revolves around a single programming language.
The javascript has the API which is used for working with arrays, text, regular expressions,
dates and basic level of DOM manipulation. The best feature which makes javascript a
popular language is that it doesn’t include the I/O such as graphics facilities, networking,
storage, etc. They all are dependent on the embedded host environment. Earlier javascript
was only implemented as a client-side in web browsers, today the engines are embedded in
other types of host software which essentially includes server-side scripting in web servers
and databases. They are also embedded in non-web programs like PDF, word processors and
also in runtime environment which makes JS available for writing desktop as well as mobile
applications along with desktop widgets.
.js in node.js is a conventional filename extension which is merely the name of the product. It
has an event-driven architecture which has the feature of handling the I/O. The design
7
choices are aimed to optimize the scalability, throughput and reduce latency as there becomes
a rise in the usage of the input/output operations and real-time web-based applications such as
browser-based games and real-time communication systems and programs.
A simple Javascript program is alert (“hey there!”); .This will be put inside the script tags on
the HTML page and tells the web browser of the user to interpret it as the code for javascript
such as script alert (“Hey there!”); which then makes a small alert box whereas in case of
your node on your REPL, all you need to write is node and your interpreter is good to go. For
example,
> Node
> 1+5
6