0% found this document useful (0 votes)
51 views8 pages

Assignment N0. 03 Submission Due Date: 16 June 2020 Marks 10

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows JavaScript to be run on the server-side. Some key differences between Node.js and JavaScript include: Node.js executes JavaScript code outside of a browser environment, allowing it to be used for server-side web applications. JavaScript is primarily used for client-side web development and interaction, while Node.js allows JavaScript to be used for non-browser environments like server-side scripting. Node.js provides APIs for working with the file system, networking and other functionalities that are not available to standard client-side JavaScript.

Uploaded by

rizwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Assignment N0. 03 Submission Due Date: 16 June 2020 Marks 10

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows JavaScript to be run on the server-side. Some key differences between Node.js and JavaScript include: Node.js executes JavaScript code outside of a browser environment, allowing it to be used for server-side web applications. JavaScript is primarily used for client-side web development and interaction, while Node.js allows JavaScript to be used for non-browser environments like server-side scripting. Node.js provides APIs for working with the file system, networking and other functionalities that are not available to standard client-side JavaScript.

Uploaded by

rizwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Computer Science & Information Technology

Web Engineering
Semester 5 Section A (Spring 2020)

Assignment N0. 03
Submission Due Date: 16th June 2020
Marks 10

Instructions (Any): Softcopy with proper front page only.

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 File System modules:

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');

fs.readFile ('TestFile.txt', function (err, data) {


1
if (err) throw err;

console.log (data);
});

 Create Files

The File System module has methods for creating new files:

1) Fs.apppendFile (): which allow us to add content to a file.

For Example: fs.appendFile (‘file.txt’,’add thus’)

Will add “add thus “to end of specified txt document.

2) Fs.open(): this allows to perform several functions on a file such as instead


of opening a file each time we write to it and then close it we pen the file
once and close it after all operations are finished. This is done using flags
which fs.open () uses.

fs.pen(‘file.txt’,’w’)

This open a file for writing.

3) Fs.writeFile (): this allows us to replace a file if it is already present.

fs.writeFile (‘file.txt’, ‘replace this’)

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.

The fs.rename () method renames the specified file:

Example

Rename "mynewfile1.txt" to "myrenamedfile.txt":

var fs = require('fs');

fs.rename ('mynewfile1.txt', 'myrenamedfile.txt', function (err) {


if (err) throw err;
console.log ('File Renamed!') ;});

Question No. 2:
Answer the following briefly

i. Where can we use node.js?

Node.js can be used to develop:


 Real-Time Web Applications
 Network Applications
 Distributed Systems
 General Purpose Applications

ii. What is the advantage of using node.js?

Advantages of using node.js are:


 Node.js offers an Easy Scalability
 Easy to Learn
 Node.js is used as a Single Programming Language
 The Benefit of Fullstack JS
 Known for Offering High Performance
 The Support of Large and Active Community
 The Advantage of Caching
 Offers the Freedom to Develop Apps
 Getting Support for Commonly Used Tools
 Handles the Requests Simultaneously
 Node.js is Highly Extensible

iii. What are the two types of API functions in Node.js?


3
There are two types of API functions in Node.js:
 Asynchronous, non-blocking functions
 Synchronous, blocking functions

iv. Why Node.js is single threaded?

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.

v. How node.js works?

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.

Here is a list of the built-in modules of Node.js version 6.10.3:

4
.
Example

Convert the string "abc" into a stream of binary data:

var buf = Buffer.from('abc');
console.log(buf);

The buffers module provides a way of handling streams of binary data.

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

Extract the filename from a file path:

var path = require('path');
var filename = path.basename('/Users/Refsnes/demo_path.js');
console.log(filename);
Example

Open a file and return the content line by line:

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 And JavaScript:

 Web development technology, javascript is the primary interpreted programming language


which is high level, weakly typed, dynamic, prototype-based and multi-paradigm. Javascript
is also among the three core technologies of the World Wide Web which enables interactive
web pages. All major web browsers and websites have a dedicated JS engine to execute it. It
supports an event-driven, imperative and a functional programming style construct.

 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

You might also like