Lintroduction: Its Official Documentation Chrome'S Javascript Runtime
Lintroduction: Its Official Documentation Chrome'S Javascript Runtime
Hello and welcome to our backend track. In this track we are going to learn how to create a backend
server using Node.js, Express and MongoDB. These are the technologies that we are going to use
during this track.
Let’s start with Node.js. In this super skill we are going to:
• Discover what’s nodejs?
• Learn how to setup our environment and create our first application?
• Understand how to use the REPL terminal?
• Understand what’s the node package module and the concept of modules in Node.js
• Practice how to work with event loops, callback function and file system?
What is Node.js?
Node.js is a server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js
was developed by Ryan Dahl in 2009. The definition of Node.js as supplied by its official
documentation is as follows:
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and
scalable network applications. Node.js uses an event-driven, non-blocking I/O model
that makes it lightweight and efficient, perfect for data-intensive real-time applications
that run across distributed devices.
Features of Node.js
The Following are some of the important features that make Node.js the first choice of software
architects:
• Asynchronous and Event Driven − All APIs of Node.js library are asynchronous, that is,
non-blocking. It essentially means a Node.js based server never waits for an API to return
data. The server moves to the next API after calling it and a notification mechanism of
Events of Node.js helps the server to get a response from the previous API call.
• Very Fast − Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very
fast in code execution.
• Single Threaded but Highly Scalable − Node.js uses a single threaded model with event
looping. Event mechanism helps the server to respond in a non-blocking way and makes the
server highly scalable as opposed to traditional servers which create limited threads to
handle requests. Node.js uses a single threaded program and the same program can provide
service to a much larger number of requests than traditional servers like Apache HTTP
Server.
• No Buffering − Node.js applications never buffer any data. These applications simply
output the data in chunks.
• License − Node.js is released under the MIT license.
Concepts
The following diagram depicts some important parts of Node.js which we will discuss in detail in
the subsequent chapters.
Text Editor
This will be used to type your program. Examples of few editors include Windows Notepad, OS
Edit command, Brief, Epsilon, EMACS, and vim or vi, ... or simply VSCode.
Name and version of text editor can vary on different operating systems. For example, Notepad will
be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.
The files you create with your editor are called source files and contain program source code. The
source files for Node.js programs are typically named with the extension ".js".
Before starting your programming, make sure you have one text editor in place and you have
enough experience to write a computer program, save it in a file, and finally execute it.
The above code is enough to create an HTTP server which listens, i.e., waits for a request over 8081
port on the local machine.
Starting REPL
REPL stands for Read Eval Print Loop and it represents a computer environment like a Windows
console or Unix/Linux shell where a command is entered and the system responds with an output in
an interactive mode. Node.js or Node comes bundled with a REPL environment. It performs the
following tasks:
• Read − Reads user's input, parses the input into JavaScript data-structure, and stores in
memory.
• Eval − Takes and evaluates the data structure.
• Print − Prints the result.
• Loop − Loops the above command until the user presses ctrl-c twice.
The REPL feature of Node is very useful in experimenting with Node.js codes and to debug
JavaScript codes.
REPL can be started by simply running node on shell/console without any arguments as follows.
$ node
You will see the REPL Command prompt > where you can type any Node.js command
$ node
>
Simple Expression
Let's try a simple mathematics at the Node.js REPL command prompt
$ node
> 1 + 3
4
> 1 + ( 2 * 3 ) - 4
3
>
Use Variables
You can make use variables to store values and print later like any conventional script. If var
keyword is not used, then the value is stored in the variable and printed. Whereas if var keyword is
used, then the value is stored but not printed. You can print variables using console.log().
$ node
> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello World
undefined
Multiline Expression
Node REPL supports multiline expression similar to JavaScript. Let's check the following do-while
loop in action
$ node
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... }
while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
... comes automatically when you press Enter after the opening bracket. Node automatically checks
the continuity of expressions.
Underscore Variable
You can use underscore (_) to get the last result
$ node
> var x = 10
undefined
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log(sum)
30
undefined
>
REPL Commands
• ctrl + c − terminate the current command.
• ctrl + c twice − terminate the Node REPL.
• ctrl + d − terminate the Node REPL.
• Up/Down Keys − see command history and modify previous commands.
• tab Keys − list of current commands.
• .help − list of all commands.
• .break − exit from multiline expression.
• .clear − exit from multiline expression.
• .save filename − save the current Node REPL session to a file.
• .load filename − load file content in current Node REPL session.
Stopping REPL
As mentioned above, you will need to use ctrl-c twice to come out of Node.js REPL.
$ node
>
(^C again to quit)
>
ctrl + c
: terminate the current command.
ctrl + d
: terminate the Node REPL.
tab Keys
: list of current commands.
.break
: exit from multiline expression.
.save filename
: save the current Node REPL session to a file.
ctrl + c twice
: terminate the Node REPL.
Up/Down Keys
: see command history and modify previous commands.
.help
: list of all commands.
.clear
: exit from multiline expression.
.load filename
: load file content in current Node REPL session.
If you are running an old version of NPM then it is quite easy to update it to the latest version. Just
use the following command from root :
$ npm install npm -g
For example, following is the command to install a famous Node.js web framework module called
express
$ npm install express
Alternatively, you can use npm ls command to list down all the locally installed modules.
Globally installed packages/dependencies are stored in system directory. Such dependencies can be
used in CLI (Command Line Interface) function of any Node.js but cannot be imported using
require() in Node application directly. Now let's try installing the express module using global
installation.
$ npm install express -g
This will produce a similar result but the module will be installed globally. Here, the first line shows
the module version and the location where it is getting installed.
Using package.json
package.json is present in the root directory of any Node application/module and is used to
define the properties of a package. Let's open package.json of express package present in
node_modules/express/
Uninstalling a Module
Use the following command to uninstall a Node.js module.
$ npm uninstall express
Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/
directory or type the following command −
$ npm ls
Updating a Module
Update package.json and change the version of the dependency to be updated and run the following
command.
$ npm update express
Search a Module
Search a package name using NPM.
$ npm search express
Create a Module
Creating a module requires package.json to be generated. Let's generate package.json using npm
init, which will generate the basic skeleton of the package.json.
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
You will need to provide all the required information about your module. You can take help from
the above-mentioned package.json file to understand the meanings of various information
demanded. Once package.json is generated, use the following command to register yourself with
NPM repository site using a valid email address.
$ npm adduser
Username: randomname
Password:
Email: (this IS public) randomname@gmail.com
If everything is fine with your module, then it will be published in the repository and will be
accessible to install using NPM like any other Node.js module.
What is the difference between core modules and 3rd party modules?The 1st one is installed with
nodejs and the 2nd must be installed separately
What npm init do? Initialize a node project with package.json file
To install a package using npm, we run
Npm i <package_name>Npm install <package_name>
What is Callback?
Callback is an asynchronous equivalent for a function. A callback function is called at the
completion of a given task. Node makes heavy use of callbacks. All the APIs of Node are written in
such a way that they support callbacks.
For example, a function to read a file may start reading file and return the control to the execution
environment immediately so that the next instruction can be executed. Once file I/O is complete, it
will call the callback function while passing the callback function, the content of the file as a
parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can
process a high number of requests without waiting for any function to return results.
console.log(data.toString());
console.log("Program Ended");
console.log("Program Ended");
Blocking vs Non-Blocking
These two examples explain the concept of blocking and non-blocking calls.
• The first example shows that the program blocks until it reads the file and then only it
proceeds to end the program.
• The second example shows that the program does not wait for file reading and proceeds to
print "Program Ended" and at the same time, the program without blocking continues
reading the file.
Thus, a blocking program executes very much in sequence. From the programming point of view, it
is easier to implement the logic but non-blocking programs do not execute in sequence. In case a
program needs to use any data to be processed, it should be kept within the same block to make it
sequential execution.
Node.js is a single-threaded application, but it can support concurrency via the concept of
event and callbacks?True
Blocking code approach mean that the execution will stop until that part of code complete
executingTrue
Callbacks are the solution of handling the asynchronous eventsTrue
Event-Driven Programming
Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to
other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares
functions and then simply waits for the event to occur.
In an event-driven application, there is generally a main loop that listens for events, and then
triggers a callback function when one of those events is detected.
Event loop
Although events look quite similar to callbacks, the difference lies in the fact that callback functions
are called when an asynchronous function returns its result, whereas event handling works on the
observer pattern. The functions that listen to events act as Observers. Whenever an event gets fired,
its listener function starts executing. Node.js has multiple in-built events available through events
module and EventEmitter class which are used to bind events and event-listeners as follows:
// Import events module
var events = require('events');
Here fs.readFile() is a async function whose purpose is to read a file. If an error occurs during the
read operation, then the err object will contain the corresponding error, else data will contain the
contents of the file. readFile passes err and data to the callback function after the read operation is
complete, which finally prints the content:
Program Ended
Dummy Text Just for Testing !!!!
The
Event Loop
doesn't start processing the event loop until the
async function
after an async function has executed.
The
Event Loop
is a queue of
callback functions
. When an
async function
executes, the
callback function
is pushed into the queue. The
JavaScript engine
doesn't start processing the event loop until the
code
after an async function has executed.
EventEmitter Class
Many objects in a Node emit events, for example, a net.Server emits an event each time a peer
connects to it, an fs.readStream emits an event when the file is opened. All objects which emit
events are the instances of events.EventEmitter.
As we have seen in the previous section, EventEmitter class lies in the events module. It is
accessible via the following code:
// Import events module
var events = require('events');
When an EventEmitter instance faces any error, it emits an 'error' event. When a new listener is
added, 'newListener' event is fired and when a listener is removed, 'removeListener' event is fired.
EventEmitter provides multiple properties like on and emit. on property is used to bind a function
with the event and emit is used to fire an event.
Methods
• addListener(event, listener) : Adds a listener at the end of the listeners array for the
specified event. No checks are made to see if the listener has already been added. Multiple
calls passing the same combination of event and listener will result in the listener being
added multiple times. Returns emitter, so calls can be chained.
• on(event, listener) : Adds a listener at the end of the listeners array for the specified event.
No checks are made to see if the listener has already been added. Multiple calls passing the
same combination of event and listener will result in the listener being added multiple times.
Returns emitter, so calls can be chained.
• once(event, listener) : Adds a one time listener to the event. This listener is invoked only
the next time the event is fired, after which it is removed. Returns emitter, so calls can be
chained.
• removeListener(event, listener) : Removes a listener from the listener array for the
specified event. Caution − It changes the array indices in the listener array behind the
listener. removeListener will remove, at most, one instance of a listener from the listener
array. If any single listener has been added multiple times to the listener array for the
specified event, then removeListener must be called multiple times to remove each instance.
Returns emitter, so calls can be chained.
• removeAllListeners([event]) : Removes all listeners, or those of the specified event. It's not
a good idea to remove listeners that were added elsewhere in the code, especially when it's
on an emitter that you didn't create (e.g. sockets or file streams). Returns emitter, so calls can
be chained.
• setMaxListeners(n) : By default, EventEmitters will print a warning if more than 10
listeners are added for a particular event. This is a useful default which helps finding
memory leaks. Obviously not all Emitters should be limited to 10. This function allows that
to be increased. Set to zero for unlimited.
• listeners(event) : Returns an array of listeners for the specified event.
• emit(event, [arg1], [arg2], [...]) : Execute each of the listeners in order with the supplied
arguments. Returns true if the event had listeners, false otherwise.
Class Methods
listenerCount(emitter, event) : Returns the number of listeners for a given event.
Events
• newListener
• event − String: the event name
• listener − Function: the event handler function
This event is emitted any time a listener is added. When this event is triggered, the
listener may not yet have been added to the array of listeners for the event.
• removeListener
• event − String: The event name
• listener − Function: The event handler function
This event is emitted any time someone removes a listener. When this event is
triggered, the listener may not yet have been removed from the array of listeners for
the event.
Example
Create a js file named main.js with the following Node.js code
var events = require('events');
var eventEmitter = new events.EventEmitter();
// listener #1
var listner1 = function listner1() {
console.log('listner1 executed.');
}
// listener #2
var listner2 = function listner2() {
console.log('listner2 executed.');
}
eventListeners =
require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
console.log("Program Ended.");
Synchronous vs Asynchronous
File System
Node implements File I/O using simple wrappers around standard POSIX functions. The Node File
System (fs) module can be imported using the following syntax:
var fs = require("fs")
Every method in the fs module has synchronous as well as asynchronous forms. Asynchronous
methods take the last parameter as the completion function callback and the first parameter of the
callback function as error. It is better to use an asynchronous method instead of a synchronous
method, as the former never blocks a program during its execution, whereas the second one does.
Example
Create a text file named input.txt with the following content
Dummy Text For Testing !!!!
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
Program Ended
Asynchronous read: Dummy Text For Testing !!!!
Let's take a look at a set of good examples on major File I/O methods:
Open a File
Syntax
Following is the syntax of the method to open a file in asynchronous mode:
fs.open(path, flags[, mode], callback)
Parameters
Here is the description of the parameters used :
• path − This is the string having file name including path.
• flags − Flags indicate the behavior of the file to be opened. All possible values have been
mentioned below.
• mode − It sets the file mode (permission and sticky bits), but only if the file was created. It
defaults to 0666, readable and writeable.
• callback − This is the callback function which gets two arguments (err, fd).
Flags
• r : Open file for reading. An exception occurs if the file does not exist.
• r+ : Open file for reading and writing. An exception occurs if the file does not exist.
• rs : Open file for reading in synchronous mode.
• rs+ : Open file for reading and writing, asking the OS to open it synchronously. See notes
for 'rs' about using this with caution.
• w : Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
• wx : Like 'w' but fails if the path exists.
• w+ : Open file for reading and writing. The file is created (if it does not exist) or truncated
(if it exists).
• wx+ : Like 'w+' but fails if path exists.
• a : Open file for appending. The file is created if it does not exist.
• ax : Like 'a' but fails if the path exists.
• a+ : Open file for reading and appending. The file is created if it does not exist.
• ax+ : Like 'a+' but fails if the the path exists.
Example
Let us create a js file named main.js having the following code to open a file input.txt for reading
and writing:
var fs = require("fs");
Parameters
Here is the description of the parameters used :
• path − This is the string having file name including path.
• callback − This is the callback function which gets two arguments (err, stats) where stats is
an object of fs.Stats type which is printed below in the example.
Apart from the important attributes which are printed below in the example, there are several useful
methods available in fs.Stats class which can be used to check file type. These methods are given in
the following list:
• stats.isFile() : Returns true if file type of a simple file.
• stats.isDirectory() : Returns true if file type of a directory.
• stats.isBlockDevice() : Returns true if file type of a block device.
• stats.isCharacterDevice() : Returns true if file type of a character device.
• stats.isSymbolicLink() : Returns true if file type of a symbolic link.
• stats.isFIFO() : Returns true if file type of a FIFO.
• stats.isSocket() : Returns true if file type of asocket.
Example
Let us create a js file named main.js with the following code
var fs = require("fs");
Writing a File
Syntax
Following is the syntax of one of the methods to write into a file
fs.writeFile(filename, data[, options], callback)
This method will over-write the file if the file already exists. If you want to write into an existing
file then you should use another method available.
Parameters
Here is the description of the parameters used :
• path − This is the string having the file name including path.
• data − This is the String or Buffer to be written into the file.
• options − The third parameter is an object which will hold {encoding, mode, flag}. By
default. encoding is utf8, mode is octal value 0666. and flag is 'w'
• callback − This is the callback function which gets a single parameter err that returns an
error in case of any writing error.
Example
Let us create a js file named main.js having the following code :
var fs = require("fs");
Reading a File
Syntax
Following is the syntax of one of the methods to read from a file
fs.read(fd, buffer, offset, length, position, callback)
This method will use file descriptor to read the file. If you want to read the file directly using the
file name, then you should use another method available.
Parameters
Here is the description of the parameters used:
• fd − This is the file descriptor returned by fs.open().
• buffer − This is the buffer that the data will be written to.
• offset − This is the offset in the buffer to start writing at.
• length − This is an integer specifying the number of bytes to read.
• position − This is an integer specifying where to begin reading from in the file. If position is
null, data will be read from the current file position.
• callback − This is the callback function which gets the three arguments, (err, bytesRead,
buffer)
Example
Let us create a js file named main.js with the following code
var fs = require("fs");
var buf = new Buffer(1024);
Closing a File
Syntax
Following is the syntax to close an opened file
fs.close(fd, callback)
Parameters
Here is the description of the parameters used:
• fd − This is the file descriptor returned by file fs.open() method.
• callback − This is the callback function No arguments other than a possible exception are
given to the completion callback.
Example
Let us create a js file named main.js having the following code
var fs = require("fs");
var buf = new Buffer(1024);
Parameters
Here is the description of the parameters used :
• fd − This is the file descriptor returned by fs.open().
• len − This is the length of the file after which the file will be truncated.
• callback − This is the callback function No arguments other than a possible exception are
given to the completion callback.
Example
Let us create a js file named main.js having the following code
var fs = require("fs");
var buf = new Buffer(1024);
Delete a File
Syntax
Following is the syntax of the method to delete a file −
fs.unlink(path, callback)
Parameters
Here is the description of the parameters used :
• path − This is the file name including path.
• callback − This is the callback function No arguments other than a possible exception are
given to the completion callback.
Example
Let us create a js file named main.js having the following code
var fs = require("fs");
Parameters
Here is the description of the parameters used :
• path − This is the directory name including path.
• mode − This is the directory permission to be set. Defaults to 0777.
• callback − This is the callback function No arguments other than a possible exception are
given to the completion callback.
Example
Let us create a js file named main.js having the following :
var fs = require("fs");
Read a Directory
Syntax
Following is the syntax of the method to read a directory:
fs.readdir(path, callback)
Parameters
Here is the description of the parameters used:
• path − This is the directory name including path.
• callback − This is the callback function which gets two arguments (err, files) where files is
an array of the names of the files in the directory excluding '.' and '..'.
Example
Let us create a js file named main.js having the following code:
var fs = require("fs");
Remove a Directory
Syntax
Following is the syntax of the method to remove a directory:
fs.rmdir(path, callback)
Parameters
Here is the description of the parameters used:
• path − This is the directory name including path.
• callback − This is the callback function No arguments other than a possible exception are
given to the completion callback.
Example
Let us create a js file named main.js having the following code:
var fs = require("fs");
fs.readdir("/tmp/",function(err, files) {
if (err) {
return console.error(err);
}
files.forEach( function (file) {
console.log( file );
});
});
});
What is the difference between writeFile and appendFile?The fs.writeFile() method replaces the
specified file and content if it exists while fs.appendFile appends data to a file
What is the result of the following code:the content of the file as raw buffer
fs.readFile('movies.txt', function (err, data) {
if (err) throw err;
console.log(data);
});
Which method of fs module is used to open a file?fs.open(path, flags[, mode], callback)
__filename
Node.js global objects are global in nature and they are available in all modules. We do not need to
include these objects in our application, rather we can use them directly. These objects are modules,
functions, strings and object itself as explained in the upcoming slides.
The __filename represents the filename of the code being executed. This is the resolved
absolute path of this code file. For a main program, this is not necessarily the same filename used in
the command line. The value inside a module is the path to that module file.
Example
Create a js file named main.js with the following code:
// Let's try to print the value of __filename
console.log( __filename );
Based on the location of your program, it will print the main file name as follows:
/web/com/1427091028_21099/main.js
__dirname
The __dirname represents the name of the directory that the currently executing script resides in.
Example
Create a js file named main.js with the following code:
// Let's try to print the value of __dirname
console.log( __dirname );
Based on the location of your program, it will print current directory name as follows:
/web/com/1427091028_21099
setTimeout(cb, ms)
The setTimeout(cb, ms) global function is used to run callback (cb) after at least ms milliseconds.
The actual delay depends on external factors like OS timer granularity and system load. A timer
cannot span more than 24.8 days.
Example
Create a js file named main.js with the following code:
function printHello() {
console.log( "Hello, World!");
}
clearTimeout(t)
The clearTimeout(t) global function is used to stop a timer that was previously created with
setTimeout(). Here t is the timer returned by the setTimeout() function.
Example
Create a js file named main.js with the following code:
function printHello() {
console.log( "Hello, World!");
}
Verify the output where you will not find anything printed.
setInterval(cb, ms)
The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms
milliseconds. The actual delay depends on external factors like OS timer granularity and system
load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the timer
using the function clearInterval(t).
Example
Create a js file named main.js with the following code:
function printHello() {
console.log( "Hello, World!");
}
The above program will execute printHello() after every 2 second. Due to system limitation.
The __filename give us:
The name of the file being executed and it absolute path
The setTimeout(cb,ms)Run the cb function after the time has passed.
Which of the following is true about global objects in Node applications?Global objects are global
in nature and they are available in all modules.Global objects are not required to be included in an
application, rather they can be used directly.
Utility Modules
There are several utility modules available in Node.js module library. Among the most frequently
used we mention the following, make sure to check them out:
• OS Module:
Provides basic operating-system related utility functions.
• Path Module:
Provides utilities for handling and transforming file paths.
• Net Module:
Provides both servers and clients as streams. Acts as a network wrapper.
• DNS Module:
Provides functions to do actual DNS lookup as well as to use underlying operating system
name resolution functionalities.
• Domain Module:
Provides ways to handle multiple different I/O operations as a single group.
The os module provides operating system-related utility methods and propertiesTrue
The net module provides an asynchronous network API for creating stream-based TCP or IPC
servers True
What is a module?It is a simple or complex functionality organized in single or multiple JavaScript
files which can be reused throughout the Node.js application.
eb Application Architecture
A Web application is usually divided into four layers:
• Client : This layer consists of web browsers, mobile browsers or applications which can
make HTTP requests to the web server.
• Server : This layer has the Web server which can intercept the requests made by the clients
and pass them the response.
• Business : This layer contains the application server which is utilized by the web server to
do the required processing. This layer interacts with the data layer via the database or some
external programs.
• Data : This layer contains the databases or any other source of data.
Creating a Web Server using Node
Node.js provides an http module which can be used to create an HTTP client of a server. Following
is the bare minimum structure of the HTTP server which listens at 8081 port.
Create a js file named server.js :
File: server.js
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create a server
http.createServer( function (request, response) {
// Parse the request containing file name
var pathname = url.parse(request.url).pathname;
<body>
Hello World!
</body>
</html>
response.on('end', function() {
// Data received completely.
console.log(body);
});
}
// Make a request to the server
var req = http.request(options, callback);
req.end();
Now run the client.js from a different command terminal other than server.js to see the result:
$ node client.js
<body>
Hello World!
</body>
</html>
Select among the following answers, the module types of nodejsCore ModulesLocal ModulesThird
Party Modules
What modules are core modules
httpurlfs
To use http module, we have to use const http = require("http")