NodeJS Hiren Sir
NodeJS Hiren Sir
• Node.js process model increases the performance and scalability with a few
caveats. Node.js is not fit for an application which performs CPU-intensive
operations like image processing or other heavy computation work because it takes
time to process a request and thereby blocks the single thread.
Concepts
• The following diagram depicts some important parts of
Node.js
Setup Node.js Development Environment
the installation.
Setup Node.js Development Environment
• Verify Installation
• Once you install Node.js on your computer, you can verify it
by opening the command prompt and typing node -v. If
Node.js is installed successfully then it will display the version
of the Node.js installed on your machine, as shown below.
Node.js Console - REPL
• Node.js comes with virtual environment called REPL (aka Node
shell). REPL stands for Read-Eval-Print-Loop. It is a quick and
easy way to test simple Node.js/JavaScript code.
• To launch the REPL (Node shell), open command prompt (in
Windows) or terminal (in Mac or UNIX/Linux) and
type node as shown below. It will change the prompt to > in
Windows and MAC.
• You can now test pretty much any Node.js/JavaScript expression in REPL. For
example, if your write "10 + 20" then it will display result 30 immediately in
new line.
• The + operator also concatenates strings as in browser's JavaScript.
• You can also define variables and perform some operation on them.
• If you need to write multi line JavaScript expression or function then just
press Enter whenever you want to write something in the next line as a
continuation of your code. The REPL terminal will display three dots (...), it
means you can continue on next line. Write .break to get out of continuity mode.
• For example, you can define a function and execute it as shown below.
• can execute an external JavaScript file by writing node filename command. For
example, assume that node-example.js is on C drive of your PC with following
code.
console.log("Hello World");
REPL Commands
• 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.
• 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.
Node Package Manager (NPM)
• Node Package Manager (NPM) provides two main functionalities −
• Online repositories for node.js packages/modules which are
searchable on search.nodejs.org
• Command line utility to install Node.js packages, do version
management and dependency management of Node.js packages.
• NPM comes bundled with Node.js installable after v0.6.3 version.
To verify the same, open console and type the following command
and see the result −
Global vs Local Installation
• By default, NPM installs any dependency in the local mode.
Here local mode refers to the package installation in
node_modules directory lying in the folder where Node
application is present. Locally deployed packages are
accessible via require() method. For example, when we
installed express module, it created node_modules directory in
the current directory where it installed the express module.
• 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.
Node.js Basics
• Node.js supports JavaScript. So, JavaScript syntax on Node.js is
similar to the browser's JavaScript syntax.
• Primitive Types
• Node.js includes following primitive types:
– String
– Number
– Boolean
– Undefined
– Null
– RegExp
• Everything else is an object in Node.js.
• Loose Typing
• JavaScript in Node.js supports loose typing like the browser's
Node.js Basics
• Object Literal
• Object literal syntax is same as browser's JavaScript.
• var obj = {
authorName: 'Ryan Dahl',
language: 'Node.js'
}
• Functions
• Functions are first class citizens in Node's JavaScript, similar to the browser's
JavaScript. A function can have attributes and properties also. It can be treated
like a class in JavaScript.
• function Display(x)
{
console.log(x);
}
Node.js Basics
• Buffer
• Node.js includes an additional data type called Buffer (not available in browser's
JavaScript). Buffer is mainly used to store binary data, while reading from a file or
receiving packets over the network.
• process object
• Each Node.js script runs in a process. It includes process object to get all the
information about the current process of Node.js application.
• The following example shows how to get process information in REPL
using process object.
• Defaults to local
• Node's JavaScript is different from browser's JavaScript when it comes to global scope.
In the browser's JavaScript, variables declared without var keyword become global. In
Node.js, everything becomes local by default.
• Access Global Scope
• In a browser, global scope is the window object. In Node.js, global object represents
the global scope.
• To add something in global scope, you need to export it using export or module.export.
The same way, import modules/object using require() function to access it from the
Node.js Basics
• Access Global Scope
• In a browser, global scope is the window object. In
Node.js, global object represents the global scope.
• To add something in global scope, you need to export it using export or
module.export. The same way, import modules/object using require()
function to access it from the global scope.
• exports.log = {
console: function(msg)
{ console.log(msg);
},
file: function(msg) { // log to file here }
}
• Now, you can import log object using require() function and use it
Node.js Module
• Module in Node.js is a simple or complex functionality
organized in single or multiple JavaScript files which can
be reused throughout the Node.js application.
• Each module in Node.js has its own context, so it cannot
interfere with other modules or pollute global scope. Also,
each module can be placed in a separate .js file under a
separate folder.
• Node.js implements CommonJS modules standard.
CommonJS is a group of volunteers who define JavaScript
standards for web server, desktop, and console
application.
Node.js Module Types
• Node.js includes three types of modules:
– Core Modules
– Local Modules
– Third Party Modules
Node.js Core Modules
• Node.js is a light weight framework. The core modules include bare
minimum functionalities of Node.js. These core modules are compiled
into its binary distribution and load automatically when Node.js process
starts. However, you need to import the core module first in order to use
it in your application.
• The following table lists some of the important core modules in Node.js.
Core Module Description
http http module includes classes, methods and events to create Node.js
http server.
url url module includes methods for URL resolution and parsing.
querystring querystring module includes methods to deal with query string.
path path module includes methods to deal with file paths.
fs fs module includes classes, methods, and events to work with file
I/O.
• Example
Node.js - Event Emitter
• 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.
• Node.js allows us to create and handle custom
events easily by using events module. Event
module includes EventEmitter class which can
be used to raise and handle custom events.
EventEmitter Class
• As we have seen in the previous section, EventEmitter class lies
in the events module. It is accessible via the following code −
1 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.
2 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.
3 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.
4 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.
Sr.No. Method & Description
5 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.
6 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.
7 listeners(event)
Returns an array of listeners for the specified event.
1 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.
2 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.
Buffers
• Pure JavaScript is Unicode friendly, but it is not so
for binary data. While dealing with TCP streams or
the file system, it's necessary to handle octet
streams. Node provides Buffer class which provides
instances to store raw data similar to an array of
integers but corresponds to a raw memory
allocation outside the V8 heap.
• Buffer class is a global class that can be accessed in
an application without importing the buffer
module.
Creating Buffers
• Node Buffer can be constructed in a variety of ways.
• Method 1
– Following is the syntax to create an uninitiated Buffer of 10 octets −
var buf = new Buffer(10);
• Method 2
– Following is the syntax to create a Buffer from a given array
var buf = new Buffer([10, 20, 30, 40, 50]);
• Method 3
– Following is the syntax to create a Buffer from a given string and
optionally encoding type −
var buf = new Buffer("Simply Easy Learning", "utf-8");
Writing to Buffers
• Syntax
• Following is the syntax of the method to write into a Node Buffer
−
– buf.write(string[, offset][, length][, encoding])
• Parameters
• Here is the description of the parameters used −
– string − This is the string data to be written to buffer.
– offset − This is the index of the buffer to start writing at. Default value
is 0.
– length − This is the number of bytes to write. Defaults to buffer.length.
– encoding − Encoding to use. 'utf8' is the default encoding.
• Return Value
• This method returns the number of octets written. If there is not
enough space in the buffer to fit the entire string, it will write a
Reading from Buffers
• Syntax
• Following is the syntax of the method to read data from
a Node Buffer −
– buf.toString([encoding][, start][, end])
• Parameters
• Here is the description of the parameters used −
– encoding − Encoding to use. 'utf8' is the default encoding.
– start − Beginning index to start reading, defaults to 0.
– end − End index to end reading, defaults is complete buffer.
• Return Value
• This method decodes and returns a string from buffer
data encoded using the specified character set
Convert Buffers to JSON
• Syntax
• Following is the syntax of the method to
convert a Node Buffer into JSON object
−buf.toJSON()
• Return Value
• This method returns a JSON-representation of
the Buffer instance.
Concatenate Buffers
• Syntax
• Following is the syntax of the method to
concatenate Node buffers to a single Node Buffer
– Buffer.concat(list[, totalLength])
• Parameters
• Here is the description of the parameters used −
– list − Array List of Buffer objects to be concatenated.
– totalLength − This is the total length of the buffers
when concatenated.
• Return Value
• This method returns a Buffer instance.
Compare Buffers
• Syntax
• Following is the syntax of the method to compare
two Node buffers
– buf.compare(otherBuffer);
• Parameters
• Here is the description of the parameters used
– otherBuffer − This is the other buffer which will be
compared with buf.
• Return Value
• Returns a number indicating whether it comes
before or after or is the same as the otherBuffer in
Copy Buffer
• Syntax
• Following is the syntax of the method to copy a node buffer
– buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
• Parameters
• Here is the description of the parameters used
– targetBuffer − Buffer object where buffer will be copied.
– targetStart − Number, Optional, Default: 0
– sourceStart − Number, Optional, Default: 0
– sourceEnd − Number, Optional, Default: buffer.length
• Return Value
• No return value. Copies data from a region of this buffer to a
region in the target buffer even if the target memory region
overlaps with the source. If undefined, the targetStart and
sourceStart parameters default to 0, while sourceEnd defaults to
Slice Buffer
• Syntax
• Following is the syntax of the method to get a sub-buffer
of a node buffer
– buf.slice([start][, end])
• Parameters
• Here is the description of the parameters used
– start − Number, Optional, Default: 0
– end − Number, Optional, Default: buffer.length
• Return Value
• Returns a new buffer which references the same memory
as the old one, but offset and cropped by the start
(defaults to 0) and end (defaults to buffer.length) indexes.
Negative indexes start from the end of the buffer.
Buffer Length
• Syntax
• Following is the syntax of the method to get a size of a
node buffer in bytes
– buf.length;
• Parameters
• Here is the description of the parameters used
– start − Number, Optional, Default: 0
– end − Number, Optional, Default: buffer.length
• Return Value
• Returns a new buffer which references the same memory
as the old one, but offset and cropped by the start
(defaults to 0) and end (defaults to buffer.length) indexes.
Negative indexes start from the end of the buffer.
Buffer Class Methods
Sr.No. Method & Description
1 Buffer.isEncoding(encoding)
Returns true if the encoding is a valid encoding argument, false otherwise.
2 Buffer.isBuffer(obj)
Tests if obj is a Buffer.
3 Buffer.byteLength(string[, encoding])
Gives the actual byte length of a string. encoding defaults to 'utf8'. It is not
the same as String.prototype.length, since String.prototype.length returns
the number of characters in a string.
4 Buffer.concat(list[, totalLength])
Returns a buffer which is the result of concatenating all the buffers in the list
together.
5 Buffer.compare(buf1, buf2)
The same as buf1.compare(buf2). Useful for sorting an array of buffers.
Buffer Methods
Method Description
alloc() Creates a Buffer object of the specified length
indexOf() Checks if the Buffer object contains the specified value. Returns the
first occurrence, otherwise -1
ref() Makes the Timeout object active. Will only have an effect if the
Timeout.unref() method has been called to make the Timeout object
inactive.
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 and writing, telling 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).
w+ open file for reading and writing. the file is created (if it does not
exist) or truncated (if it exists).
Flags
Flag Description
w+ open file for reading and writing. the file is created (if it does not
exist) or truncated (if it exists).
a open file for appending. the file is created if it does not exist.
a+ open file for reading and appending. the file is created if it does not
exist.
ax+ open file for reading and appending. the file is created if it does not
exist.
Get File Information
• Syntax
• Following is the syntax of the method to get the
information about a file −
fs.stat(path, callback)
• 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.
Node.js fs.Stats class Methods
Method Description
• 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.
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 server and index.html
Creating Web client using Node
• A web client can be created
using http module. Let's check the following
example.
ExpressJS - Overview
• ExpressJS is a web application framework that
provides you with a simple API to build
websites, web apps and back ends. With
ExpressJS, you need not worry about low level
protocols, processes, etc.
What is Express?
• Express provides a minimal interface to build our
applications. It provides us the tools that are
required to build our app. It is flexible as there
are numerous modules available on npm, which
can be directly plugged into Express.
• Express was developed by TJ Holowaychuk and is
maintained by the Node.js foundation and
numerous open source contributors.
Why Express?
• Unlike its competitors like Rails and Django, which have an
opinionated way of building applications, Express has no
"best way" to do something. It is very flexible and pluggable.
• Pug
• Pug (earlier known as Jade) is a terse language for writing
HTML templates. It −
• Produces HTML
• Supports dynamic code
• Supports reusability (DRY)
• It is one of the most popular template language used with
Express.
ExpressJS - Environment
• node --version npm --version You should get an output similar to the following.
• v5.0.0 3.5.2 Now that we have Node and npm set up, let us understand what npm is and how
to use it.
• Node Package Manager(npm)
• npm is the package manager for node. The npm Registry is a public collection of packages of
open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless
other needs of the JavaScript community. npm allows us to access all these packages and install
them locally. You can browse through the list of packages available on npm at npmJS.
• How to use npm?
• There are two ways to install a package using npm: globally and locally.
• Globally − This method is generally used to install development tools and CLI based packages.
To install a package globally, use the following code.
• npm install -g <package-name>
• Locally − This method is generally used to install frameworks and libraries. A locally installed
package can be used only within the directory it is installed. To install a package locally, use the
same command as above without the -g flag.
• npm install <package-name>
• Whenever we create a project using npm, we need to provide a package.json file, which has all
the details about our project. npm makes it easy for us to set up this file. Let us set up our
development project.
• Step 1 − Start your terminal/cmd, create a new folder named hello-world and cd (create
ExpressJS - Environment
• Step 2 − Now to create the package.json file using npm, use the following code.
• npm init
• Step 3 − Now we have our package.json file set up, we will further install Express. To
install Express and add it to our package.json file, use the following command −
• npm install --save express
• To confirm that Express has installed correctly, run the following code.
• ls node_modules #(dir node_modules for windows)
• Tip − The --save flag can be replaced by the -S flag.
• This flag ensures that Express is added as a dependency to our package.json file.
This has an advantage, the next time we need to install all the dependencies of our
project we can just run the command npm install and it will find the dependencies
in this file and install them for us.
• This is all we need to start development using the Express framework. To make our
development process a lot easier, we will install a tool from npm, nodemon. This
tool restarts our server as soon as we make a change in any of our files, otherwise
we need to restart the server manually after each file modification. To install
nodemon, use the following command −
• npm install -g nodemon
ExpressJS - Hello World
• We have set up the development, now it is
time to start developing our first app using
Express. Create a new file called index.js and
type the following in it.
• Save the file, go to your terminal and type the
following. var express = require('express');
var app = express();
• nodemon index.js app.get('/', function(req, res){
res.send("Hello
world!");
});
app.listen(3000);
How the App Works?
• The first line imports Express in our file, we have access to it through the variable Express. We use it to
create an application and assign it to var app.
• app.get(route, callback)
• This function tells what to do when a get request at the given route is called. The callback function has 2
parameters, request(req) and response(res). The request object(req) represents the HTTP request and
has properties for the request query string, parameters, body, HTTP headers, etc. Similarly, the response
object represents the HTTP response that the Express app sends when it receives an HTTP request.
• res.send()
• This function takes an object as input and it sends this to the requesting client. Here we are sending the
string "Hello World!".
• app.listen(port, [host], [backlog], [callback]])
• This function binds and listens for connections on the specified host and port. Port is the only required
parameter here.
– port
• A port number on which the server should accept incoming requests.
– host
• Name of the domain. You need to set it when you deploy your apps to the cloud.
– backlog
• The maximum number of queued pending connections. The default is 511.
– callback
• An asynchronous function that is called when the server starts listening for requests.
Node.js - RESTful API
• What is REST architecture?
• REST stands for REpresentational State Transfer. REST is web
standards based architecture and uses HTTP Protocol. It
revolves around resource where every component is a resource
and a resource is accessed by a common interface using HTTP
standard methods. REST was first introduced by Roy Fielding
in 2000.
• A REST Server simply provides access to resources and REST
client accesses and modifies the resources using HTTP
protocol. Here each resource is identified by URIs/ global IDs.
REST uses various representation to represent a resource like
text, JSON, XML but JSON is the most popular one.
HTTP methods
• Following four HTTP methods are commonly
used in REST based architecture.
– GET − This is used to provide a read only access to
a resource.
– PUT − This is used to create a new resource.
– DELETE − This is used to remove a resource.
– POST − This is used to update a existing resource
or create a new resource.
RESTful Web Services
• A web service is a collection of open protocols and standards used
for exchanging data between applications or systems. Software
applications written in various programming languages and
running on various platforms can use web services to exchange
data over computer networks like the Internet in a manner similar
to inter-process communication on a single computer. This
interoperability (e.g., communication between Java and Python,
or Windows and Linux applications) is due to the use of open
standards.
• Web services based on REST Architecture are known as RESTful
web services. These webservices uses HTTP methods to
implement the concept of REST architecture. A RESTful web
service usually defines a URI, Uniform Resource Identifier a
service, which provides resource representation such as JSON and
set of HTTP Methods.
Creating RESTful for A Library
• Consider we have a JSON based database of users
having the following users in a file users.json
• Based on this information we are going to provide
following RESTful APIs.
Sr.No. URI HTTP Method POST body Result