Back End
Back End
➢ Node.JS:
• A server-side platform which is built on Google Chrome’s V8-JavaScript
Engine which was initially built and developed by Mr. Rayn Dal in the year
2009.
• Using Node.js we could able to build faster and scalable network
applications.
• Node.js internally uses event driven and non-blocking I/O mechanism (or)
model which makes it light weight and efficient for data intensive real time
applications which run on distributive system/devices.
• Node.js is open source and cross-platform runtime environment using which
we could develop server side and networking-based applications.
• JavaScript is programming language using which we could able to add
instructions within Node.js.
• Node.js comes with rich library of various JavaScript modules which
simplifies the development of the web applications using Node.js.
• Node.js is a combination of Run-Time environment and JavaScript library.
➢ Features of Node.JS used for developing web applications:
➢ Asynchronous and Event Driven:
All API’s of Node.js are asynchronous and non-blocking which means
it is a server which doesn’t waits for any API to return the data, the server
moves to the next API after calling it, a notification mechanism of events of
Node.js helps server to get response from previous API calls.
➢ Faster in Processing:
As the Node.js is built on Google Chrome’s V8-JS Engine, it is very
faster in executing instructions.
➢ Single Threaded but Highly Scalable:
Internally Node.js uses a single threaded model with event looping, the
event mechanism helps the server to respond in a non-blocking way and makes
the server highly scalable.
1|Page
Naresh i®technologies Mr Durga Prasad.P
➢ No Buffering:
Node.js applications never buffer any data, these applications output
data in chunks.
➢ Architecture of Node.JS:
2|Page
Naresh i®technologies Mr Durga Prasad.P
➢ Node Package Manager (NPM):
• NPM is an online code repository publishing of open source Node.js code, it
is also a command line utility for interacting with NPM repository for
package installing and version management.
• It is through NPM we could work with installation (or) uninstallation (or)
upgradation node modules. Through NPM we could even start or stop node
server.
➢ Node.js Module:
A module in node.js is a simpler or complex functionality organized in
single or multiple JavaScript files which can be reserved throughout the
node.js application. Each module in node.js has its own context so that it
doesn’t interfere with other modules or doesn’t interrupt the global scope.
It is through NPM we install (or) uninstall version manager of any
module can be done.
Following are the basic steps which we need to create a HTTP Node
Server,
Step-1: Download Node HTTP Module through NPM,
npm install http //Downloads a HTTP Module.
Step-2: Create a JavaScript file and include HTTP Module through require
method.
var http = require(“http”); //including a Node Module.
Step-3: Use “createServer” method under HTTP, which takes a callback method
as a parameter with default request and response objects.
http.createServer ( (req,res) => {
….. //Code to handle request and response
});
Step-4: Using response object we can specify the type of content being returned
from the server.
res.writeHead(200,{‘content-type’: ‘text/HTML’});
3|Page
Naresh i®technologies Mr Durga Prasad.P
Using writeHead method we can specify the response status code along
with the response text type.
Step-5: using res.write() method we can write the content to be responded from
the server.
res.write(“<Content>”);
Step-6: res.end() is an predefined method through which we can send request
using user response.
res.end();
Step-7: Make the server to listen at a particular port number
server.listen(8080);
4|Page
Naresh i®technologies Mr Durga Prasad.P
npm i fs
Step-2: Include and create a reference for FS Module,
var fs = require(“fs”);
Step-3: Use the following predefined methods under FS Module through
which we could able to do any file operations in files.
Fs.readFile(“<filename>”, (err,data) => {
//err objects holds the error if any while reading file
//Data holds the actual data of file.
});
Fs.appenFile(“<filename>”, “<Content to be written>”,(err,data) => {
//err objects holds the error if any while reading file
//Data holds the actual data of file.
});
Fs.rename(“filename”,”newfileName”,(err) => {
Err……
});
Ex:
5|Page
Naresh i®technologies Mr Durga Prasad.P
➢ Node Express.JS Framework:
Express.js is a web application framework for node.js through which
we could able to create flexible node.js web applications. It is one of the most
popular web frameworks provides following mechanisms,
• It writes handlers with requests with different http at different URL paths.
• It integrates with view rendering engines in order to generate response by
inserting data into the template.
• It sets common web applications settings like the port to use for connecting,
location of templates that are used etc.…
• Express itself is fairly minimalist developers are created compatible
middleware packages to address utmost any web development problems.
• There are libraries to work with cookies, sessions, user logins, URL
params, post data, security headers and so on.
• Express was released on 2010 and current latest version is 4.17.1*.
➢ Folder structure got created through express generator:
• Node_modules:
Under which all the downloaded node modules of the current app will be
stored. This folder name cannot be renamed.
• Package.json file:
It holds a json object with all the configuration information of the current
module.
i. It holds list of dependencies of node module.
ii. Name and Version number of the current application.
iii. Author name and contact details.
iv. Starting point of the current application (node start/server start).
• Bin Folder:
A predefined folder holds a predefined ‘www’ holds the set of instructions
through which we could create http server, making the server to listen a
default port number handling the errors if any.
6|Page
Naresh i®technologies Mr Durga Prasad.P
o Bin folder and www file name can be renamed to a custom name,
corresponding changes to be done at package.json
• Routes Folder:
Holds set of JavaScript files, where each JavaScript file represents a single
web service. Any number of web services can be added with in node
server, all the web services JS files should be placed under the Routes
folder.
o Index.js, users.js are the default services available under routes
folder.
o Once the services are added under routes folder, corresponding route
mapping or URL mapping has to be done under app.js file.
• Steps to create a web service under a node express server:
Step-1: Create an external JS file under Routes Folder.
Step-2: under the JS file, create an instance of express module.
Var express = require(‘express’);
Step-3: create a router instance through a router class under express
module.
Var router = express.Router();
Step-4: using the router reference, through GET/POST method add the
business logic of the corresponding webservice under call back method.
router.get(“/”, (req,res) => {
….. //Logic
res.send();
});
(or)
router.get(“/”, (req,res) => {
….. //Logic
res.send();
});
Step-5: under the app.js file specify the router path through which web
services can be accessible.
7|Page
Naresh i®technologies Mr Durga Prasad.P
Ex:
JS File
8|Page
Naresh i®technologies Mr Durga Prasad.P
9|Page
Naresh i®technologies Mr Durga Prasad.P
*Mongo DB*
➢ MongoDB:
MongoDB is a cross-platform document-oriented database program
classified as a NOSQL database program. MongoDB uses JSON like
documents with optional schemas. MongoDB is developed by MongoDB.INC
and licensed under the server-side public license. MongoDB is a document
data base with the scalability and flexibility that you want with the querying
and indexing that you need.
➢ Key Features of MongoDB:
• Supports ad hoc queries:
In MongoDB you can search by field, range query and it also supports
regular expression searches.
• Indexing:
You can index any field in a document.
• Replication:
MongoDB supports slave replication. A master can perform read and write
and a slave copies data from the master and can only be used for reads or
backup (not writes).
• Duplication of data:
MongoDB can run over multiple servers. The data is duplicated to keep the
system up and keep its running condition in case of hardware failure.
• Load Balancing:
It has an automatic load balancing configuration because of the data placed
in shards.
10 | P a g e
Naresh i®technologies Mr Durga Prasad.P
• Easy to administer in case of failures.
• It also supports:
o JSON data model with dynamic schemas.
o Auto-Sharing for horizontal scalability.
o Built in replication for high availability.
o Now a days many companies using MongoDB to create new types
of applications, improve performance and availability.
➢ Difference between MongoDB Structure and SQL Structure:
11 | P a g e
Naresh i®technologies Mr Durga Prasad.P
➢ Commands that can be run on a Mongo Shell:
• db – returns the name of current database been used.
• use<db name> - switches to specific DB.
• db.help( ) – throws help commands.
• Show dbs – throws list of all database under MongoDB.
• Show users – returns the list of users.
• db.getCollectionNames( ) – shows list of collections within a DB.
• db.createCollection(“<Collection Name>”) – used to create a collection.
Ex: db.createCollection("Login_Details")
• db.collection.insert({document}) – used to insert a document to a
collection.
• db.collection.insertmany([<document1>,<document2>,…] – used to
insert many document to a single collection.
Ex:
db.Login_Details.insertMany([{_id:'adminLogin',uid:'admin',upsw:'admin'},
{_id:'studentLogin',uid:'student',upsw:'student'},{_id:'parentLogin',uid:'paren
t',upsw:'parent'}])
13 | P a g e
Naresh i®technologies Mr Durga Prasad.P
➢
14 | P a g e
Naresh i®technologies Mr Durga Prasad.P