Hi everyone. This is Archana. And I welcome you all to Edureka’s YouTube channel We provide you online training on trending Technologies
with world’s best instructors and 24/7 support. So today I am here to give
you a brief overview on Node.js full course. Node.js as you guys might know is one of the most heavily
used JavaScript framework for server-side web
application development. It’s developed on Chrome OS
version a JavaScript engine and it helps in compiling
JavaScript directly into native machine code. In other words node.js allows you to build complex
and Powerful applications without having to write
any complex code thus we have created this full
course in such a way that everything you need to know
about node.js is divided into ten distinct modules. First module will talk
about what node.js exactly is and why should you learn it in? Audio will discuss
some basic concepts related to node.js in this session. You will learn
how to install node.js on your system related modules and features along with some ants on explanation
in the third module will learn about one of the most important
concept related to node.js which is NPR next up in the fourth module
will discuss how to create node.js application
using Express module, which is a framework for web application development
in module 5 will see how to develop restful API. By using node.js and perform
various grid operations like create read update and delete moving on
in the sixth module will learn how to integrate
your node.js application with mySQL database similarly
in the seventh module will learn how to integrate, you know, Chase
application with mongodb, which is a nosql
database innate module will cover an advanced topic
on how to dock rise. Your node.js application module
9 will teach you how to build a mean stack application where me Pack is an amalgamation
of four Technologies, which is mongodb
expressjs angular and node.js Tarik module
is mostly korea-related in this module. We’ll discuss most frequently
Asked interview questions that you will come across
in node.js interviews. Well, that’s all about
the offerings of this course. Thank you guys
while you are at it, please do not
forget to subscribe and Eureka YouTube channel
and hit the Bell icon to stay updated with
current trending Technologies. Hello everyone. This is Reuben from Eddie Rica. Every session. We are going to learn
what is node.js. So without any further delay. Let’s move on and look at the agenda for today’s
session in this session will first understand
the client-server architecture and multi-thread model followed
in times of architecture. Then we look at the limitations
of multi-thread architecture and will understand how node.js solve those limitations using single
thread architecture further moving on will understand. What is node.js. The help of Google case
study will look at some of the node.js success stories, then we’ll go
through node.js modules, which are important while working with
node.js projects. And at last we’ll take
a look at a demo which will help you
to understand how to work with node.js projects. So now let us move on
to our first topic which is why node.js will first understand the client
server architecture and know how client interacts with
the server in this architecture. We have users who interact
with the client machine like laptops desktop. Ops mobile phones Etc. So while interacting with the client machine user
makes our generates requests, which are then sent
to the server server takes those requests and processes those requests and send back the result
in form of response. Now, let us understand this
with the help of Ubers example, so there are
customers were interacting with Uber applications and generating request
to book a cab. So now those requests are sent to the Uber server
Uber server checks in the geospatial
databases for the cab. Other active and available in that region and then allocate
Sardis passes cap to a customer. Then server send
back the card details or driver details
back to the customer in form of response. We will understand
what is multi-threaded model and look at the limitations
of multi-grade model. In multivariate model the server
assigns incoming requests to a new thread similar has
a client-server architecture. The user generates a request while interacting
with the application and then it is sent
to the server. So every time a new request
comes in the server Maps a new trend from the thread pool
to the incoming request. Then the threat is responsible
for processing those requests and sending back the response. So the number of requests
is equal to the number of threads used
from the thread pool. Now the threads present
in the thread pool are in limited number based
on the resources. So if you will look closely, you will find a limitation
over here suppose. We have millions of requests
coming in per second. And as the thread present in this thread pool
are a limited number so there will be a situation when all the threads are
exhausted from the thread pool in that situation a new
incoming request has to wait until a thread processes
the last request and return back the result. So this is an situation of scalability which It is all
by adding more resources and creating more threads
inside the thread pool. But this is
a costly solution guys. We have an easier way of doing
that using single thread model, but before going
to single thread model will first understand
the second limitation of multi-thread model. The second limitation
of multivariate model is whenever a thread is working
on a shared resource. It acquires a lock
on that resource. So that no the thread
can access that resource. Now rest of the threads
trying to access that resource will be blocked. So you can see in this figure
that threat is working resource and it acquired a lock
on that resource. Now the rest of the threads
which are trying to access that resource are blocked till the time thread a will
finish the processing similarly when a will finish
the processing thread B will access that resource
and will again occur. I don’t lock against the rest
of the threads. So this creates
a bottleneck situation in processing requests
over shared resources and will affect
the response time of the server. Now, let us move
to our next topic and understand how single thread model
works and how it eliminates the limitation
of multi-thread model. So in a single thread model, whenever a user performs
an activity or event is generated for an example if you click a button
in an application, or if you fill up
a form a new event is generated you can take
each new request as an event. Now, once you generate an event
event amateur omit those events and then those events decide
inside the event queue in the server. Then these events are executed
using event Loops, which is a single thread
mechanism in this mechanism a Ted TX events from the event Queue at
allocates the event to a worker thread present
inside the thread pool. The allocation is based on
the operation performed by even like it could be eye operation. So it would be handled
by IO worker thread. It could be a network operation or it could be a CPU
intensive operations. So based on that it
will be handled to of thread. Then this worker thread
will process this request or synchronously. We’ll discuss this a synchronous
part in a while guys. Now you can notice here that we have only one thread
in This event Loop so one thread in the event Loop will be
handling the events directly. So no function in node.js
can directly perform operations. Thus the process
will never get blocked as this process are based
on events and events are handled by even Loop does this model
is also called even drill model. So our node.js follows
this event driven model or you can see
a single thread model. Now, let us conclude what we
went through till now so first in multi-threaded model the thread inside
the thread pool are limited and can Exhausted
at a point of time while in a
synchronous event-driven. There is only one thread
handling all the events from the event queue so there will be
no block request as no function in node.js can directly perform
operations now going to point to multi-thread model follows
an incoming request model in which whenever
a new request comes in the server allocates
the new thread to a new request while in a
synchronous event-driven, there is an event queue where all the event
resides the event Loop handle those events
using a single thread. Mechanism now in third point
in case of multiple events, the multi-thread model
will execute the event in a synchronous manner. For example, if we have events A
and B, then event, he will be executed first until that time event B
will wait once event a has finished its execution
then even be will be executed while in case of a synchronous
event driven event a and event B will be processed
or synchronously. They both will be executed
simultaneously and once the event has completed. Equation, it will send
back the result then in our fourth point
in multivariate model. There are lots of threads
which are working. So there will be context
switching between the threads why inner synchronous
even driven model. There is only one thread
handling the event queue so there will be no contest which in between threads
in our last Point as we just discuss now
due to her sickness execution and block threads. There are situation
when request will get blocked while in even driven
we execute events or synchronously and whenever
a request is processed. Assess it will call
a callback function. We will be discussing callback
function in the wild as please. Stay with me. As we know the difference
between multi-celled architecture and single
thread architecture now, let us move on
to our next topic. So now in the Uber story will
look at the old architecture and know the limitations
of the old architecture further moving on we’ll look
at the new architecture of uber and know how the limitations
are solved by node.js. Now let us understand the Uber
old architecture customer book the cap using Uber application
on the phones. Then the request
of Booker cap is sent to the Uber server then
the server checks with the geospatial databases that which are the Caps
available in that location. Now the server allocates
a cap to the customer and sends back the response
in form of cab details or driver details earlier. They were using PHP as
a server-side scripting. Now what happens with Uber is Ubers matching system creates
an enormous amount of support. Notification and demanded
quests but a rider or butter right request
is not just select the eventual driver nears
driver are stored in a geospatial databases, which is continuously updated
by every active driver on the network as they move about
the city matching Rider with the driver partner is the
mission critical system of uber if this breaks over it
does not have a business since they were using PHP for server-side and it is
a multi-threaded model. So each user requests to book a cap is handled
by a separate thread. So what happened was multiple threads were executing
car dispatch operations. So there were cases when one car is dispatched
for user and in between the same car
is dispatched one of the user so you might end up
in a situation where same car is allocated
to multiple users. So now let us understand
how node.js solve their issues. Nobody customer books a gap. It goes to the
node.js server here. We have two databases as you can see in the figure
first is mySQL database for persistent storage that is customer details
and Driver details, which is served
using python script whereas Mongo DB
which is the another database which is the geospatial database
for maintaining the records of active drivers in the region. So now the car is dispatched
for the mongodb dispatcher and the driver DDS is responded
to the user from mySQL database. Yes, so while using node they found that it can handle
a synchronous requests with a non-blocking
single thread event Loop, which is well suited
for distributed systems that makes a lot of network requests will discuss
about the non-locking in a while guys, but now Uber can handle multiple
requests or synchronously. The second point is
no GS is an accident for quick iterations program
can be inspected and errors can be addressed on the fly without
acquiring a restart so developer can publish and
deploy the new could constantly. Now as we know node
is open source. So the active open source
Community continuously optimize the technology and it get better
and better every time so now what happened Uber is now
doing over 2 million are pieces per second at Peak
across node.js Fleet. So now you can understand how node.js head Uber
to eliminate all the limitations that there are facing while using their
old architecture. So now let us understand. What is node.js. No DS is Javascript runtime
build on top of chromes. A JavaScript engine. It uses an event-driven
non-blocking I/O model that makes it lightweight
and efficient note the S is a cross-platform involvement. So it can run on OS X
Microsoft Windows Linux FreeBSD and multiple operating systems
node js has a JavaScript library for running applications
or for creating applications that is networking
or server side applications. It is open source
and free to use so now let us take a look at
the success stories of node.js. First we have Netflix, which is online TV series
and video streaming website, which use JavaScript and node.js
to transform their website into a single page application. Then we have PayPal which is an online
payment company people created two teams. One is Java Daemon Hammer one is
Java Script team to develop their application simultaneously what they found out is
Javascript team build the product both faster
and more efficiently. We just discussed how Uber use node.js
to build their website now. The next is GoDaddy, which is an application
hosting website, which use node.js for building
quality applications deploying new features and writing unit
as well as integration testing. Then we have linked in which
the social media website when it went to rebuild
their application. They use node.js for
their mobile application server at last we have eBay
which is an e-commerce website. They had two
primary requirement. The first was to make
application as real-time as possible and second was
to orchestrate a huge number of This specific services. So they achieved their primary
requirements using node.js. Now after knowing
this node.js success stories, let us move forward
and look at the job Trends. So now you can see
the graph this graph shows. The percentage increase
in job postings per year of node.js. So as you can see
this Rising curve, so by this curve, you can easily figure out that
if you are expecting a career in server-side web development. No, just the technology. You should Target or you
should start working with so now let us move forward and look
at the features of node.js. So the first is a synchronous and even driven we
already discuss the event driven mechanism. So now let us
understand or synchronous. So if our sinfulness you
can see we have caller who are requesting and we have recipient who are
sending back the response. We also have a callback
function over here. So let us understand
how this or synchronous works. So all the apis of node.js Library
are are synchronous that is non-blocking. So a node.js based
server never waits for an API to return
data the servo moves to the next API after calling it and a notification mechanism
of events of the node.js helps the server to get a response
from the previous API call. This is one of the Reason
by node.js is fast. The notification mechanism
is called callback function. We will discuss about it
in a while and in retail, but before that we’ll look. The other features
of node.js as node.js is built on top of Google Chrome’s
V8 JavaScript engine. So it’s library is very fast
in code execution. Then we have single thread
but highly scalable. So no just follow
the single thread mechanism as we know with event
looping it is highly scalable because even mechanisms
help the server to respond in a non-blocking way and makes
the server highly scalable as opposed to traditional
servers then we have no buffering. Node.js Kurds down
the overall processing time while uploading a video or audio file notice application
never before sending data the application simply
outputs the data in chunks, and we also know that node.js has an open source
Community which has produced many excellent modules
to add additional capabilities to node.js Applications. Let us move forward and look
at the node.js installation. So no just installation
is very easy. Let us go to Chrome browser. You can open the official side of node.js which is
node.js Dot o– r– g– and you can download
the windows version over here. You can easily install
by double-clicking it. So once you install your node js you will find a node.js
command prompt over here as you can see in my desktop. Okay guys now let us move
to our next topic which is node.js first example, so the steps for reading
a first example or for the steps for creating
a node.js application is so first create a directory in which your project
reside then create a file with JavaScript extension and embed your code
inside that No, open your node.js CMD and navigate to the node
example folder in which you created your project and then you can execute
your node js file or you can execute
your file using node space that file name come
and we’ll just see how to create a first
number in a while. So now this is
an important concept blocking and non-blocking. Now let us understand
blocking and non-blocking I/O with the help
of a restaurant analogy. Now what happens here
is there is a waitress and a chef in a restaurant while the customer comes
in the waitress takes the order and gives it to the ship. Then while the chef
is preparing that order waitress batik new orders
from new customers and given back to the chef. So this is the non-blocking
I/O situation where the waitress does not wait for the shift
to prepare the food. She takes the order
whenever Your customer comes in after the order
is prepared by the chef. The wheat is delivers
it back to the customer. But if it would be in a blocking way then
waitress would take the order and give it to the chef. Then waitress will be
in the food is prepared and delivered back
to the customer then only she can take a new order. So in this situation
no New Order comes in while or until unless the last
order is delivered. So now from this analogy, you might be clear how blocking
a non-blocking I/O works now, let us understand
this practically So in non-blocking I/O the execution of additional JavaScript
in the node.js process must wait until the non JavaScript
operation completes. Now let us create
a node.js project and execute this docking
iron non-blocking I/O so that you will get an example
or you will get an idea to how to work
with node.js projects. Will create a new file
with the name of blocking Jas. Now we have imported
file system module so that we can use
it in our program. Navi create another variable
that is data. So now you can see here. We are using function
read file sync. So this function will read
the file in a synchronous order. So while the file is read
the execution will wait for it. And then only it
will execute the commands. So in the name of the file, we are importing or we
are reading is TxDOT TST. We are printing
the data from the file. And at last it will print
and here we’ll see if this file will create
a new file with the name of techstop TST. No, I’ll write
welcome to edu Rica. I’ll save this file
then we’ll go to our node.js Command Prompt. Will execute this file
using node and the file name was blocking dot JS. So now you can see here that the first output
was welcome to Eureka which was there in text Dot txt. And then it printed and here
which was the last statement. You can see here now, let us hope with a non-blocking
JS will again create a file will name the file as
non-blocking dot Js. David again importing
file system modules from a node.js dependencies So as you can remember
guys last time we used read file sync function which made the function
synchronous but to make the function or to keep
the function or synchronous you will only use read file. And then the name of the file
is again TxDOT TST. And I will create a function. So now while you are defining
our signals function, the last parameter should be callback function which
we just talked about. Now, the first parameter
of your callback function should be the error exception
you are going to handle and the second parameter
should be your data or it might be a data which you are going
to respond back. Now we’ll check
if there is any error. We’ll print the error
using console DOT log. Another using a
settimeout function guys. So what this function
does is this execute a one-time call back
after delay milliseconds what you specify so over here. We are creating
the function settimeout. So inside this settimeout
function will print display after 2 seconds. Now guys that we have defined
a read Phi function and the properties and this is the ER
synchronous function now outside this function. I’ll also create or I will also
display console DOT log and inside this I
will print start here. I saved the file now, what will be happening you guys until this read file function
is being executed before that your console
DOT log will execute and it will print start here. Then while this read
file function will be executed and wild after displaying the result or after
processing this request. It will generate the result and then it will display
the result after start here. So let us take a look
and execute this program. So as you can see, we first get started here
and after 2 seconds, you are getting display
after two seconds. Let us move to our next topic
which is node.js modules. These modules are
pretty much important is because this will help you in
working with node.js projects. So we’ll look
at them one by one. We first start with npm. So npm stands for
node package manager. It provides online
repository for node.js. It is a command line utility
to download node.js packages or download online packages
of node.js it downloads and All the dependencies
from package.json file which is present
inside your project. So we have three commands. The first command is npm install
which will install the modules that are specified in your package Json and it will
create a node modules folder and will download and install all the dependencies
over there then second. We have npm
installed module name. You can download or install
the module name specifically by giving the name
for an example. So second command is
npm install module name. So in this command you can specifically mention
the module name for an example. You can mention
npm install Express and it will install
all the express modules inside your project. Then we have npm install
module name – G command, which will install
your module globally. We have a global scope so that you can install
your new command. You can solve your module
globally and you can use it in any of the projects
you are working on. So now let us move to our next. Beck which is Global’s so
Global objects are available in all modules. You have a global scope and you have a local scope
in global scope. You can use those objects inside any project of node.js
while in the local scope if you install locally or
if you are using object locally, then you can only
use that object inside that project only so we have a list of node.js
global objects will first start with underscore dir name and underscore file name so
underscore Dir name will give you the directory in which
your current code decides and file name. It will give you or will specify
the name of the file. That is that currently
contains the code. We have some
of the timer functions also and these timer functions in
node js is an internal construct that calls a given function
after a certain period of time so we have three functions which is set immediate
setinterval and settimeout. We just executed
settimeout function as you can remember guys
in our non blocking code. So this settimeout function
is used to execute a one-time call back
after delay in milliseconds. Then we have set
interval function, which is used to define
a time interval and will be executed after a certain interval
of time specified by you. And remember that is that the time you need
to specify hit in milliseconds. Then you have set
immediate function which is basically used
to execute a command immediately or will execute
that command immediately. We also have three more
functions that is clear immediate clear interval. Unclear timeout function the clear immediate function
stop immediate objects, which is created
by set immediate the clearinterval function
stops an interval object as created by set interval and clear timeout function
prevents a timeout object as created by set timeout. So let us move
to our next module which is file system module. This is also very
important module guys vs executed a part of it as you can remember
we use the syntax where FS is equal to require FS, which is used Import
the file system module from your node js modules
and you can use it in your core. So file input output
is provided by simple wrappers around standard poses function
for an example. You can first create a file
you can read a file you can write in a file or you can open
a file every method in FS module has a synchronous
and asynchronous forms as we just saw
that we use a synchronous form while we have creating
a blocking I/O and we use the Earth synchronous from while we are creating
a non-blocking I/O. So the main difference is you
have to use this s YN C keyword, which is used for
synchronization at the end of every function that will make it a synchronous and if you will not use
this s YN C keyword, then it would be
or synchronous function or the function would be
in our sinfulness form. So the Earth synchronous methods
take the last parameter as completion function callback a simplest method is preferred
over a synchronous method because it never blocks
the program execution where the simplest
method blocks now. You can easily compare and I
think you will be getting it because we just executed this
in locking and non-blocking I/O. So in file system module we have
functions for opening of file. So functions are
the first one is FS dot open and you have to give
the path of the file. Then you have to give flags and modes and at last you
will give a call back. So seeing this callback part
you can remember that callback is always given in case of a synchronous
functions not in case of synchronous function. So this is the way to open
a file or synchronously now, the second one is f is
not open as Phi n see as I told you earlier. If you are using S YN C
in end of any function, it will make the function
in synchronous form. So now you have to specify
the path of the file that you are opening. And again, you have
to specify the flats or modes at last. This is the close function that is f s dot close and you close the file using
FSD close function then we also have functions
for reading from a file. So the first function
is read function. So in this read function guys, we are reading the content
from file and save a get inside a buffer. Whoa, so this FD is
a file descriptor. This is the name of the buffer
the offset the length of the character to be read. Then we have position
the position till which it should be red. And at last we have a callback
as we all know that callback will return or will be executed
after the completion of this function then
second we have read file and in this we are
specifying the file name which we have to read and then we have
a callback function as we know from this that this is the synchronous
read file function then Have read file s YN C
that is synchronously. So we are going to read
this file synchronously and we will only give
the file name as the option or argument now moving on. We also have a right function
for writing into a file. So using this right file
function, you can open a file and you can insert the data or you can write the data
inside that file. And again you have
a callback function at the last so this is the synchronous form
of this function then second we have right file sync
and And as you can see, we have S YN C
keyword over here. So this is a synchronous
form of a function. You can say synchronous form
of right function. Then you give the file name
and you give the data which needs to be written
inside the file. So now let us move
to our next topic or the next module of node.js
that is called back. We have been talking about call
back since a while now, let us understand
this callback in detail. You might have got
a pretty much clearer idea of what our callback or what
are this callback functions? So basically a callback is
a synchronous equivalent for a function and is called
at the completion of each task. Now. I will make this simpler for you
a call back function is executed whenever a synchronous function
has process the request and will send back the response. So after processing, the request is callback function
will be executed and what we do in this callback function, as you can see in this code that in this callback function
the first parameter, which we are returning
is the error. Exception that will occur
inside the code or that may organ inside the code and the second parameter
argument is the data that is expected
to be responded. So as you can see here that callback will be executed
after the file is complete. So as the read file function
has to read a file. So after reading a file
it will call this function that is callback function. The first parameter is error
if it contains any error, it will display their error. Otherwise, you will get the data which Will be responded
to the users or times you can say now. Let us move to our next part
that is events. So as we know and we
discussed it earlier also that node.js follows
event-driven architecture. So every action you take or every activity
you do is the event and it is created automatically, but you can use
this events module and import this module and you
can create your own event and emit your own event. So let us understand
this by the code. So first we are importing events
from the node.js module which will be accessing
in our code or program. You can say then we
are creating a my emitter and this is the instance
of event amateur class. So a new event will be created
inside this my emitter from the instance
of event emitter class. Now, you’re taking
this my emitter and you’re emitting an event
called read file and then now when you are emitting
an Event now in this part you are registering a listener
and you are checking that event whether that event
has been occurred or executed or not. So here you are
diving my amateur which is our emitter and Dot
on function will check that these read file event
has emitted or not. So if it has been emitted, it will give
read event occurred. Otherwise, it will
escape this function. So now let us move
to our next module that is HTTP module. So as we all know
this HTTP stands for hypertext transfer protocol and we are using HTTP to create
client server architecture so that we can receive
the request and we can process those requests and then we can send
back the response to the customers or clients. You can see now this HTTP interface in node.js
are designed in such a way that it could support
many of the features of HTTP protocol. Now, let us take a look
at the code how it works. First you can see we
are importing HTTP module so that we can use it in
our code now as a second step. We are creating a server
using HTTP dot create server. And inside this we
are creating a function which will take the first
parameter as request, which we are receiving
from the user and the second parameter is response, which we will send back
to the user after processing. So now we are they are passing
a fast URL to get the pathname. They are reading a file then
they Creating a header with a content type text HTML and so that they can send
the data within the header. If they’re catching any error
in read file function. We will respond or they will create the header
with 4:04 error and the data will be missing else. If there is no error they
will create the header with 2 0 0 status that is okay and they will again
the content type would text or HTML and they will print
the data in your browser. At last this is response
to attend and this part that is listen 3000
will open your 3000 port and several recent
to the 3000 Port OK guys. So now let us move
to the interesting part of our session that is Hands-On. So in this demo you
will be clear how to use the modules
that we just discuss now. So we are going to use a couchdb so we should first install
couchdb on our system. We’ll go at the
couch TV website. Download it for Windows. And then I’ll install this file. So that now I have installed
cows debut on my system. We can take a look at couchdb. So for digging a look at County
we have to go at localhost 5 9 8 4 then underscore utils So
now you can see we have lots of databases over here
will lead is database. So I just specifying
the name address and I needed that database. Okay, guys. So now let us go back
and create a new project. We’ll make a directory name
of pip sample will navigate to the directory. So now I will create
package.json file using npm High 90 command. It will first ask
for the name of the project which I will keep
as end, Eureka. Then this asking for a version I’ll press
enter for description. You can give a description
whatever you want. But I am pressing enter. This is the entry point
of your project. The entry point would be
a DOT J’s file test command. I’ll press enter git repository. I’ll press enter and I’m not going to give any
of this arguments. It is asking that
the details are okay or not. It is okay. Yes. So now we’ll go back and check whether this file
has been created or not. We’ll go in example folder. Now you can see we have
a package.json file over here guys. I will clear the screen and now we’ll just install
all the dependencies that we are going
to use in our project. So we need Express. So I have to go
with npm install Express and using – – safe. We’ll save this module or this dependency
in my package. Json file guys. So now I show you
my package Json file. Now you can see
inside this dependency. We haven’t included this yet, but it has been
automatically included. So when you’re using – – save it will automatically
include that dependency inside your package
dot Json file. Now we are going
to install Jade. Now we’ll install. 900 these are all the packages or you can say these
are the dependencies which we are going
to use in our project. Now we’ll install body Paso. Now we’ll install error handlers
for handling error. Now we are going to install
server favicon and URL. So first we’ll go with URL. No, we are going to install so fabric on Twin to track
record in our website. Now we are going
to install logger. Now then we will install. Jason and at last we are going
to install Express session. Okay. So now going back to Pakistan. Json file. You can see all the dependencies that we have installed our here
and we have specified using – – save that it will
save the dependencies which we are installing inside
our package.json file now, I’ll do a npm install
command at last. And yes, I have just downloaded and installed
all the dependencies that are need to install. Now they’re just getting
back to our code. So first, I will create
my most important file. That is a DOT J’s file. Now first, we have to import
the downloaded dependencies, which we are going
to use in our project. So I will first download
all the dependencies. We are going to use
Express framework. So I am importing
Express framework so that we can use it. So now we have imported
Express framework, which we are going
to use in our project. Then we are going
to just import routes which will just
create after this. So we are importing the routes. Dot slash over here. You can see we
have got such over here. So it is search inside the root directory
for the routes folder. So now we are going
to import HTTP module. Order this will import
part module for working with absolute paths. Now we are going
to import URL module. And we’ll give the name
as URL encoded on next. We are going to import
body posture for reading our Json file. And we’ll give the name
of the variable as body Paso. Then we will import Json. to work with Json file I spy
working with couchdb will get the data in format of Jason or will send data
in format of Jason. So we need Jason package
to work with Json files. Then we have to
import the logger. Then we’ll import
method override. And we’ll name the variable
as method override. Sorry guys. I just forgot to mention this method
overriding our dependency. So I will quickly go
and mention it. So again, I’m going
to use npm install. Method override space – I feel safe. Okay. So now we have installed
method override now. This will work. So now we’ll import Nano. Require and this is nano. So by using this Nano will just
get connected to our couch DB. So we’ll enter the address
of cows DB that is HTTP. Colon slash slash localhost. Golan the port number
is five nine four eight. now we’ll specify the database
that we are going to use that is where our DB is equal to Nano
again for working with database or you can say cows DV
then we’ll use database with the name of a dress. Okay guys now let us create and
carve using Express framework. Okay. Now we are done specifying
all the variables and importing the packages now. We’ll go to our next step. Now I will use this app dot set. And we’ll set the port number. Address this application
will run or this application will be hosted. So we’ll specify port. We’ll give tosses
dot environment. Pout. And it should be 3000. It is not specified then we will again go will set
using AB dot set specify the views folder will create
this views folder in a while guys will first
end up working with this app dot JS file. So the bottle of views as we just discussed
so to give the path of use will use partnered join
and we’ll use the global object which we just discussed
that is that rename dir name. And then the folder
would be views. We guys and at last
we’ll set the engine. So the view engine is Jade. We are going to use Jade. Now we are specifying that the app should use
which all modules that we just imported now. So the first is body passed
on Jason for passing. We have body Pastor Jason
then we are going to use. body parcel dot URL encoded So that you can pass
our data in urls. Then we are going
to use method override. now Express we are
going to use static and we are giving
the path using pardot Joy. Similarly here also will give
first directory name using this double object dir name
and then we’ll give it public. Now we specified what
all to use now. The next part is. R dot get so hard the root location or at
the main website address. We should get
crowd sought index. Now an app dot post
the first parameter is will use create DB. but breeding database
functionality inside this and will give a function. The argument of the function
should be the request and the response. Now we Define the function. And using Nano. We are going to create the
database Nano dot DV dot create. Now the name
of the DB should be. We’ll get it from requ
dot request dot bald or DV name. Which will be specified by
the user and then again, we are using
a callback function. As we discussed and first
parameter is the error. now in this if we
are getting an error. Then response dot Cent. Are creating database? Plus the database name
that is req Dot. body. TV name and then
we’ll give return. After this spot we can use
request dot send and if there is no error. database Plus the name of the database
and paste it here and then here it successfully. Now these are
the first AB dot Post in which we have given
the code for creating TV. Now we will again create
AB dot post and this will be for the new contacts at we are inserting so
new underscore contacts. Sorry new underscore contact and then again similarly will go
with the Callback function. The function should return
request and response. so after this create this function So the name would
be request dot body dot name. and the phone would be
requested Bergdorf phone And I will insert this thing
we can insert these values inside our database for that. We’re using DV dot insert. now name should be named. I’m a phone should be your phone
and we have a third parameter make this crazy as
true now outside. This will also
specify this phone because this phone
is going to be the key and all the contacts are being
saved by this phone number and at last we are again
defining a function. For a terror are your body and your header
with three arguments? Now create and singly
I will again check if you have error. then request dot send
error creating contacts So error creating contact? Yes, and then
we’ll go with return. Then if there is
no error will send it. the confirmation message to the user contact
created successfully. Now we’ll create
another app Post in which we have view contacts function. The address would be / view. So now we will create
a new app Post in which we have view contact and the name of the URL or the extension of the URL
should be view contact. Elegant read a callback
function inside again. We have a request
and a response then we’ll create this function specifically
created variable variable dog. variable all dog over
here will specify four in other contacts Now I
will retrieve the values from database using
DV dot get function. request or body DOT phone So as I told you we
are saving the contacts and their IDs are
phone numbers now, you can search the contact
using your phone number. then we have revisions info
should be true. We true and at last again. As you know, I’m going to call
a callback function where we have error. And we have body now. If so if there is no error. We are going to console DOT log. And we print the body. Now if we have body. all dog silly plus equal to name
And it should print body name or your name. Plus, we’ll give
a break using BR tag and good friend phone number. It is pulled out phone. If you have this
if you have the body else. it’s all dog equal to should be
no records found. no records found
response dot send for sending the response. And we’ll send all dock. Then again with a post and the last part
is deleting a contact. So we are specifying
delete underscore contact. So we are again
creating a function. Which has request and response. now looking check using DV dot
get We’ll check for the phone as is the as phone is the key. Then the region info is true. Then again, we are going
to mention a callback function. with parameter error
and the body So if there is no error. David or destroy
for dealing the contact. And it has to
destroy requested 40. Phone. We are deleting
all the revisions that are stowed. Then we are again creating
a callback function with error and body. Now again, we are
checking for the error if you have error. response dots and would
be are deleting contact as if there is no error it will
respond with response dot Cent. And we have contacts. deleted successfully So now that we have created our code
will use stdp dot create server to create a server. And we are using the app
and we’re using Dot listen. And we’ll get the port that
from App dot boat. So we will get the port
using App dot get boat. Then go to giving a function. leaving the function empty and now we are going to print
inside our node.js CMD. Express over listening
on boat I need buying the boat. Using half don’t get towed. Now let’s we have almost created
our AB dot JS file. So now we are going to create
two folders inside our project. The first folder is routes. as we specified and the second folder is views
So we created two folders routes and Views and I
will go inside routes. So we’ll first start working
with views will create a new file inside views
and the name is layout Jade. And we are going
to create another file that is index dot Jade. So we first go inside layout dot
Jade will specify doctype HTML. New York St. Ml tag
Then we have had tag inside which we have title tag. So title equal to title. Now you can specify a link to a style sheet using
Arielle is equal to stylesheet. And then you can get the path of
this pie style sheet currently. We don’t have a path. So I am giving it / style sheet. / Styler CSS Then we have body. Inside body we
have block content. Now we are done
with our layout update files is so we’ll save this file and we’ll go back
to our index dot file. So he will extend our layout or Jade using
extend layout command. So this command
will automatically extend the template we are using
in layout dot j. Now the block content
we’re mentioning here first. We have H1 tag. And we have our new contacts. Then we have a form over here. Method is post. And your action should take
you to new contact which we just discussed
in app dot JS file. So first we have
named then input. The type is text. And the name will
keep it as name. Not a second parameter
would be phone number. I put bit again text and this would be phone
then we have a button that a submit button
for submitting the form. Sandy a second form
for creating a database so our new database
From the method is post. and the action is create DB. Then we’ll get
the database name. So then to type is
again we have text. And the name is DB name. Then again, we have
a submit button. So I’m going to add
a button here. That is submit and add new
it should be a database. Then we have a forum for enter
phone number to delete contact. will copy The Firm then whether it’s post
the action would be delete in the score contact as we specified in our a DOT J’s file
And then we need a phone number. Do the contact the name
should be phone. And at last we
have delete contact. at last we have view
specific contact form the method is again post. This one is view
underscore contact. We need a phone number. The name is phone. We have a submit button
and it would search the contact. So we’ll mention your search. And I’ll save the file. So inside views we
have mentioned layout Jade file and we have mentioned
the index dot file now. We’ll go inside the routes. Over here will create new file. The first file is index dot JSP. And the second file
I will create is. Create DV dot J’s. No, they first go
to index dot Js. export dot index equal to function we have
requests we have response. Look the function
response dot render. index the title is Express. how to save this file then
we’ll go to create database. So now we are again going
to use exports dot create. And we are creating a function. It has a request and a response. Nano door DB . 3 8 request or party
DB name The function we are going to create. so if error response dots and which we do error
creating a database Are creating the database? Okay. Then we’ll use return. and then we have
response dots and database created successfully. So now as we have
created our code, I’ll first take you
this app dot J’s over here. We have just installed or we have just imported
all the dependencies that we are going to use then
you can see we have sent in moment for this app, like Port views and view engine
that we are using that is Jade. Then we are specifying
all the modules that we are using
then using a Gad. We are specifying the root then
in create DB the functions like request response
and it should create. It goes DV then we have / new contacts using which we
are creating a new contact. Then we have /
view contacts using which we can view the contact. Then we have
delete contact using which we can delete a contact. So now that we have created
our app will now go at the couchdb address
so that we can see the cows DV. So the address is localhost
5 9 8 4 then we’ll add you till so that we can use the tables
and these are the TV. He’s pressing inside
our couch TV. So will Now quickly go and
execute an application for that. We will use note space
app dot JS the main file and now you can see that express service
is being on Port 3000. So now we’ll go and look
at those three thousand. So this is the form
which you created. We have add new contacts. You can add new database. We can enter a phone number
to delete a new contact and we can view a specific
contact using the phone number. So let us add the database
which we are using that is address and our new
database database address created successfully you
got the message, then we’ll go back
to local 3000. So now we’ll go and we create a contact
the contact is harmless and the number would be
nine six one five four one, six five two. Evan and we’ll add a new contact
contact created successfully. So let us go back
to our couch TV and check whether it
has been created or not. Yes. We have address table created. And yes, we have
96.5 for the number from which is the ID and you
can see the name is Thomas. So yes guys,
this is saving the contact. Now let us go and view that specific contact
using the phone number. So we’ll search the contact
and is the contact that is name is Thomas
and the phone number is 96.5. Oh one six five to seven now. We’ll go back again
and will delete this new contact that is nine six one five four one six five to seven
and we will be deleting. This contact contact
little successfully will go back to our cows DV
will reload this page so you can see that our database address
is empty the new Max inside this database. So now you might be clear how you can work
with node.js projects. And how do you in create
an application using node.js? Let’s check out
the agenda for today. So if we see
the agenda we would see that what node.js is will see
the node js architecture. We would see what
node.js package manager that is npm is no J’s modules. What is a
package.json file then? We’d go more
into node.js Basics, which is any language
Basics that is there. We will also move
into the file systems. There is an FS module in node.js that would enable
us to read file and write files on our server and then there is
an events module in node.js there is also
something called HTTP module and there is also
something called Express that we would be going for which
is a node.js framework. So at the end we would be going
through the entire code and we’d be going
Through a small demo as well which would be something of a mix of all
the node.js concepts that we go forward to so, let’s go ahead and see
what node js is actually so if we speak
about node.js it’s a powerful JavaScript framework or I might say,
it’s a run time where you can run
JavaScript on the console. It is developed
on Chrome’s V8 engine. So if anyone doesn’t know
what a V8 engine is, let me just tell you what it is. So if I check out Out the V8
Dev the official website of chromes V8 engine
you would see that it is an open source high-performance JavaScript in
webassembly engine written in C++ and you
know more or less. This is the engine
that runs on the Chrome browser. So you would see that whatever
your Chrome browser understands. It would be the same thing
that node.js also understands. So the creator of node.js
thought that okay. This is an open
source JavaScript engine. Why not Implement
that in a platform which enables you
to And JavaScript on the server. So this is the reason why
node.js understands JavaScript. So that is one thing that we would like
to also keep in mind. So it’s something
that runs on Chrome’s V8 engine and it compiles JavaScript
natively into the machine code that is all because of
chromes V8 compiler that we have and it is basically used for creating
server-side web applications and also Network applications
actually so mainly node.js is used for and basically if it is a data intensive Civ
application node.js is something that is specifically
made for that. Let’s see how that happens. Basically if you talk
about the features of node.js it is open source, it is simple and fast it
is asynchronous highly scalable. You would face no problems in scaling
your node.js application. It basically works on something called a micro
service architecture as well. And it facilitates that micro
service architecture really. Well, it is
a single-threaded model which means it is
not resource intensive. And yet it is fast, you know yet it allows things
to be done in parallel will see how that is done. And then there is no buffering. Basically, there is no waiting
as far as node.js is concerned and that is because of
a concept in JavaScript, which is called event Loop. We’ll see more about that
as well and it works on so many platforms. So that is some brief
of the feature. Lets see node.js architecture and before going
into the architecture of node.js we would also like to see the traditional Lecture
so traditional architecture if we speak about traditional
server architecture is basically where every client request
is managed by separate threads. So there is a multi-threaded
model going on in normal server architectures like Java, for example, it’s
a multi-threaded application or a multi-threaded
set up altogether. So where your web application
runs on multiple threads and various client requests
are processed parallely now, there is nothing to take
away from this model because it is really good and it
has been working throughout Us when Java is at this point
of time Java is actually one of the best languages and secure language has
to be programmed in. However, this is resource
intensive because you can see that there are so many threads
going on in parallel which means your server
or your machine as should be something which is capable of running
these many threads. However, if you talk
about node.js it only runs on single thread and still it processes
requests in parallel. So one thing that I
would also like to clarify by here is in the background
or maybe under the hood if you may say no just doesn’t process
any requests in parallel, but it goes
through an event Loop where once the request comes
it goes into the process and node.js doesn’t wait for the output
of the request to come in while it takes in
the next request. So as in when the first request for example gets the output
it would just respond for the output or with the output to the respective
client basically, so, you know in the background or under the hood Is basically
running only one thread which is not resource intensive and it is at the same
time processing requests from so many clients and it provides a Virtual Field that everything is
running in parallel, but everything is not so that is all because of even Loop
that is going on. So that is basically the
architecture and then we talk about something very important
as far as node.js is concerned which is called
node package manager now, it is called npm in short and it was primarily known
as node package manager, but nowadays it is not known
as node package manager because it is doing so many
things then package management. It’s doing so many
other things as well. We’ll see what it is. So if we talk about
the official definition, it’s a package manager
for node.js packages or modules, which has been added as
a default installation from node js version 6 or 0.6 onwards
and then it’s stuck. It is already there
in any installation that you do in node.js if you are a Java programmer you
can relate this with Even and if you are a PHP programmer, you can relate
it with composure. So it is the same mechanism where npm has a repository
of so many libraries and then the repository serves
whatever the package you need for your project. And if we talk about
the features it provides and hosts online
repositories for node.js which can easily be downloaded in our project using
a command line. So it provides a command
line utility as well and it also Also allows you
to manage the repositories for the versions of libraries that your project may use. So we’ll see
what are the versions and what are the libraries
that we are talking about. So the library is that I’m talking about
when I say libraries. It is just node.js modules so
node.js modules or if we talk about the module system. There are core modules. There are local modules and then
there are third-party modules. So code modules are the ones that are actually available
in the default installation. Of node.js you don’t have
to program anything. You don’t have to install
anything else just node js to get the core modules working
few of them are listed in here like HTTP URL query
string will be using them and there are
some others as well, which we’ll be using today
and then local modules. It is something
that a programmer builds. It could be a function. It could be an object P
anything the programmer builds and the programmer exports so that other team members are other programmers can import
that module and use them. So it’s something that a programmer would builds
like a custom module and then third party module. This would be installed
through the npm repository. So if we speak about npm
in this particular case, let me just also open
up the npm website. So it is basically
n PM J s.com and you can see that it has so
many repositories. There are so many companies
that it is serving and all it is so many
repositories available as well. Let’s just search
a few repositories in this particular case. I’ll search one of them. If we talk about react
you might have heard of react react is one of the repositories it
is available in npm. You might have heard of Angela. That’s also one of the libraries that is available
in the npm repository. You might have heard of jQuery. You might have
heard of bootstrap. These are like naming
just a few of them. There are so many
so many repositories that are available even express that we are going to use is
one of the libraries that is available in this
particular repository of nbm so you can see here
that Express is one of the That is to be used. So we’ll be installing
expression C. By the way. This is the way you install
any third-party library from the npm repository. Alright, so that is the 3rd party modules and you
would be using npm install or in PMI to be in short to
install this particular poetry. Now, let us see the package.json
file package for Json file in node.js is the heart of the entire application is
basically the Manifest file that contains the metadata
of the project now at this point of time. Let me just create a node.js. Each project and see
what this package dot. Json file looks like and then we’ll analyze
what this file actually is. So let me just create
a folder here at Eureka and in this folder. I’m willing to you know,
initialize a node.js project. So let’s say I’ll call
it nodejs demo, or maybe I’ll call it
task manager even better. We’ll try to create some of the task manager
functionalities in here and it will be an API that will be creating
or a Service that will be green. We’ll see what it is. So in this task manager, I am going to initialize
a node.js project. And in order to initialize
a node.js project you need to have node js installed
in your computer, which means you have to go
to node.js dot org website and you can download
this LTS version. This is a currently release
which is basically experimental. It’ll have all
the latest features but it is prone to be
erroneous at some time. So, you know, generally for I meant
you don’t use this one but you use this one but say if you want to check
out the new features, you can also install
the current release but will always go with the LDS and I already have this
installed now clicking on this will allow you
to download the MSI file that is the setup file
and then you can just double-click on that set up
in just install it on Windows and in Mac as well. The setup is really simple and in Linux as well probably
will give you a dip file. For example, if you’re going for a boon
to so the setup of node.js would be really straightforward
but a To the setup is done. What you have to do
is you have to just check whether node.js is installed
in your computer or not. And you’d be checking
it this way node – we will give you the version
of no Jason can see that I already have it installed and I have the version
10 point 15.3. That is the LTS. And then I’d also check npm. If you recall we saw
that npm is something that comes in inherently
with node.js all together. So we’ll be going for npm –
be that will give you. The npm version so we’re all set
in we’re all ready to go. So let’s go for creating or
initializing a node js project. It is something
like this and p.m. In it dot which means
current directory. So if we just hit enter
I think dot is something that is not to be done. Right? So this would ask
you certain questions. Like what is the package name? Let’s say I want the same
package name as task manager. I want the version 2 B1. Okay, the Let’s say this is
a task manager project. Right entry point
would not be significant at this point of time. So we’ll just keep it as it is. No test command as
of now no git repositories. I’m not going to even commit
that to a git repository and know keywords
as well author. I can say at Eureka and license no meaning as
of now for a license because we are not going
to make it public or anything. So it tells us that is it? Okay and also tells you that it’s about to write
to this particular file package Json inside our
task manager folder. So which means after I say, yes, there is a possibility
of this being written into a file called package.json
inside my project. So let’s go for hitting enter. Let’s say yes, and if I now
check out my older you see that the package.json file is in let’s just open
this up in our editor. You see that here is the package dot Json
available with every information that we provided. Now. This is a very basic
package dot Json. There’ll be so many things
inside a package.json file and a normal
or a real-world baxter. Json might look
something like this where there are so
many things like the name of the project is
there then there is something called version as
well description of the project. What is the starting point of the project which is
Humane script to run first. There are certain scripts. We saw that we in provide any test command and then
there are certain engines. What all tools do you
use to run this project? Who is the author? What is the license? There are certain
third-party modules that we would like
to have you can see in this particular example that there is Express that is there as
a third party module which will be using and then
there is Dev dependencies. Like when you go
into a development environment, like for example our computer,
they’ll be certain dependencies. There will be there
and that will be listed inside the dev dependency
then there is Repository. The information which we
didn’t provide actually if you want to see what are the bugs and all
there has to be a separate URL and the homepage so that is your package dot
Json file which got created. By the way, when we initialize
the node.js project and you can also manually
create it but it’s better that we go for and be
a minute as a process. So now let’s go for node. Js Basics. So if we talk about Basics, it’s like any language
Basics and the main thing that we need to check out as
a basic is the data types. So there are
certain primitive data. Types, there are
certain abstract data types, like non primitive data type. So primitive data types
as string number Boolean null and undefined abstract
data types are object array and date to name
a few by the way. There are so many others but these are to name
a few so say for example, if I create an application, let’s just create a string
as a variable and let’s see how that works. So, let me just create
an app dot JS file and in this I’ll
create a variable and I’ll be very specific. I’ll say first name and I’ll say First name
to be at Eureka. This is a variable that we
declared and if I want to show this variable in my console, I’ll just do console
DOT log first name. So when I do this
the main perception is basically if I want to run this app to JS. I might have to
create an HTML file where I might have to include
this app as a script file and then I might have
to execute the HTML file and open the console to see
this particular output. But if you have installed
node.js on your computer. Which we have you
would actually be able to run this particular
app dot JS really easily. Let’s run this one for that. I would have to go
into my project. Let me just clear
the screen and run this one and really simple to run
a node.js application. It’s simply node. And the file name that is app dot JS
and you can see that it displays
the first name in my console. So whatever I do as console DOT log
gets displayed in my terminal that is something
that I would like to keep. Mind and remember
this is a string that we have created but there is no concept
of a strict data type. So basically the first name
can also be something like this. The first name can be reassigned
to let’s say a number and that will not be
a problem for JavaScript. That is the core nature of JavaScript is
not strictly typed. So that is something that I would also like
you to keep in mind. So there are so many data types that are available which
we have created a string and then there are
so many others. This is how you create
a variable that we already saw. Oh and then there are operators. Now as I say like
there is something that is already similar to all the other programming
languages variables are one of them operators as well. However, there is one operator
that is pretty unique. And that is
the triple equal to sign. So say for example, if I go for something
like this bar, let’s say h 1 is equal to 30
and for H2 is equal to 30 and then let’s say
VAR result is equal to 8. One double equal to H2. I am using this double equal to similar to any
other binary operator. Like I might go
for plus and similarly. I’m going for double equal
to now this is because it is
the comparison operator this would return either true or false and this
will get stored inside the result variable. So this time I know that you might have guessed it
it would be returning as true. And if I do the result if I go for console log
of result in if I execute this after chase, you see
that it returns a string. True now if I go
for a string, all right, and when I declared a variable
in the previous example, we saw that there is
no strict data type. So this would not actually
check for the data time. This will just check the value
and though it may seem that it should give us false. This would give us true
and the fact is like JavaScript doesn’t care
about data types. So if say for example you want
to also compare the data types along with the value
instead of double equal to use triple equal. And that way this
would give us false. There is so much
going on inside or in this particular two examples
that we have but for now, you can remember
that double equal to doesn’t compare the data types while triple equal to also
checks the data types, but then again there is so
much going on under the hood, which it’s not in the scope
of this particular session, but just keep this
in mind as a unique operator that is available
for JavaScript specifically and then there are
certain other languages that might have these operator. But JavaScript is
the one that came up. But this all right. So this is one thing
and by this time you might have got an idea on
how we run an application or how we run a file in node.js. So this is one other thing that I would also
like to mention where functions are created
say, for example, if I have a function to create
let’s say function say hello and I pass in name inside it and I return let’s say
hello plus name a plus here is a concatenation operator and
that would return Turn us a name or a Hello message with the name
whatever we have passed in so I can do this like console DOT log say hello
and let’s say hello to Ravi. All right. So if I run
this particular file, it will give me whatever output
we expect which is fine. Now one thing I would also like
to tell you is in JavaScript. There is a provision where you can create
a function without a name an anonymous function, which is also something
that JavaScript came up with a function with no name
and If this is the case then how would you call the function? So for that you can do
something like this VAR say hello is equal to a function
something like this. And then the rest of Thing
Remains the Same function gets called as as normal. What we have done is we
have created a variable and inside this variable. We have assigned a function
rather than a value. So and then we are calling
the variable as a function. So again, if I run this particular code
the output would be the same just keep in mind that function
here can be a Anonymous in JavaScript, all right, and then objects now object. There are two ways you
can create objects one is through object literals like were let’s say
a student is equal to a constant object which has let’s say name Ravi
and email robbery at gmail.com. For example, right
and then we can do a something like this console DOT log
student dot name right and then student email and so on. And that would display
whatever the name is basically so an object dot
property can be done. And then there is a Constructor
pattern as well available, but it’s okay if we don’t go for that but then
there is another pattern which uses object Constructor
to create an object now going into node js core modules. One of the modules
is file system. That is the fs module FS module if you want to include
or any module if you want to include you go
for this syntax a variable is equal to require. Oh and the module name
and this would be something like this for example par FS
is equal to require FS. Now for this FS module you don’t
have to install anything else but no dress has to be there and which is there and Fs module
would be available. It’s a core module in node.js. So let’s say for example if I have a file
called hello Dot txt, and it has some data. All right, and if you want to read
this file I’d be able to do. It’s like f s dot read file and it asks me the path
of the particular file. So let’s just give the path. Basically I can try
with a relative path first. So it will be basically
Hello dot txt. Let’s see what it gets
and the second argument that we need to pass is
the Callback function so node.js or any JavaScript platform
would work more on the basis of callback function. That’s how it creates
the virtualization of so many things working
at the same time. All right. So I would go
for a callback function and this function
Anonymous function would go for two arguments one is
error and one is data. Let’s see if there is
no error then we’d go for logging the data
inside the console. Let’s see. What data we get. All right, so if I run this file now Hopefully
I should get the contents of Hello dot txt file the sea. Here, I don’t get the content
but I get something called a buffer that buffer is
basically a some container that contains raw data
out of this buffer. I can get the string basically. So let’s say if we go for buffer dot tostring
which is a function which will convert
the data to a string. So now it will give me
whatever the content hello world has and similarly if I for example have to write
something inside a file. Let’s say I want to write
something inside a file and then once the file is written. I would like to read
out the file. We do something
like this FS dot right file and write file would
again go for the path. And I would be going
for the data as well. And the data is something
like something like this and once I go for the data data
could be any data type by the way could be Boolean
could be object could be any data type and then I go
for a callback function. The Callback function
would have something only one argument here, which is error
if there is no error like if no error then I
would like to read the file right so Then I can go
for f s dot read files. I can just take this whole thing
and I can put it in here. So what I have done is I
have written something into the file and if there is no error after
writing whatever I have written, I would be trying to read
that file and in here if there is no error I would
like to display the content. So hopefully this should give me
how are you or maybe let’s see whether it overrides
whether it appends. Let’s see what happens if I check out this you can see
it gives me how are you? And if I go into Hello dot txt, you can see
that it has overwritten the particular content
that was there before. So this is an f s module demo. This is how you would be reading
and writing files. I might like to also try and read and write Json
in some Json file that might actually
give me a feel of an API that is being created. Let’s see how that goes. Then there is
something called events, but before going for events, I would like to create
a server first. Let’s just create
a server and you know, the events are basically something that we
would be working with where you would be
committing certain events and then we’ll be listening
to those events. Let’s see how
that whole mechanism works and how the event handlers
would work and all but before events. I would like to go into creating the server
through the HTTP module because server is also
a network application, which is something that node
js would enable us to create so we’ll create a server
through the hdp modules and then I’ll come
back to the events. Let’s see how that goes. All right. So let’s just get rid
of this FS related code and I will again go
for the fs code in some time. I’d also get rid
of the Hello dot exe. I don’t need this. Right. And then what I’ll do is
I’ll create a server in here before the server. I’ll go for VAR H DP
is equal to require HTTP and then there is something which is really simple
to create a server in node.js as opposed to all
the other languages. So is something that a programmer would create
so say for example, if you compare no TS
with JSP or Java, there is Tomcat
Apache web server that is already available if you compare
no chess with Doc. Net there is is server
that is already available. If we talk about PHP
there is Apache server that is already there compiled and available in node.js
there is no server so the concept
of node.js being a server. It’s something that is not true
in node.js it is just run time which enables us to run
JavaScript on your machine so that you can create a server if you want to and
creating a server. It’s not a big deal
in node.js this is how you create a server
HTTP dot create server. That’s it and as Save it
in a variable called server and my server would listen to
the port number 3000 the server Dot listen 3000. Alright, so this is what
your server would listen to and if you want you can also provide
the hostname here which is by default localhost. But if you want explicitly can provide Local Host
as the host name. So your server would be listening to Local Host
and 3000 and after it, you know starts listening. I would also like
to provide a mess. Message and again
the Callback function or an anonymous
function would come into the picture so function. So log will go for Server
started on Port 3000, right? Okay. So what have we done? We have simply created
the server by HTTP create server and we’re listening
on port number 3000. So that is what it is. And then at the end we
are displaying some message on the console. So let’s see one thing that you’d notice is
in the other programs. The application actually ended like once you are done
with the whole program. You see that you get
the command prompt back. But in this case when we are listening the server
is constantly listening to the port number 3000. So the application would not end
in this particular case. You may have to end
the application forcefully by hitting Ctrl C. So, let’s see now if I run it you see
that server started on port number 3000 and
the application is not ending. Alright, so if I go for say localhost port
number 3000 Something that might happen you see here that the request is sent to the server the server
is not responding because we have not
programmed our server to respond with something. So here the server
is not responding while the server is running. All right, so if I stop my server you
would see it would tell you that the site can’t be reached. So basically what that meant was previously
the server was running. So if I for example run
the server this would again, let me just open up localhost. Thousand this will again start to load but the messages the
side can’t be reached won’t come because the server
is still there. The site is reached for the server is
not responding to us. So let’s program our server so that it responds
to us in which you go for a callback function
inside your create server method and this callback function
has two things request and response to arguments. And if I want to send a response
in this particular case you go for response dot end. All right, and if I
let’s say server works. That’s the message
that I want to send. All right. So what this would do
is it would send a message to your browser
saying server works. So let’s just take that message. So for that because I have changed
something in my app doj’s I might have to stop this and I
might have to restart my server. So server listening
on Port 3000 and if I now refresh you see that it gives me
the message server works. So this is pretty cool. We have created a server in like
almost three statements, right? So that is something on
how you create a server. But generally what people do is
people use this functionality of creating a server
along with Express and then create a server through Express the framework
that we are talking about. So we will see how to create
a server through Express. But before that let’s move back
to the events and let’s see how events would work
in this particular case. Now when you talk about events,
there are two methods. That you would be generally
going for one is called Emmet and one is called on so remember these two methods
Emmet and on let’s see how we can make it work
and what our events basically or how an event ID
system would work. So in that case we
again use a core module which is called events. So our events is equal
to require events again, a node.js code module. You don’t have to do
anything to include this one and And in this particular case, but also create
an event emitter. So far event emitter is equal
to events dot event emitter. And it should be a new event
demented actually right now. If we go for the presentation
you would see that they have also
emphasized on to things that is on and Emmet. So we will see what
these things are. All right. So now in this particular case, let’s go for something
called event dot on even dot on or not even turn on actually
even Demeter dot on this. Action, it’s basically
an event listener. Now whenever an event occurs
this function would listen to that particular event. All right. So this would listen
to the event an event dot on we would have the name
of the event and let’s see what we can do as a function. There’s a callback function. That is also something
that is involved in here. So we will see even diameter
where it will go for event dot on and something
inside as arguments. But as of now,
I’ll just keep it this way. And I’ll simply go for something
on the inmate side of things. I would like to emit
an event and let’s see how that goes. All right. So in this particular case, what I would do is
whenever there is some requests on the server,
I would like to emit an event and then I would like to listen
to the event and log something on the console, right? So, let’s see in
this particular case. I’ll go for event emitter dot m
it and I can name the event anything the event
that I’m trying to You Know M. It is Basically, someone has requested
to the server. So what I would say is on request maybe just
someone I can name it anything. That’s why I’m naming
it a very bizarre name. So someone requested
it is an event name and if I want I can pass
in some data as well. But as of now I’ll just
keep it this way. I’ll just omit someone requested and when I would like
to do something when someone requested so I would go for even diameter
dot on someone requested and That is a callback function. Let’s just go
for console DOT log and I’ll just say a request
has been done on the server. Something like this
on the console. All right. So this is an event emitter and basically on is
an event listener. Alright, so we are
triggering an event or maybe I can call
trick even trigger that will be a better name. So this is an event trigger
and this is an event listeners or even admitted it m
it is an event trigger and even tomato dot on
is an event listener. So whenever this would
be triggered this event would be executed. And this function
would be executed. So let’s see. So if I now read on my server because I have changed
something in my node.js app. I’d have to rerun this. It says server started
on Port 3000. I will just refresh
and we’ll give me server works. That is fine. But if I check out
on the console, you’ll see that a request has been done. If I refresh again, you see
that a request has been done and then there are
two requests that is because one is checking whether the method get
is available on the server or Not in this the other
requests is basically executed with the method get actually so
there are two requests but we don’t need to you know, go into detail
in that particular case. However, one thing is
for sure that whenever the event emitter is triggered
we can execute the on method and we can listen
to that particular event. All right, and
if say for example, I want to pass in some data,
let’s say test, right? This data can be taken into the function
the anonymous function. An argument and I
can just display that for example data
that should display test to me. So whatever you pass in could be
a string could be a Boolean and object anything
would be taken into the function as an argument can name it
anything and you can display that particular argument is well
inside the console. Let’s rerun our program
and let’s refresh the server is requested and you can see request
has been done on the server and test this particular data
is also been displayed. And so that is the event
diameter you can emit events and you can listen
to events whenever the event would be emitted
the listening would happen. All right. So this way, you know, our node.js server
becomes an event ID server and this is really good. If you want to create
a chat application or any real-time
application event handling would actually help us create
a good real-time application. So that is where this
would basically come into the picture. So, you know,
you can check out socket IO. That is a lie. Equals socket IO which helps
you to create a chat server. This would heavily
use event amateur on and emit methods, right? So this is the one now we have created the server
using the HTTP module and if we talk about the server
you can always see that the client would be
either a web browser or mobile browser
or an application that might request
to your web server and the web server
would contain your server file. That is the app dot JS
that we created plus some application logic
as well if you want and the Chick might be taking
data from the data layer or any external system and it might be serving
the request back to the client. So basically this data would be
taken into the business layer and to the web server
and the web server respond, like response dot end sort of thing would happen
in this particular case. So this is how the request response cycle
would go on and then we see here that it’s creating a web server
using node.js you can pause this particular portion. You can also try out
this whole thing. I think you would know
how a server is created. You already know that and then there are
certain other things that are listed in here which you can try now we’d go for third-party module
or a third party package. You may say or even we
can call it a library. It is called Express JS
next press JS. It is a node.js framework, which is basically facilitating
the management of data flow and routing as well. It is very
lightweight and nowadays if you create a node application for an a API or a web server
expressjs is something that you definitely have so it’s like basically the part
of the language itself right. Now. It facilitates faster
application development. It provides applications with template engines
two of them are Jade which is nowadays known as pug and ejs
to of the very popular ones, but then there are so
many others it helps, you know, building single page
applications building multi-page applications as well. It helps you to connect
with any database MySQL mom Be read as Etc. The configuration is
really simple will see how you create a server
in express its really simple and it also helps
you to handle errors or maybe it gives you
a good facility to Define error handling processes so that your maintainability
of the application is something that would work. So let’s just create
a demo in Express. Let’s see how that goes. And as I said and be using HTTP and express together
To create a server that is an ideal way
of doing it. So let’s just get rid of
everything and let’s just start with the express server for that
and have to include express. Our Express is equal
to require Express. Now when I do this and if I
execute this, you might expect that this might work but remember Express is
a third-party Library. It’s a third-party module. So in that case you would have to install that particular
module on your system. Obviously if I run
this Let’s see. If I try to run this this
will give me an error cannot find the module Express. So let’s install Express in here
in order to install expression. Remember the file structure
that we have. We have an app dot JS
file a package.json. There’s nothing else in here. So let’s just install Express. Let’s say npm install Express. You can go for install
the whole word or I as a short form. It’s all fine. I didn’t stall Express and this would actually download
Express from the npm Repository. Re and install it on your local machine
and you’d be able to also see where that Express
exactly gets installed. So you can see that expresses installed
48 packages installed. All right, and now you can see a change
in the file structure there was already package dot Json
an app dot JS in my file system while there is a folder
called node modules created and package underscore locked. Oh Jason also created the node
modules folder would actually have the library Express and then there are so
many other libraries that Express depends on Which
are also imported and installed. So now if I execute this this
will not give me an error. However, we have
not created a server. We have not listened
to a port number. So we’ll be doing
that through Express. Let’s do that. What we would do is
Express and brackets. Like we’re calling
expressed as a function and we’d be saving it in server
a variable called server. So this is how you create a server
in Express pretty simple. And what we would do
is server Dot. Ian 3000 and then the same drill like 3,000 and not specify
Local Host because I know that it is localhost and at
the end a function that tells that the server
is listening to 3,000 so console log server
listening to Port 3000. All right. So the express is required like
included a server is created and the server is listing now, let’s just rerun
the application and because the server is listening
the application would not stop. He’ll keep on
listening and let’s go into our browser and let’s
refresh this time around when we refresh you. Do not get that
whole loading thing. But instead you’d get
an error and it says that it cannot get / this is actually not an error
on the server side. The server is all okay. The thing is that we have not programmed
a server in a way that it would address the get
request on the root path. This is our root path. There is no get request
addressed on the route, but that’s what it says. As so what we’ll do is we
will address the get request. I will do something like this. We’ll go for Server dot get but actually run
the naming it a server because I would like to use
this server identifier somewhere else in some time. I’ll go for app now go for app
Dot listen this time around. All right, so app
and this also tells you that you don’t have to name
it server can name it anything that you want. So this is my Express app
and app that get / and a Request and response the
same request response function that we had but it
is now specifically for the root path and in here I can go for a response
dot either I can go for end or I can go for send
and response but send let’s say Express works. I can also let’s say
I have an H1 tag so that I had browser
displays it as a heading that can be done. And now let’s rerun
the application and hopefully the root path get request is
a Dressed so let’s go for it. I’ll stop this we
run the application. By the way, there is a utility called Node
1 which would help you to run your application automatically once there are some changes but this time around
we’ll just you know, read and the
application manually. So if I now refresh you see that it gives you an H1
which says express works. So which means this is all done. Your root path is addressed. Your route get request is done. But what I would like to do is I would like to go for something
called tasks, okay. There is no route that is tasks that is defined yet
not programmed our application so that it addresses
the get request on the tasks in this get requests. Let me just go for the get request first
app dot get slashed asks is what I want to have
the server address and function request
and response question response. So now I can go for response dot send I
can go for another H1, which says tasks work
if I now restart. At my server and remember
to stop myself. I’m just pressing in control C. And if you are a Mac
User is command see that is stopping the server. And now if I refresh you see
that tasks work this works. Now what we have done here is we
have created two routes one is app dot get for the root and one is apt-get
for the tasks. It could be app dot post. It could be after put up dot. Delete AB dot patch anyone
who is familiar with the rest. API would be Of it all
these words your server can address any request get put
post delete patch any requests that you want to address here. We are just going
for get requests. All right. So now what I would like
to do in here is I would like to return something from a file like for example at create something
called DB dot Json. So Json file and in
this Json file, I’d like to go for let’s say
a key called tasks and tasks would Be
basically an array of tasks which would be learn node. Js now in JavaScript. You can create area with square brackets similarly
in Jason’s in text. That will will work. Let’s say learn
JavaScript learn Express. So these are the three tasks
that I have. And what I would like to do is
from this Json file, I would like to read
these three tasks and I would like to return
them as a response. So, let’s see what we do
in this particular case. Let’s see how that works and that Bonds has to be
in this particular case right in this particular response dot
send I’d like to read the file and then send the response so we know that what we use
for reading the files, which is the fs module
and also we know what we use to create
a server as well here. We have created
a server by Express but generally people
always use a mix of Express and HTTP to create a server. Let’s see how that happens. What I’ll do is our first I’ll go for HDPE
is equal to require. HDPE and for this week. Obviously, we don’t have
to install anything. It’s a core module and I’d go for VAR server is equal
to HD P dot create server. Right and app is something
that I’ll be passing in as an argument. So my server is created which has all
the goodness of Express. So instead of app Dot. Listen, I’ll go for Server Dot. Listen. All right. Again, this is all stay
the same will it will listen to the port 3000
and then go for a message. It is server is listening
to Port 3000 and so on and so forth. So what we have done is we
have included Express. We have included HDPE. You’ve created an Express app. We have created a server
with the Eight server method we have passed on the express
app as an argument which means all these routes
would be something that would be addressed. And then at the end we
are listening to Port 3000. Now, this is the common way that people would use
to create a server and an Express app together. Alright, so now
let’s rerun our program. Let’s see what happens. It gives me an error
and that is a typo. So let me just resolve that. Right, and now let me
just rerun the program. So again listening to Port 3000 this would
not probably give me anything. You can see that it
is still loading because there’s no response that I have programmed in here
which I would like to but say, for example, if I go to my root path,
it gives me Express work. So that is all working fine, which is cool actually
to be using Express and node.js server together. Alright, so now I’d like to read
from the DB dot Json. So obviously I would need Need
VAR FS is equal to require fs. And in here at go
for f s dot read file and go for the path. That is T V Dot Json the
relative path and the Callback which has two things. As far as read file is
consent one is error and one is data. Let’s go for console DOT log
or not even console DOT log. Let’s just do a Onslaught send once you have the data you go for data dot tostring
would be something that would be going
for let’s see what we get in this particular case. I would have to restart
my server and this works and if I go for tasks
this gives me an object that has an array of tasks. So I don’t like to have
the whole object, but I just want the array
of tasks to be there. So one would think that okay. I can do something like this. This like let’s say
what tasks is equal to Theta dot tostring dot tasks. That is the object that we want to get
but the thing is like this tostring would convert
the whole data to a string which would not have
that property called task. So if I want to convert
this string to a Json I’d like to do something
like Json dot parse. One of the codes have
a script methods this would then have the property called tasks. Json dot parse data or to Which
we are passing in as an argument and that will convert
this string to a Json and then I’m going for tasks
as a property at the end. I can simply send in tasks. So that will give
me the plane are a that we require. All right, and specifically if I want to send Json I’ll just
do response dot Json rather than going for response to send
so I am being specific in here. So let’s just stop this
and start the app again and refresh and now you see
that you get the array. Now you might not get
the same output that I am getting as
far as the color is concerned because I have a an extension which is running
in my Chrome browser that is adjacent reader
or something like that. I had installed it years ago. So that is the thing. But the more important thing is
you’re getting the plane array from the file that we have. So in this case, we have used almost all
the things that we have learned. We went for Express. We went for HDPE combined. It was a server that was created
and then BFS module to read something from the file
this Would probably be something that we would like to go
for from a database. Like we’d like to get a database
connectivity done and get all the data in and then read data
from this particular case. But as of now read file
would be enough and this might give you
a good introduction to node js. So this is what it is
and further down. You can try out more routing
and more database connectivity in node.js see how
that goes for you. Today I will be talking
about what is no chairs and how we can use npm
along with the no chairs. And after some
of the theoretical Concepts, we will see a small demo that how you can start with
node.js and npm development. So let’s get started. So the agenda for this webinar
would be I will be talking about what is npn. What is the need for npm? What are the different npm packages how you
can install npm? And how you Can use
the different npm packages from the npm site and then how you can install
node.js npm at the same time. I will be talking about that how you can use
package dot Json file, which is the initial or basic
file in any node based project and then there will be
a small hints on so this will cover
the complete agenda for this particular webinar. So let’s get started. What is npn? But before we go ahead
and learn about what is n Again, I just want to tell you
that what is node.js so that you have a basic idea
of node.js also so node.js if I talk about node.js is an
open source server environment. It’s absolutely free as it is an open source
environment and it uses JavaScript on the server. Now, if you have a question that what can node.js
to so let me tell you that node.js can generate
Dynamic page contents. It can create open read write
delete files on the server. It can collect form data or it can add delete modify data
in your database. So that is all about node.js because before starting with npn
you need to have node.js in your system in your laptops and your environment then only
you can go ahead with npm. So npm is stands
for node package manager. It’s a package manager for
node.js packages and modules, whatever you like. It is basically responsible for
managing all your GS packages because it’s a node
package manager and modules which are present
in an application. So if I talk about a high level
node package manager provides you to main functionalities. So what are those two basic
functionalities of npn? So first one is
that it’s an online repository. You can say for node.js
packages or modules, which are searchable on search dot node.js dot orgy
and the second thing is that it provides you a command. Line utility to install
node.js packages. It allows you to do
virtual management and also the dependency management
of node.js packages if I talk about npn npm
comes bundled with node.js so when you install
node.js npm also gets installed and this happened
after virgin 0.6 0.3 of node.js so you can say npm is
the default package manager for node.js and it is completely
written in My script so now we will see
what are the basic advantages which npm provides. So first and foremost,
it is very simpler than soap. It’s a default package
manager for node.js. It is completely open source and
entirely written in JavaScript. It is developed by isec shuttler and it was initially released
in 2010 in January. You can say that n pm has now become the world’s
largest software registry because it manages has
all the packages and modules for node.js and it consists of also
command line client npm. As I said that you
don’t have to install npm in a different way or separately because it gets automatically
installed into the system with the installation
of node.js and as I said, it’s a free and
open-source package system. So all the required packages
and modules in node project. You have to install
or get install using npm. So I am talking
about package and module. So what is a package
basically so a package contains all the files which are needed for a module and modules are
the JavaScript libraries. So basically a package if I talk about a package
and a module so a package contains all
the files which is needed for a module and modules
are the JavaScript libraries that can be included
in a node.js project according to the requirement
of the project. So what are the main
functions of Npm. So we talked about what are the two main
functionalities of npm? So first one is that it
provides online repository for different packages and modules for node.js
and we already talked about. What is the package and what is the module
so package is nothing but it contains all the files which are needed for a module and modules are
the JavaScript libraries that can be included
in a node project according to the requirement
of the project. So II mean functionality of npm. Is that it also White CLI. Now what is the CLI
command line interface? So it provides
a command line utility or you can set provides
a command line interface which helps the developers in locally interacting
with the system which means that npm and node.js provides
a command line utility so that the developers can
install the node.js packages. They can do version management. They can do
dependency management of the node.js packages. Now if I talk about that how
it do the dependency management and a version management. So npm basically can install
all the dependencies of a project through
the package dot Json file, which we are going
to talk about later but it completely do it using
the package.json file it can update and uninstall
the packages and so on. So now we will talk about
what is a need for NP n YN p m– is required. So if I talk about
the need of the npm It helps in incorporating
the pre-built packages into our project second is that it assistant downloading
various Standalone tools, which can be used right away. So if I talk about npm, it allows you to adapt packages
of code for your apps or incorporate packages
as they are. It allows you to download
Standalone tools. You can use it allows
you to run packages without downloading by using n PX developers often use
npm to Share your code with other npm user. So you can share your code with other users
other developers using npm in the other weight also
allows to restrict your code to only specific developers
at the same time. It allows you to share the code
with the required user. So require developers, but on the other hand, it also allows you and helps you in restricting the code
the only specific developers and forming virtual
teams using orgs. Now. What is this Ox here? Also means organizations so you can create orgs that is organizations using node.js and npl to coordinate
package maintenance coding and sharing with the developers. So you can form
virtual teams using herbs. Also, it helps in managing and maintaining the various
versions of the code and their dependencies which means the
code dependencies. So it allows you to maintain
the different versions and if any virgins comes
it it gets updated and it allows you to update
that version also npm updates the application with the update
in the underlying codes. As I said that if any version
of any package has been updated or it has been changed
or new version as came into the market. So it allows you to update
your applications easily when the underlying
code is updated. There are multiple ways
to solve the same puzzle. We already know that forming the virtual
teams by using odds or managing the multiple
versions of the code and code dependencies. Allowing your code to share
with the other developers. All these are the basic
advantages or need for NPR. So in one particular point
we talked about n pi x so with the use of npx it
allows you to run packages without downloading them. Now. What is n PX all
about those of you who have upgraded npm
to its latest version npm after it five point two
point zero or the latest one. They have might notice that it also have installed
a new new binary alongside the usual NP n that is
n pi x so you can say that n PX is basically a tool which is intended to help
round out the experience of using the packages
from the npm registry. But only thing is that you can run your code
or you can run the packages without downloading then we
talked about the virtual teams. So let me tell you
what is an org. So as an organ donor
you can be a or owner. Owner or admin you can create
and remove teams you can add and remove members
from the teams and you can also configure
package access settings for the different teams. That is how you
can go ahead with npm. And if I talk about teams, what is the team group of
or members with a defined access to a set of packages which is governed by
the or so all these things you can Implement
using npn now will move towards the and npm packages. So what are the different
npm packages available? But before moving ahead,
I want to show you something that how you can go
ahead with node.js. So this is the site from where you have
to install the node.js which is node.js dot o– r– g– so there you will find
to version 1 is the LTS version and one is the current
version LTS stands for long-term support version
and the current version. You can install any one of them. There is no problem at all. This also says that node.js The JavaScript
runtime built on Chrome version a JavaScript engine. So before using npm
you have to download and install this node.js
in your environment and npm will be pre-built
or pre-installed with that node js version. So we were talking
about the node.js packages will start talking
about node.js packages now. So this is the npm site
which is npm chase.com. You see here you can build
amazing things from npm. You can search for packages. There are Hundreds and thousands
of packages available out there on this particular site, which you can see I
am just scrolling down JavaScript packages available many packages are available. Even if you search
for Lexi jQuery package or any other packages, it will allow you to search for those package install it
in your environment. So the website is in p.m. Chairs.com and to install
the node.js the website is node js dot o– r– g– let’s go back
to the PPT again and we’ll talk about some of
the Ready famous npm packages. So let’s get started. What are the different
npm packages available? So first one is npm Express. So if I go to
this particular website, which is npm chase.com and if I write here packages
and writing just packages and if I talk about Express if I press enter
for you will see that it allows me
to explore all the packages which I can explode. When it comes to npm
express will talk about what is expressed or chase. What is this npm
Express Express tour GS you can say it’s a node.js web
application server framework. So let me go to the side. So it says NP +
J s.com / package. / let’s check
this website Express. So you see here. This is the Express package, which is npm Express
which is expressed or J’s. What they do is you see here
that how you can use it. What is the
installation required? So you have to say
npm install Express and Express dot JS package
will get installed along with a Javascript file
expressed on Chase. So expressed what GS is a node.js web application
server framework basically, which is designed
for building single. Paige multiple page and you can
say hybrid web application now, it has become the standard
server framework for node.js. I will say
this Express framework or the package has become
the standard you can say server framework for no chains and expresses the back
end part of something which is known as mean stack em Ean mean stat mean you can say it’s a free and open source
JavaScript software stack for building Dynamic
websites and web. Asians now, what is mean is stands
for m stands for mongodb? It’s a standard nosql database
e stands for expressjs, which is the default
web application framework a stands for angularjs
and n stands for no change. So basically if I talk
about this Express framework or Express package
this Express package or expressjs Frameworks, it makes it very easy
to develop an application which can be used to handle
multiple type of request. For example get port
and post delete request. So that is all
about the npm express. It’s a very good package to use. The other one is
npm body parcel. Now, let’s see. What is npm body power surge. Let’s go back to this site. We wrote expressed earlier now, I will write body parts
of now, press enter. Now you see here. This body parts are node.js body
parsing middle beer. So to handle HTTP post
request in expressjs. Watching for and above you need
to install a middleware module, which is called a body parser. Again. It tells you that how you have to install is
the installation command is npm install body – Parson how you have
to use it by using an API. So you have to create a variable where body parts or and
required body posture package. So basically it is required to handle all
the HTTP post request. That’s why you need
a body parcel. As I said that body parser
it extracts the entire body. A portion of an
incoming request stream. So whatever is the incoming
requests to stream it extracts that and exposes it
on the request body and it’s a kind of a middleware for expressed or J’s. You have to install
it separately a year. It was a part
of the Express package only if you want to use body parcel, then you have to
install it separately. Now this module
or this package body passer. It passes the Json buffer
string and URL encoded data, which is Did using HTTP post request you
can install body parser as I said using the command npm
install body – parcel. So let’s talk about
the third package, which is not bound npm
Node 1 Node 1 is a utility that will monitor
for any changes in your Source. It will talk about any change if which has happened
in the source and it will automatically
restart your server. So let’s search
this particular package also, so I will go to the same side and instead of body parts
are now I will write non mon. This node Mon. It’s a tool that helps develop
node.js base application automatically restarting
the node application when there are
any changes in the code or any changes in the file, which is in the directory
if those are detected. So what it will do that it will automatically
restart the node application. So just use node ball. I will say instead of
note to run your code and now your process
will automatically restart when your code changes so
for the installation what you have to do Do you
have to run the command npm install – G note Mall – G is used for
the global installation. So you can write npm install –
gee not more. So let’s talk about
the other packages now. So we talked about npm Express npm body
parser npm node more now. We’ll talk about table code. Now. What is this table code? So let me tell you that the current browsers
probably don’t support all the new at my script
six features yet. You need to use a compiler. Apparently a transpiler to
transform your ecmascript 6 code to ecmascript 5 compatible code. Maybe there are
other options also, but payable has
become very famous. Now, it has become
The Benchmark or a guideline. You can say a de
facto standard to compile the ecmascript 6 application
to a version of ecmascript that can run
in the current browsers. That’s why we need this package and p.m. Table code
this Babel can also compile. The other versions of ecmascript
as well as the Riad jsx. So we use react jsx and react.js applications
to compile or transpile those ecmascript code. Also just people can be useful. Now the other package
npm low – no – makes JavaScript Easier
by taking the hassle out of working
with arrays numbers in object screen. You can create composite
functions manipulate objects and do some kind
of manipulations on arrays. So that is Npm. Low – very good package. I will say so let’s
check this package. Let’s go to the same site
and I will say now load Ash. So this package
has been deprecated. Now, this is again
a very important news that this package
is no longer supported and has been replicated. So let’s talk about the other
packages now now npm react this npm react package allows you to create
react applications. There are very good
react projects and applications available on GitHub. One of them is create react app, so you can use npm react
for those kind of application the other packages npm request. This package is designed
to be the simplest way possible to make HTTP call how you can make it very simple or very possible to make
HTTP calls by using this npm request package. It supports https. And it also follows redirects
by default the last package which we are going to talk
about is npm assessing. Now. What is this? Async async is
a utility module you can say which provides straightforward
powerful functions for working with a sin. Acronis JavaScript applications. I will say it is designed
originally for use with node.js and installable
Via npm install a sink. So the command to install these
packages npm install a sink but I will say it can also
be used directly in the browser. So these are some
of the very famous or generally usable npm packages
which we talked about. So, let’s move forward
now npm installation. So as I said first of all you have to do Note
the node.js from this website that is node.js DOT, o– r– g– first you have to download and install it you can
either install the 80s version or probably the current version. That’s not a problem at all. But before using npm
it’s very much required that you have to install node.js because npm indirectly comes with the
installation of node.js. It’s not a
separate installation. You have to install
node.js to use and go ahead with npm projects now the Json
file The most important file which is the package dot Json
file for any node based project for any node based development. If you want to do
the starting file or the first file, which comes into picture is
the package dot Json file. So package dot Json file
in node.js is the heart of the entire application. It is basically
the Manifest file which contains the metadata
of the project. So what all metadata it contains
you can say that package dot. Json is present
in the root directory. A of any node application or module and is used to define
the properties of a package. So inside the package of Jason, we will find almost
a metadata is specific to the project no matter it’s
a web application node.js module or even just a plain
JavaScript library because this metadata so as you can see on the screen
that it is showing me the name. So what is the name
of the application and projects? All these are the metadata name
version description scripts engine dependency step. Of dependencies home
page repository bucks license author engines all
these are the metadata. This metadata helps me
to identify the project and you can say it acts
as a baseline for users and contributors to get
information about the project so you can see here that a package.json file
is always a structured in a Json format which allows it to be
easily readable as metadata, and it is also
passed by machines. Italy so in this particular
side, you see here. It is showing me the name
that this Isadora car nodejs. So it is showing me the name. So what will be
the project name? So you can say that the npm init command is
there is a step-by-step tool to scaffold out our project. So this is the project name then it will show me
the initial version the version of the application which must follow
the semantic versioning rule. So it is showing me
what will be my projects initial version 10
the project description. So what is the purpose
of my application? What is the description
of my application or the technology used
like react mongodb and so on so I can write that in description main. So what will be my day starting
point of the application? So project entry point you
can say the project main file meaning the project mean Phi
of any node based project. It shows you that what will be
the starting point of the application in this case
script dot JS will be the main. Motor starting file then
the scripts so scripts which are required to be
included in the application to execute it properly. So it will come under
the script section engines. What are the versions
of the node and npm used? What are the engines you have used to execute
this particular project that will come
under engines author who is the primary author that can be specified
in this particular section. So we talked about name, which is the name
of the package. We talked about version version
of the package. The description author, then the license license to
which the application confirms are mentioned in
this key value pair. So these are the dependencies
the key value pairs. So third-party packages or modules which are installed
using an npm can be specified in the dependencies. So you can say it has
a list of dependencies npm automatically installs
all the dependencies which are mentioned here in the node underscore
module folder of your package. So what are
the dependencies are shown like a Press body power surge
a two-part Flash and so on. So all these
are the dependencies which are required
Dev dependencies. Dev dependencies means
the dependencies, which will be used only
in the development environment. Those are specified in the devil
dependency section repository. So information about the type
and URL of the repository where the code of the application lives
is mentioned here, so that will be a repository. So repository type and URL
type of the package. An be specified
in this section, but also URL and email. We are the bugs in the application should
be reported can be mentioned in this section now hands on so we will talk
about the package.json file. Now that how you can create
a package store Jason and I will show you that how you can deal
with a node-based project. So since node.js is installed as
of now in my system. So I will directly show you that you can start
a node-based command from node js command prompt. So this is the node.js Come on
from which is installed in my system. I will run this
as administrator. So you see here
that it is telling me that your environment has been
setup for using node.js and npm. Let me Zoom it just
I’m going to Fawn. So that it should be visible. So since node js
is already installed. So let’s check node – – V. Okay node –
be so node – be it showing me the version that which version of node.js
is installed in my system. It’s saying eight-point 12.0
which is installed in my system. So node version 8 is installed
my system you can see so we check the command. You have to write node –
V5 to take npm – we it’s going to show me that six point nine point
zero is currently installed in my system now how you can create
a package.json file. Unload this project. So for that there is a command
called npm in it – – yes. You see here that it has right to file which
is a package dot Json file. So let me create first
of all a folder directory. So I am saying mkdir can see
here mkdir new project node. So new folder will be created. So let’s go to that folder. new crotch note so we are
in new projects note. Now. This is the folder
which we have created. So now I will say
npm in it – – yes. Now, what is this command
npm in it – – yes. So npm in it is a command which is a step-by-step tool
to scaffold your project a node-based project. So for example, if I will not say yes,
I will only say npm in it. So it will ask you
some of the questions for which the default answer
I am giving us. Yes, so that’s why
I’m writing npm in it – – yes, because if you will note right? Yes, then it will prompt you for input for a few aspects
of the project. For example, what will be the project
name project initial version project description what will be a project
entry point and so on so that is why I am saying
npm in it – – yes. Now when I press enter so
it has route to a file which is called
the package dot Json where you can see that it is right
name version tsk. Option and so on. How can I open this project? I can open this
in Visual Studio code. So for that I will say code Dot and enter so let’s
open this folder in Visual Studio code
and here you will see that a file has been written which is package dot Json
which is having a metadata which is named
virgin description main what will be the scripts
which will be used what are the keywords
author and license? So all these details all
the Delta has been captured in a file called
package dot Json. Now what all you can do
with the package.json. Let’s see with installing
a third-party J’s file or third party package or Library called
moment dot J’s, which allows us to play around
with the date formats. I’ll go to this location
and then we can go ahead so I’ll go to the D drive. to a folder Okay, let’s open this
in Visual Studio code. Let’s close this so
package dot Json is there let’s open this file in this folder actually
in Visual Studio code and then we will see what we can do
with package dot Json. So you see here,
this is the package.json file which we wrote now
in this particular project. What I have done is
that you see here is script. I have a start script
node index dot j so I can ignore index dot JS. I have index dot JSP. Which is a blank file which I created and then write the code
for Human Torch are so moment or Jace is already installed in
node understood modules folder. So every node project will have
a node underscore modules folder in this particular example. I am playing
around with moment.js, which will show me
the current date and time. So I am writing console DOT
log moment dot format ddmm. Bye. Bye. Bye in this format. It will show me the today’s date
and time if I talk about package start chasing
you see the dependency here. The only dependency
which is shown is of movement and the Virgin is with
a caret sign a version is shown which is 2.2 4.0. So I’m using woman dot JS
in this particular example, and that has been specified in package dot Json and for
writing the code and writing it in index dot JS. So what I am writing is where movement equal
to require movement, so this moment is the package which has been referred
in node underscore module. So if I show you know that the score module there are
so many packages available. They’re in note
understood module. You see a lot many packages
are available by default. It gets installed when you
run the command npm install. So node understood module also gets added as a folder
in your solution. So if I show you
the solution you see here node underscore modules is there
and some other files package. So Jason is there
with some other files which I have created. So I just created
this index dot J’s and point hold it
now by bundling. I meant that you have
to bundle your note pace. Project by using backpack and once that is done so file
is created bundled or chairs, which I can run from anywhere. So let’s go here
and go to package dot Json. Also, I can show
you the two ways how you can run the code I go
to this demo to folder because I have a camera
to folder also here. So let’s run this command first node index dot J’s
and see what happens. You see here that it is giving me
the correct output, which is the today’s date
and time the current time which is happening 15 September 2019 and five thirty
nine forty nine p.m. So how I got this output. So you see here that we talked
about scripts, right? And Maine is the main file that is index dot J’s
in index dot JS. I have written that. I want to print the current date and time using the command
woman dot format ddmm by was very simple code. So In fact, it’s not Jason
in the script section. You see that start here. This is a kind of a script
which you can create. You can create your own
custom scripts also, but at the same time there
are some predefined scripts which are available
in package searches in if I show you see here install post-install post back
post publication Zone. All these are available as the
by default script commands. You can use our own just
like I used bundle in this case. So in start I said
that what will be my – I start statement
so node index dot J’s. So I went to the
node.js command prompt what I wrote note index dot J’s, and I pressed enter and it
is giving me the correct output. But when it comes
to the bundling, you can also run it
using curling of the file because after bundling using their pack the file
is created bundle dot JS so I can also write node. Bundled or chase it
is giving me the same output, right so you can see that while creating
the package dot Json. This will be the first file or you can say the basic file
Benchmark five to start with any node based project development
that you have to create how you can create that fight. I already showed you that the command to create a
package.json file is npm in it – – yes, so it will create
a package dot Json file for you. Okay. Once that package
file is created. Then you can create
many other files. You can install the node
underscore module folders. You can create other files
also just like in my case. I created index dot JSP. You can write the code
over here but everything which we have to specify
whatever dependencies or Dev dependencies in my case. You can see here that Dev dependency I have used
dependencies are there who is the author author
is literally pulled rain. So author has been Specified what are other scripts
which I am using. What is the main file? This is very important
that is starting file or the main file
of your node based project. You have to specify
in our cases index dot JSP. So whatever start command
you have specified that is node index dot JSP so that can be run
node index dot JS and it will give you the correct
output the desired output which is required. Today we have to learn
expressjs with note. So we’re going to see
what is expressed and how it integrates with node.js
and help us create our websites or apis with it. The agenda for today’s
session is Introduction to expressjs features of extra JS getting started with expressjs routing
methods and Hands-On. So expressjs is a web framework, which is built on top of node.js it helps us in creating
web applications or apis which can respond back from the server side
to the client side. They can integrate with different type
of database like mongodb Oracle or MySQL and helps
us in creating server-side applications. It’s a very
lightweight framework. So you don’t overload or on your system with unnecessary
dependencies in the code. So it’s very easy to integrate and it’s helps us
in creating websites or web applications which work with apis or a back-end for
our mobile applications. So some features about
Express Express is very useful because it helps us
in our development life cycle because by using Express we
can reduce the number of hours. We spend on your
application development time. The thing is Express
is built on top of note. So it provides apis or methods which can help you
in doing things which have to be done custom
in-ear node.js application or have to be done
repeatedly again, and again, so it writes
you a complete boilerplate for all the code set up
and helps you in development. Next point is it provides
a built-in templates, we look for templating
engines or framework for doing templating. But in this case
Express rights as Jade or ejs as a templating engine
for our estimate. Eat it helps in building
a single page multi-page or hybrid applications. So what’s the meaning of this? So when you need to create
a multi page application, so that’s application which navigate from one page
to another page and server has to create
those pages for us that’s called a multi page
application in the same case. We have single page applications which resists the first load
of the page on the server and then process everything
on the client side. If you want to create
a multi page or a single page application, you can use express
as a back-end and even if you You like to choose
kind of a hybrid you have a react front-end and some part of it is then
on the client side or some part is done
on the server side. You can use express
also in that case. So it’s very sensible. You can send it different things
and requirements. Next thing is it makes
integration with database easy. Another thing in expresses. It makes our life very easy when we are going
to use databases because it’s built
on top of node.js so we can use all
the node.js packages for connecting with
different types of databases and integrate in our application and they will
work perfectly fine, and they will not have
Integration problems with it. So it helps us in connecting your applications
with different AF databases, or you can choose a combination
of different databases and develop applications on it. The next point is it’s
very easy to configure express application because it’s been developed with the mindset of making your
developer experience very easy. So you don’t have to do a lot
of code for configuring express application just few
congregations in different files and your application will start
working the base application which you get from
the expression data is very Much configured so you don’t have to make
too much tweaks into it and create your application. It’s very convenient
to use express. Next part is it helps us in defining middleweights
for different tasks. If you work with Express,
they will be a lot of places where you going to configure
different middle layers, but waves are like operations which are done
between particular operations, so they can intercept operation
and to determine ablation or send different responses
or even do logging for us. So by using different
middlewares Express helps us in doing error handling. Manipulation of data
or logging the information this approach helps us in making
your life cycle very easy. We don’t have to struggle
in finding out problems or issues in our code
by going through all the line. You can just configure middle
beards at different points of data flow and they will help us
in a locking out information or finding errors. So, let’s see how we can start
with Express post your to create a node.js application. Then we will install
Express into it. So for create a node application
first have to create a folder so that I will open my terminal
the terminal I will say navigate to the desktop and mkdir
or make directory which is named as node. Js Express tutorial. Once the folder is created. I will just navigate
to this folder. Let’s see Dean ogs Express
tutorial in this folder. I will run npm in it and pivot. It will ask me some questions
about what the package name and all those information
or the package which can see I
will just press enter and go through them at the end. It will give me a package
or Json file, which is a We all fit so I will just say
yes, it’s fine. And it creates
the package.json file for me in the budget now have
to open this folder from my Wheel Studio code. You can choose any editor
of your choice. I’m using visual studio code any rate of will work
for this application. Now in this folder, you can see
I have a package.json file, which is the same which we configure
through the terminal window. Now what have to do is
we have to install Express. So we’ll go back to terminal
and run npm install Express. And dashed essay. I’ll go back to my project
folder and you will see that it created
node modules folder, which has all the dependencies for this package
for the application. So now I will create a server dot JS file
in This Server dot J’s file. We will write our Express JS
code will write const Express. So that’s for importing
Express into this project. If you don’t
write this statement, we won’t be able to use express. So let’s try to express and then we have
to create express application. So I’ll just read
const app is equal to express. I’ll start writing the first
route of my application. So we haven’t covered
the routing part of it, but I will show you the root URL
and then we’ll see how the routing mechanism
Works in Express. So first we will have
to write m dot get that means it’s the get drought
for a replication the /url and defined the callback for it. So the Callback
receives two parameters, so request and response so you can write
the logic of data which you want to send
from this request. The request will receive
all the information which is sent from the The client and response
will have all the information which you want to send
to the client side back because we don’t need to receive
anything from the client yet. So I will just say rest dot send and Define hello world
over his stream. So we have created a first route and now I have to
start this application. So starting this application. We can just write app dot Json and it will listen
to port number 80. So once the router is configured
we just go back to my terminal and And node.js command
for several JS file. So just first clear this screen. So we’ll run node server dot DS. So this has to be done
in your project folder. So once done I had got
any message back because I haven’t done any console DOT log or returned
anything from that file. So that’s fine. But now I will go to my Chrome browser
any browser of your choice. It’s not that you have to go
to Chrome will just say localhost:8080. It prints out hello world. So that’s the message
which you get. It from your server
onto your client because we are making
a request on the root URL. That’s the /url
and it’s a get call all the browser requests
which you can do from the address bar
are actually get calls. So speaking of get called
to the node.js server and print out information here. So if I change it to something
else so achieve the message and I will restart my server
for restarting server. You have to go
to the terminal window again and press control
C. It stops the server then you have to run
the same command again and it will restart. Over go back to your browser
and refresh it prints. Hello Edith Akana. So this is how we create
a simple application with extra GS and you
can use express generator which can be used for creating a complete boilerplate
for your application so you can try out
extra generator later on. So this creates our basic
application based on Express and we can use express
you need to create a complex boilerplate for us which will have different files
and different configurations, which we can
configure it around. So now We’re going to start
working with drought. So when we work with Express JS
the first key thing which XYZ as Priceless
as the routing mechanism, the routing mechanism
in node.js is not present. So we have to write
our own object to Define that if a request is coming
for particular URL how to navigate to that URL and
what’s the callback for that? So instead of writing
your own code. We use express for that and express works
on different methods, which are available
on the HTTP protocol. So these methods
can also be considered as current Like
get post put delete and they will be configured
in our application to do restful QR else also
or application URLs. So when we are working
with our application, we have to decide like which URL will do what so when you are creating
application anything which sends data to the client side
that’s a get call. So any kind of URL which is sending
data is configure the get method anything which receives data
from the client side and stores it on the database or processes that information
will be a post. Call anything which
updates the data like if you sending any data from
your client side to your server and that information is already on the server
and you want to update that you have to make
a put call for that so that the system knows that this call is specifically
for updating the content. The next one is delete. Now when you already
have some content in the system and you want
to delete that records, you can use the date call. I’m going to show you
like how this is actually integrated in Express. So we’re going to create
two requests one is get which you already made. The next one is post. So let’s see how a post request
is configured in the example. I created a URL
which is AB dot get now. What I’m going to do
is I’m going to create app dot host over
here and Define / and responds to its end. I just prefix it with host
over here so that we know that it’s supposed
call response now, I’ll go back to my terminal
and restart the server. So one thing I will do is I will
write a call back over here. So this lets us know
that the server is started. Otherwise, we’re not getting
any message on the screen that is started or not. And one more thing. I you can choose
any port over here. So I’m by default using 8080
but you can choose any port of your choice or
if any port is blocked. You can change this
to of your choice. So we’ll go back
to my terminal and say note. So that’s a server started. So my application
is started again. I will go back to my browser
and then the page again, so it prints the same output
which is hello Erica. Now, the next thing is
I have to use host Man Too. Send a post request because I cannot send
a post request from my browser until I do a forum Summit
or a Ajax call. So I’m not going to do Ajax call
or a form submit right now. I’m just going to use
Postman to test our apis or the URLs will just
go to host this URL and I’ve already have some URLs. I will just remove
that and write localhost 8080 and then this request
I got the response, but it gave me a it gave
me a JavaScript error that reference error. Race is not defined. So something wrong
with the code. Let’s fix it. So I have used race over here, but I haven’t configured
the arguments for that. So I’ll just copy these
arguments and put over here. That’s the same for post and
after restart the server. So whenever you make
some changes into the file, you have to restart your server
to get the latest updates. So I will go back
to my terminal. Unclear and loads over now
on the run the same URL again that prints post. Hello and Rica. So now I’ll post URL
has been configured. So in this way, you can send data back from your server
to your client Side based on different types
of requests you have if you have different URLs, I’m just configuring
the root URL over here. But if you have
different routes, like people or adults or those so you can just try
it AB dot get products or post or delete
or put a spanner. Eat and right / products
and just update this products. And people so make some changes and I’ll go back to my terminal
restart the server again and show you the content now, I’ll just write new
clothes products like this and the command so it says cannot find this you
religious post / products. Yeah, that’s correct because we don’t have
that URL configured. It’s only get so I have to switch back to get
and with request again. You can see it sends us
information about products. So this way you can use
different templating engines like handlebars or Jade or EGS to send
different pages over here. So this is how we configure
or routes for system. So as we need you can choose
whichever method you want to choose and configure
the route based on it. Every route can have
multiple methods on it, but you should not use
the same method again. And again, otherwise, it will just overwrite
the previous ones. So you cannot have to get
routes for the root URL or the product URL
because the last one What I did the other one so you
can only have one route name with the route method. So the next thing is I will show
you what is expression data. So explosion later
is npm package, which we can
download our system. So it has to be installed
globally on our system. So you have to write in – G or to make it
a global package. I will go back
to my terminal window and stop the server and run the
command Express install extra G. So it gave me a deck it’s not able to install
Express generator – jion a system. The reason is because when you’re
installing Global packages on a Mac machine it based
on your node.js installation. It might require
your Superuser access on a Mac machine or on
a Linux machine. So you might face this error. So I’m facing this error. So what have to do is
I have to clean up. It’s running npm install
Express generator – gee after I write sudo npm install Express generator – G so this will ask
for the user password, which is currently logged in. Once you add the password says
excavator has been installed successfully on system. Now we can use
express senator now for using expression data burst, if to create
a new project folder, so I’ll just move outside of this project folder and
create a new folder over here. So I’ll just write mkdir
or make directory and create a new folder. So I’ll just write
node Express chin. So now we’ll go to this folder mode Express gen
and run express our so once you install exploit
generator globally on a system. You can run the express command. Once you run this command
it gives you this output. This says that you
have used X-ray generator and it created all
the files for us. So this is the boilerplate files which are paid by
the expression editor and you can use them to run or create a node.js server
and express application. So I’ll just run npm install to install all
the dependencies first. So all the
dependencies are done. Now. I have to run npm start over here with Starts
Here express application. So let’s wait for it to start. I think it started on our system and Guru browser
and localhost 3000 so you can see it installed
all the packages and everything which are required by Express
and create a spoil of lie in which we
can tweak all the files. So I’ll just go
to my visual studio and open this project folder
to show you what’s in this seat. So the Stock node Express gen. So here you will have
a app dot J’s file, which is for configuring
your application similar way, which we read
our server or chairs. And then we have Route 5. So for every route
or every method which you want to use
in your routing we have these files for that
by default price index and user so index of the root URL
and users is / users and you can use these
to configure your routes and if you need more outs, you can create
multiple of these. These and start importing them
in year after J’s file like this and they will start integrating
to your application. So you don’t need to decide that you need 50 routes and
what will the methods of them and anything you can configure
your routes based on your needs and how your development grows so it’s very flexible and you
don’t need to Define everything on the first load
you can develop and make the system grow later
on in building rest crud API with no chairs. So what’s rest API
rest stands for representational State transfer? So that means rest is
a architectural design which allows you to Define
approach for communicating between different parties. Like if you want to communicate
between a server to server or client or server or client to server to client rest allows you to create
a standardized approach where you can Define how the communication between
different parties will happen. This is also regarded as
the language of Internet. So most of the newer Frameworks like like react
angular view JS rely on rest apis to update the data transfer even for
Android and iOS applications. So anyway video
developing application which connects to a server
or client connects to a server that will probably
using a rest API so we can say it’s
a language of internet where most of the things
on internet actually rely on rest apis provided by
third party or the epa’s which they are
themselves creating. So one good thing about race days rest
is a stateless model where the client Only
Stores the information who is Actually
accessing the information and the server does not have
any information about it. So that means servers
do not create any sessions for a particular user. They just send information back to the client side whenever
anyone request for it. So that means it’s much more
easy to load balance it much more easy to maintain rest apis
other than the traditional way of handling features of
rest rest is simpler than soap because it relies on a much
more simpler way of defining how the data transfer will work. How the URL scheme has will work
rest supports XML and Json. So if you already have some apis
which are designed on XML or providing data based on XML. They can be migrated
to a restful architecture or if you designing new apis and you want something
to be created in Json or a mix of Jason plus XML
that can also be done. So rest is completely
independent of how you want to design a data transfer. You can choose XML or Json for it restful apis are
very easy document Solutions like Swagger helps you
in getting documentation for a very simple application or even for Enterprise level
application the help you in creating different types
of documentation for your needs. So it’s much more easy
to work with restful API so that you can
easily document them. You can share the documentation
across your different teams and they can easily collaborate
with you handling errors in restful apis
is also very easy because restful API is works on HTTP methods and
if you have any kind of error or if you have any kind of a problem with the server
We’re going on you can just send error code from your server
based on the HTTP methods or STD code which have like if you have
an internal server error, you can just send HTTP code
of 500 and client-side will know that there is
an internal server error, which actually happened
on the system. It’s very easy to handle out rest errors with rest apis
principles of rest API. Rest apis are stateless. They are used for communicating
between client side and server side. They have a uniform interface. They can be cashed
at either on the client side or the server side. They follow a layered system
and you can coat them on demand. Okay, so we’ll cover
the first part which is ticklish. So like I said rest
API is a stateless. So when you are creating
a restful API, the practice is we
should not store any session information
onto the server any time when you are communicating
between a client side and server side. You only sent tokens for authentication like a GW
to confer authenticating that this is a particular user
is equal it or not. When you’re making
a request from the client to the server the server
only validates a token. It doesn’t cares
about who’s the user because if the token is valid and if some information
is requested by the user the server
will respond back to it. There are different ways
of implementing JW tokens in it. And which allows you to do
full complete authentication in restful services. The benefit of this is if you
have a load balanced system, you don’t need to care about
which server has to respond back to what client because if the session
is not binded to a particular server
on a load balanced machine or environment then
any server which is running your application can respond
back to the client side. So it’s much more easier
to load balance restful apis. Next point is client
and server communication. So rest API is help
you in communicating from the client side
to your server side or server side to a client side. Also the new application which are getting created like
Android based applications iOS or web applications based
on react angular vue.js. They all use rest
apis most of the data which you are going to use
is coming through rest apis and there are third-party
Solutions like third-party apis which are provided by companies
like for weather apis or for doing currency conversion or doing market price and we’ll
say so these kind of things so those all provide you
rest API so rest apis are kind of the future
for Addition between client-side and server-side third
Point uniform interface. So when we’re making
a restful API, we generally follow
a standard way of defining how the URLs are going to work how the scheme is
going to be defined for a particular use case. So most of the rest apis
will look similar. So if you have
resources URLs will be based on resources for you. So rest apis are designed to handle different
resources resources are like data in your
application like example, if you are making some data of of A group of people
you can just say / people is my source. So that kind of a URL
schema will be used in different applications
and most of the applications which actually depend
on this kind of a data set or resource will follow
the same kind of a URL schema. It’s very easy
for developers to know what this resource
is actually working on because they might have worked
on the same kind of a URL schema in different applications. So it’s very convenient for developers to
work with staps. Next point is cashable. So rest API is
are also Cashable. So like that’s a very
good benefit of them because the rest API is don’t work on sharing
information on they don’t rely on any cookies
to be doing any authentication so you can actually cache data which are coming from rest apis
and rest API is actually have very simple data
structures like Json and XML XML can be little heavy. But Jason is very lightweight. So if you are doing
any kind of a computation and getting some results based
on some database queries or some machine learning logic, you can actually catch
that logic on your A server or even on your client side so that you don’t need
to make that same request for the same resource again and again can just get
the information once and reuse the information again, and again in your application. The fifth point
is layered system. So when we are working with
rest apis rest apis are designed on a layered approach like if you have multiple databases
or multiple places from where your data
is going to be received. What you can do is you
can design architecture where one resource is coming from one different
location and one. Source is coming from
a different database or even if you already have
a legacy system like a soap API and now
you want to migrate here. So P Pi into a restful API
that can also be done so you can create a wrapper
of a rest API on a legacy code which can actually
help you in migrating from a So based architecture
to a rest space architecture. So restful apis can be used as
a layer architecture on any kind of a legacy system or they can be used
on any kind of a new system. Also. The last point is kodanda. And that means when we
are making a restful API, it doesn’t requires
create everything on the first day of development. You can create
as your requirements increase. So if you are working
with a particular part on the UI or in the client side and that requires only
a particular set of resources, you can first coat
those resources start your application delivery
application to the customers. Then once newer things
are coming in. You can just create
the resources on the go to a kind of Agile development
for your apis. So it’s very easy. It helps us as a developer
a lot to create apis based on demands. Otherwise as a developer, you cannot create
everything in one go and deploy it to the customer. So that’s going
to take a lot of time and energy instead of that. You can just create few apis
delivered to the customer. You see if the EPS
are working fine how the interactions are with the customer and then
scale your epa’s based on it. So one of the most
important thing in rest apis as methods of rest API, so when we’re working rest apis, we actually rely
on HTTP methods which are get
post put and delete. So these are HTTP methods
which are being from the days when HTTP protocol was defined. So when you are requesting
any data from or when you actually posting data or you want to delete
some records from the database, we actually rely
on these methods. So what happens is in a restful
architecture we defined at a particular resource is
available on a particular URL. So in this simple example we
have Poor people and we’ll say / people will represent that group
of people in our database. So now if I want to get the data of those people I
will just say / people and make a get request
to the server. So when you making
an HTTP request to the server will Define that this HTTP request is based on the get method and I
want to get the information on a particular URL which is / people and everything from that URL
will be written back. So internally the server
will Have logic to go to the database and
get the information the end user customer
can see the information. So in this case, it can be react or angular
or Android or iOS application which gets the data prints the list of all the people now
you want to add new people. So for that we’re not going
to create a different URL. What we will use is we are going
to use the same resource but instead of using
the get method from HTTP, we’re going to use
the post method. So when we send a request
again on the same URL, which is the same resource
which has / People with the method post
we can send a payload that defines what we want
to store in the database and based on our Logic
on our server. The system will
receive the information and process the information
and store it based on how we configure the code for it. So instead of creating different
URLs for different type of data, which we want to request
or push back to the server. We just use the HTTP
methods for it. So similar to that we use put
for updating the data. So if you already
have some resources and you want to update
them you can Just write that make HTTP requests
to the / people URL and Define the method as put so the
system will identify that it’s a put request it
requires to update some data. We have to write some data
into the database back. So we’ll Define
how that procedure will work and the system will receive
that call and handle that for you. Okay same for delete. So in this case delete
will just delete the record. So if you say / people it
would lead everything we / people / some particular
name like Parveen, so to delete the data
for the Praveen in the system. So when you’re working
with rest apis, this method part is going
to be used a lot. So it creates some kind
of a confusion like how the data is getting transferred
from which one resource to how so always remember get
is forgetting the data post is for pushing back to the data
to the server side so that the server can store
the information put as if you already have some
information you want updated so you use the put method
on that resource and write all the update query
in your server side and delete is
for deleting the data for that particular resource. So now we will see how this API is going
to be created using node.js so we will develop a restful API in node.js so for that first
we need to open editor. So now I’m going to create
a express application in this code. Okay, so expresses
a web framework, which can be used along
with node.js it’s based on node.js actually Express
has a dependency on nodes. Yes, it will not
work without it. But this by framework allows
you to create restful apis. It gives you a structure of
how to create restful apis and also gives you
helper methods middle layers and plugins to how configure
your node.js application and pdps through it. So the first thing is
I’m going to create a new file. Okay, so I have this project. This is the budget
which we created in the last session and add
overexpress code on top of it. So for that first thing what we have to do is we have
to install Express for that in my command prompt
or in my terminal. I will write npm install. – – save for saving that in our package
Edition Express So this installs a node module
which is expressed. We have a dependency on it so that it allows us
to configure application. So once done we create a new
file called application dot JS you can name it as
application server index or JS. It’s up to you
what you prefer in this file. I’m going to import. Express so I’m going
to write const Express. Equal to require Express. So once Express is important
to known to our code. We need to create
express application. So that’s very simple will write
const app equal to express and will execute this function. So this creates
our express application on the app variable
now this app variable. I’m going to Define
our API URLs. So like I said
in restful apis, everything is based. on resources, so every resource has a URL
so a very simple example, if you have people in your database a people table
will point to a resource URL which will be /
people a person in that people database like a row
will look like something like this a people / Ro so if you want
to point to a person in the people database the URL
will look like people / person The person will be
the name of the person that you want to look for. Okay, and something else like if you want to find out
what’s the person’s job or age that kind of ambition. Will Point like in a database that will be inside
of a person inside of a row for a particular person
inside the H. So, we’ll Define the resource
URL like people / person. The person will be the name
of the person and what you want you want the age of the person
so we’ll have the URL like this. So we’ll just create
a Sleepy Eye to match this Euro schema. So the first thing is
we’re going to write app dot get so get is the method which is used
for getting the data. Like I said when you need to get
data for a particular resource, we use the HTTP get method. So in this case app dot get
means we want a Handler to handle all the API request for a particular URL
on the get method. So we defined the method
as get now. We want to define the URL. For it, so we’ll write / people. Okay. So if any request
comes for / people which has HTTP method as get this Handler
will be called. Okay. So now we have to
define the Handler. So we’ll write function. This will be the callback
for this evil and over here. We have to write
request and response. So they are two arguments which we receive
first is request. Second is response request
has information about what the Mission has been sent from the client side
to the server side and response or risk means what information
you want to send as a response from your server to the client side so over here
I will just write dress dot send and now I can just
write any information which I want to send. So in this case, I will just write
a string hello world. Okay, and save this file
and on the scope. So now the URL is configured but
we haven’t started the server. So when you are writing
a node.js application, we don’t need to rely
on a patch a or is f or nginx for running a server because node.js provides its own
development server or Observer which we can use along with that because it can actually listen
to all the network request on a system. So node.js will help
you create your own server. Okay, so we’ll just create out. Over here. Okay. So now we will start our server
so for starting the server just right app Dot listen and you define which port number we want to listen
to so by default. It starts on the Local Host, which is the local machine
and you have to define a particular port number. So in my case, I will just write 3000
which is used for most of the node.js application. So I’ll just write 3000
over here and let’s see now. I want to run this application. So this node.js application has to be executed
by using notes Here. Yes, so I will go back
to my terminal and write node application dot JS
once done you will see that it’s not going to find
anything after that command because it just
started the application and it doesn’t give you
any consoles or return messages that the application is started, but right now it started so what we can do is we can
just go to our browser and see this application go
to localhost 3000. And you can see it
prints cannot get / because that’s the default URL
which goes on the TV you are in but right now we actually want
to point to people over here. So once around
localhost colon 3000 / people it prints hello world. That’s the message which I sent from my node.js
application over here. So now we created an API
over here a simple API, which returns hello world when we point to the people you
are so now we can write any kind of database logic like if you want to have a mom. Why would he be connection
or SQL connection over here? You can write everything away. So you can use any kind
of a database or any other soap API
to get the data or even other restful
apis to get the data and send it through this API. So I would say you
can use mongodb over here to connect to a database. So in this example, I’m going to
connect with mongodb, but I’m going to show you how a simple Json
data will be sent so you can just try
it rest dot Json. And Define any Json
data over here. So this will be a JavaScript
object in the JavaScript object. Whatever you want
to send you can Define so I will just write
people is equal to a and in that array every person’s
name will be added over like you have different objects
for every person. So I will just write Ravine and at the end just right
response dot end like this. Okay. So now what I have to do is have to restart the node.js
application because Cuz when you started application, it will not listen
to the file changes. So it will not
restart the application. Once you save any new file, what you have to do is you have
to stop your application and run the application again. Go back to the same URL refresh and you’ll see it prints
out a Json data. This means that now
we have a Json API based on rest architecture and returns
information about people. So this is very simple like we implemented
a get a pi now. We want to make it
little more complex. Now. We want to see how a ghost API
or a delete API will work. So that’s really simple
to write the business logic will be little different
but for defining how the API is created, so that’s very simple. So we just write just
copy the same code again and instead of get
I will write post and the URL will remain the same because that’s our resource URL so we will not change that URL. Okay. So now instead of Keeping
this Json data over here in the Callback. I will keep it over here in the out of the Callback
function in the file. So right people is equal
to Jason people and I will send people along with the API requests
in the response. Okay? So because now people is equal
to object you can pass people into rest dot Json so that it converts
the object into a valid Json and sent it back along the API
now we’re going to try how the post request works. So what I’m going to do
is is I’m going to use this people object and add
some people to it. So the people will have person and the person has
to have a name. Okay in this part, I will just go to
send some information from our client side
to the server and add the data. So one tricky thing is
we cannot make post calls from our client side
or our browser directly. The reason is because
when you type in any URL in the address bar that automatically
configure with its for a get request and for
making a post call from the client side you
actually Need to submit a form or make a xmlhttprequest or Ajax request
from the client side. So instead of doing that I’m going to use
Postman to test my API and develop it. So just open Postman. So Postman is a chrome plug-in, which can be used for sending
request to your server. So in this case, I’m just going to
clean up everything. Okay, I mean Postman now, I want to make a request
to the server. So the postman will be
a client and server is at local has 3000 / people. That’s the first resource
which is handling. So I want to make a get request. I will pick get
from the drop-down and click on send. Okay, so it sends a request and
Prince this information back. So that’s a valid Json
and now we can see that the request returned people
and the people is And a which has persons in it. It can be any number
of people over here. Now. I’m going to make a post request
to this URL in the drop-down. I will just pick post
and send it from here. So now I’ve got an error
to say is cannot boast people because they API is
not configured for that right. Now. The reason is I have added
the code for it, but I haven’t restarted
the notes are so soft to restart the node.js server
by running note application. So I’ll just go to the terminal and Reese
a node application dot Js. Restart the server again. So now it will listen
for the Post request also, so do the same request again. You can see it
returns people again. So there’s no change
in any data right now. Like it’s the same data set
which is coming back. So let’s make
some changes to it. I want to send some data
in my API request. Which is going to be
information about the person which I want to add
into the database. So we’ll write name. So this is something
which you do in Postman and they are different ways
of doing it in Postman. Also, this is one of the simplest way so I’ll just
write a method over here. Okay, so I’m just configuring that I want to send
a body in that body. There will be a record
which is of type Json which has a key name
and a value which is some it. So I want to send
this to the server. So this requires go to the server and in
the server now, I have to handle this request. So for getting that information, I will go to the Part
of my Epi so over here. I’ve just tried console
DOT log request and see what request actually paints. So restart the server and send the request again once
done will see there is a lot of data in your terminal over
here like a lot of information coming for the API. Okay, we have to look for a key
which is body in this data. Okay, little hard to find. Okay, let’s do one thing
instead of request. I will write request dot body. And how do the server again
just to simplify things? And make the API request again. All right, so it shows as undefined so it’s
not able to send the body. All right. So no problem. We’ll just send it as
a form will write name and a meth over here
send the request. Okay, so I think the reason is we haven’t configured
the middle weights for it. So we’ll just try it
body parts are as a middleware. So body parts are
is a middleware which allows us to Parts
the body which is sent from the request and attaches
with the request object. So let’s configure
it in our code. So it really simple so now install the body
parts are middleware. It’s right by Express only so I will just import
body parts require. And it never app you have
to say app has to use. Body parts are
well-defined over here. That’s it. So, let’s see
how this changes our code. an application game go
to the same URL and Okay, I think they have
changed the rotation. Okay, so I think we haven’t configured
the body parts of correctly. There might be some changes. So I think what you have to do is have
some different codes embedded. So what you have to do is
from the body parts are now they have added different type
of packages has like body parts. Are you are encoded or which we have
to use individually and write those who were here? Okay, so these are the packages which we ought to use
from the body parts of a method will just right
after use body parcel, you are encoded and after
use body parts of Jason, so this allows you
to handle Jason or form sent from the client side once done. I will restart
the application again. Right typo in the bodyguards
the name so just renamed it again on the server
again again a typo, okay. The server started again now, we will send a request
through Postman and see what that results sent
a request from Postman and in our system. We received a blank object. That means we are able
to parse the body, but it doesn’t have any
information over here right now. The reason is let’s
see have to send it as xw form URL encoded
so bright name. And value so you
start em a saint. You can see I’ve
received a value which is named as over here. So that’s our data which is being sent
from a client-side to a server. Okay, so now I can just
receive this information so I can just write request dot body dot name
and get the information. So if I run the server again
like restart the server again. Send the same request
again in the console DOT log, you can see only the amount
value is getting printed. So now simply what I will do is I will write
a simple condition we see is if we have requested body
and if that request or body has a value
of name sake of name. then enable people data
the variable which we have we have another array
which is called people. We want to push
a new record to it, which will have name equal
to request dot body dot name. Okay, very simple. So we just headed
a validation checked like if the request has a body and body has a name then only go to the people variable in
that people variable. There is an object
which is array, which is named as people again. That array will just
push a new value. Okay. So what you’re pushing is, we’re pushing a new object
which has a key called name and the name is equal
to request dot body dot name. Okay, that’s the
value once done. It will move to the next line and respond back
with the data of people. So let’s run this over again. And I’m sending name as a myth. So let’s in this request
and you can see now the data has changed instead of
sending Parveen the data set. It sends come with also
so I can just write John. And it adds on over here. So whatever the data you will
descend from the ski just adds into the variable wind
and the server side. So this data is coming from the server side fuse make
this EPA called multiple times. It will return you the latest data set after adding
the result which is in if you don’t send anything like I’m sending
a blank request. I’m not sending the form just uncheck this name over here
and send the request. So it Returns the same data set which is actually
running on the system. So it’s not storing anything. Into a database it just
has the information inside the memory. So it’s just a JavaScript
variable on the node.js server, which is in memory only and once the server gets stopped
and he starts again this in memory data
will also get removed once I start this
over again you can see And make the same call
will see Parveen only because Parveen was the data which is actually hard
coded into the code. Otherwise, if you
store any new data, that will be only in memory
and will be removed once the server has restarted. So this just shows you how you can get information
from the client side and store inside database like instead of writing
something in the memory. You can write our logic
to get up Mongo DB connection or a MySQL connection or SQL connection
any kind of a database which you want to use so
there are lots of of databases like Firebase AWS has their own
standard ones like mongodb and MySQL any of them
can be used to store the data. So it’s up to you how you want to design
the applications data right now. I’m just using in-memory
data over here only. Now, the next thing
is making a put call or a delete call. So those are very
simple also again, like I just want to copy the
code just copy the code of post and instead of writing posts. All right, put the resource
you are looking again people and we Whatever you want to do if this request comes in so if you want to remove
or update the data, you can write a check to find out if the particular
name is actually in the database and update
that or just give an error that this person
is not available and we cannot change the data. So it’s up to you how you want to define
the logic for that same way. You can Define the delete call
Just Right app dot delete and Define the late call
in which you can delete the information for
the particular user. Okay. The next part is pointing
to a specific resource. Source in a data set. So right now this data set is for people and I want
to get data for a person like I wanted it
gets rid of a Parveen. So, how can I do
that in that case? What you can do is you
can Define that a new URL like app dot cat in which the resource
you will people / person. So the person is right now
hard-coding person over here, but that means you want
to send information for a particular
person from here. Okay, so you can just
send name is it? To person from here save
this code run the server again. So I will just go
to postman and run this code. So let’s say people /
person now this code and you will get got an error because I’m sending a post call but post-call is not configured
subtle change from the drop-down and I want to use
get call over here. So now you can see it’s sending
information about name and the person Right. Now it’s hard coded like it’s not doing
any competition on the back end. But what you can do
is this person over here the name this is something which is going to be different
for every request like you might request
for a math. You might acquire supervene
you might request for x y z. So this part is going to be
a parameter for our API. Okay, so to define a parameter, we would write column
and name this defines that this part of the URL
is actually a parameter which can be used
in our JavaScript. Ode to do a different query
or do a different kind of business logic, so in this case, I’m just write request
Dot baram’s Dot name? Okay. So I’m just returning
a JavaScript object name Quest dot patterns dot name now. I will restart the server and go back to the postman
and run the same code again. So it prints person again same
data which we already signed. The now the new thing
is in the stratum it. Send the request again
and print some it now because it picks
up the parameter, which I’m sending in the URL. So this way you can write a URLs
based on different parameters. You don’t have to create
millions of different URLs for different millions of users. You have you can just Define that this part of the URL is a parameter
and that will be received in your code and you can do
your business logic based on it. So you can just write
Parveen over here. Principally, so this parameters
are not limited to one you can send any number
of parameters in the URL and it’s up to you how you want to define
those parameters. They can be named as anything
like in this case. I’m just naming them as
name you can name this as person you name this as
user whatever you like. So depending on
your business needs or how your team is comfortable
with the names. You can pick up
any parameter name and using your code. So it’s up to you. And if you have
multiple parameters, you can just write /n / h /
something the only thing is when you are defining
multiple parameters, you have to follow the same kind
of URL to match it. If you only write name / age
and you only call for name. This URL will not be called. So I will show you a demo the your latency is
people /name /features, and I’m just making a call
to people / Praveen so you can see it’s
not giving any data because it says this your
is not handled over here, right? Now Postman the reason is it
requires a parameter called age. So if I write 11 over here, it prints out that Theta
because now the actual URL is getting past so you’re always
have to keep in mind that what you are
creating in the URL. And will you be providing
all the information or you’re just going
to provide some set of the information in your code? In this case? If you want to handle
those different scenarios, like if you want to send requests to people /
name and people / age, you can just write. / / name as the first one
like Define it first and if this doesn’t match as
then moves to the next line and finds out the second option, which is available for matching. All this URL schema is mashed
from top to bottom. So anything on the top which matches the URL
will get the Handler if the scheme is not matching. It passes the control
to the next definition which you have right now. And then if that is a matches
moves to the next line and if nothing matches then
it just gives you A photo for that it’s not able
to handle this request. So this way you
can Define parameters. Also in-ear API requests today. We’re going to learn
node.js and MySQL. So to see how we can integrate
a mySQL database with node.js. So the agenda for
this today’s session is what is my sequin features
of MySQL MySQL installation and building a credibly i
with node.js in MySQL. So what’s my sequel? My SQL is a relational
database management system, which allows us
to manage a database and create records based
on tables and rows. So it’s an open source system which works with
different platforms. So you can use it
with Mac windows or Linux and different
other server platforms on which it can be installed and it provides a lot
of different storage engines which give different uses and
it’s been backed up by Oracle. So it’s open source, but it’s been backed
up by Oracle so you can use it
without any commercial license and if you want
a premium services or support from Oracle you can still buy the premium
support license for that and in which Oracle
will provide you support for any kind of issue
or a security patch or anything. So what are the advantages
of MySQL with notes? Yes. So first is
scalability and flexibility. So when we work with node.js
node.js is very scalable. So it can be installed
on a very low-end machine like Corner Raspberry Pi or it can be installed on
a large scale server same way. You can use MySQL to the embedded server
on a small device like a Raspberry Pi or on
a new server like on a aw server or even a It’s over which you can deploy
on your bit of arrows. So it’s very scalable. You can choose different
variations of hardware for MySQL and it will run on any of them. Next thing is low
cost of ownership as MySQL is open source. So there is no startup
cost for licensing or starting with mySQL. You can just download
the my SQL package and start developing
application on it. And if you want support you
can go with the Pete lessons this from Oracle. Next one is
skewed data protection. So my SQL has been getting
it more than 10 years. So it has a good
security implementation and it has powerful
mechanisms to handle out different user accesses
and ownerships on the database. So it’s very secure and it’s been following the
industry standards for security. Next point is
ease of management. It is very easy to download and start here first
development with mySQL. So it’s very easy
to maintain also so it provides two different
tools like MySQL workbench which can be used
for maintaining a database or going through
the database records and everything and you
can have events or should you Automatically configured
in your database to do cleanups or two different operation
in your MySQL server. So it’s very easy to maintain
or MySQL server when you’re developing next
night is high performance. So when you’re
working with mySQL, it provides you different ways
of cash in your a tiny memory and doing indexing so you can have multiple
indexes in your system or single index in a system and also do caching of Records in your memory
only instead of going through the records
again can cash them inside your memory only. So in that way it provides
a very good performance when you With mySQL it
can be used for applications which have a high
demand on performance. So my SQL can be also
used as a server which is highly available or can be also used
as a cluster database. So in which you can create
master and slave servers and deploy any cloud or in
your data warehouse. So in that way you can
create different applications of the same database
and keep a copy of them so that if the demand is high or if they’re in is E-Town time
of any server or any machine, you can easily switch
to another slave or you can use Cluster
for managing those things. So it’s very scalable in those parameters
of high availability. So we’ll move to the next topic
my SQL installation. So we’ll see how we
can install MySQL system. So I’m using a Mac machine
right now for the session, but you can install MySQL
on the next Windows or Mac as per your needs and most of the steps
are similar for installing MySQL machine up to go
to the MySQL official website, which is deaf dot MySQL.com. So just go to that side
/downloads / installer. So we hear what I
have to do is we have to first Download
the my SQL Server, so I’ll just go to my SQL server and I’ll scroll down and I
will find my SQL Server here. So it gives you
different options for different operating systems. These are the operating system. It supports so you can choose any of them and download
the package for this. So right now I’m using
a Mac machine. So I will go to the Mac OS and it shows you
the different options which are available. So I’ll just download
the first one it will take you to the next page where it asked you
for login or sign up. So it’s optional you
don’t need to login or signup download
the package you can. Can just say no. Thanks and start my download Once clicking on this
it was started download. So I already have
this download on my system. I’ll go to my downloads folder so you can see in my downloads
folder I have this package, which is MySQL it
Point 0.16 Mac OS X. So just start this package. So this is a DMG which will start
the installation process. So this is the package for installation from start
the installation of so, what’s the package
starts installing? It will be a wizard where you can configure
the MySQL server. So it’s giving a prom date it will Mind what software’s and
required for the installation. So just continue continue
the lessons agree. So if you want to change
the installation location, you can over here
otherwise just install so I’ll just say
start the installation. So the installation
is done just move this to trash. So now I have the my SQL Server
installed on my system. If you want to check
out is installed in just go to my system
preferences over here. I will find a tab
for MySQL is click on it and gives you information like which instance
is active and What is in cesare on my sister? So this is Max specific
screen or Windows. You will have the same
kind of a screen where you get information
about the MySQL set up on your system from here. You can start the MySQL server
or stop the my SQL server and you can also configure
different directories for my SQL like the error directory or the data directory
for my SQL Server. So the next thing is which you want to download is
the MySQL workbench. So workbench is a tool
in which you can connect to your database and get
information from the database in the do operations on it. So in this case,
I’ll just go scroll down. It gives me the option
for Mac OS X so I will just download this package. So again, it will ask me
for login or signup. I’ll just scroll down
and just say no. Thanks just start my download. So my SQL work bench
is download system. So it doesn’t double click it and start the installation
process again for Windows and Mac
the steps a little different but the utilities
are same I’ll just install this on my system. So already have
my SQL work bench. Vince install my system. So if I install this package it
will give an error right now. So we’ll just stop
the MySQL workbench and replace it again. So once my SQL work bench
is installed you can connect to our database and
see God’s on it. I will open my SQL work bench where so as this
is MySQL workbench and you can configure
your connections over here. So by default it gives option for the localhost
or the local package. So just connect to that
and it lasts me root password. So I’ll just And the password
over here and press enter. So now it’s connected
to my database and also the MySQL server and I can go
through the different database which I have over here
or different schemas queries and run queries over here
and all those information. So we’ll be using this
for wicking data into our database
and checking out how the record have been added into the muscular
called database now, we need to start with
the node.js application part where you’re going
to create a connection between the node.js application
and my seatbelt. So for that first, we have to create
a node.js application. So I’ll open my terminal. So now in our terminal
I’m going to start with a node.js application. So create mkdir directory node
my SQL example now we get to this folder now, I will set up the node.js
application which I want. So first when you create a node.js application you
have to do and get a minute. So I’ll just run npm in it. This will help me initialize
a node.js application. See is that you Name this
application as MySQL example. I just say yes. So if you’re fine with it,
you can just press enter. Otherwise, you can rename it
by typing the new name of here. So if you want something
you can name it. I’m just going to go
with the default name right now. The default name is based
on the folder name which you have. So I’m just going to continue with the folder name 34 name
the version of it description. If you like to add
any the entry point of the starting file test command
git repo keywords and author. It gives you a preview
about the packages. Listen file so it’s okay for us. I’ll just say yes now
it’s going to be created. So I’ll just clear
the screen and two Ls. So you can see the package this
in file is array into my holder. The next step is open
this node application in Visual Studio code. So I’m going to use Visual
Studio code for development, but you are free
to use any editor which Eli so the folder is loaded and it has the same
package.json file, which we just created now, we have to install all
the packages which we need. Just go back to our terminal
and it’s All the packages. So we need to stall
and payment stall packages which are dependencies
for this project. So we’ll install
Express Express handlebars. My sequel so these packages
the three of them are dependencies for this project. We also need
body parser to parse. The body information
will just install that too. We also in node mon, which we are going to install
as a dependency have a type over here or just fix this. It’s handlebars only. So the required packages
are installed now, I have to install
the dev dependencies. So I will install npm
install instead of saying – – say we are going to say it – – save – tape so that means it’s a dependency
now we install node mom. So no more is installed on my system now can start
developing your application. So we have
the package.json file. So now we will create
our server file in your project folder to Define
our starting files for node.js so that I will just
create a server or JS file and this we have
dependency on MySQL. Require my SQL
like this and express. constant body parser
require body car, sir So now we’ll create a express application by saying
app is equal to express. And then the Define AB dot use
for body parcel. So this configures
the node.js application now, I’ll just add the end. I will start this application by saying AB Dot listen
the port number. So we’ll just listen
to Port 3000. Now, you have to configure
how the database works. This is the node
express application. Now we have to define
the configuration for the mySQL database. So just write my SQL connection
with equal to mySQL dot create connection and Define
the connection object over here. So first you need
the That’s Local Host because I’m just pointing
to the local machine. So it’s going to be
local or otherwise, you can provide the IP for the database the user
for the database server. It’s going to be root
and the password. So the password is password
in this case database. And that’s configuration
for multiple statements John. Is it true? So now the connection object
has been created now, we’ll just start the connection. And it takes a callback
which provides us feedback that in the connection
has been established not so it provides us an error if the connection object
is not connected. So when you stride error
handling like this So if the connection is successful,
it will just print connected. Otherwise, it’s going
to print connection field. So now we’ll go back
to my terminal and clear the screen and see
the files over here. So now I’ll just run mode Mall. So what Jess? What’s his connection field? It means it’s not able
to successfully connect to the database. It’s not a problem. I will see if the configuration
is correct or not. So think it’s looking
for the database which we have but we have created it. So first I’ll do is I will
create this database might now. So for creating
a database in MySQL, I’m using MySQL workbench. So here we have option
for create schema. So schema or database
are same over here. So I’ll just write
the name of it, which is at Utica and apply so you can configure
in the character set and the Collision for the database and it
just confirms you the query. So now we have a little
bit screechy over here. We’ll just go back to my code
and save it again. So now you can see so what I did is I just went back
to my server or JSA. Saved it again. So which triggered the node mon
to reload the node.js server. So now because I have
the database ready it’s able to connect to mySQL. Otherwise, it was
giving an error that the connection was feeling. So the database creation
has to be done before you start
with the coding part. The next step is defining all
the routes for our application. So the first thing is we are going to create different
routes for this application which are going to be
in different files. So first I will create
a routes folder in this folder create people. Dot Js. This would have the routes
or all people and in this file. I will import Express. require Express And
Define the router, so I’ll just fine. so this is a router object
for the express application on which we are going
to create all the routes so I just defined get
And the Hand of that. best friend request and response now we
have to use this router. So I’ll just explore this module
not Sports is equal to router and in my server file acting
put this in my application that will just import
const people routes equal to require. So people out here. And then in my app Chase
for the application, I will Define app dot use path. So it’s going to be /
people and the handle of that the head of that
will be people routes. So all the routes which are coming
for / people will go to the people routes file. And in that we can
handle the request which we need to respond back. So now in my evil file, I’ll just go back
to the crowd which I have which is Get so we’re here. We can make a database
connection and the query part. So for creating
the connection first, I will do one clean up. So I will create a new file which will be named
as connection dot JS and we’ll move the connection
details from this approach is to a particular file or them. So just take this and put
it over here and now you can export my SQL connection
from here module dot exports. Click the my SQL connection to me exporting
my selection from here. So once I’m a secure connection
has been supported. I will go back to my server or chase and imported over
here const my secure connection is equal to require. Connection now I can run the connection methods we
have so we just copy this line and move to my people dot JS. So actually we need
to make action over here. So I’ll just write. Connection and write
my secured connection query. As the database query
which I want to execute. So I will Define
select star from people and the call back for it. So the first parameter is going
to be error rose trees and if we don’t get any error,
I can just write row. Otherwise it will just log that what’s the error
will respond dot send Rose. We’re here in the vice
else console DOT log error. So let’s configure
now we need to go back to our terminal
and run this code. It says MySQL it’s not defined. So that’s the issue
because I would copy the mice. Kuala Lumpur tower here. So to cut this import
from here and add it to my collection file. Now, you can see it made
a connection now the next step is go to Chrome and hit
that URL so you are was localized 3000 / people. So just hit this and go back
to terminal and see if it gives
any errors or something. So we got an error that there is no such table
available on the database. Now, what I have to do is I have
to create this table will go back to my MySQL workbench. So I’m in my second one. Bench and I will
create this table on this particular database. So I’ll name it as people. And Define The Columns
for it name gonna find different parameters for people
like age Define name worker. 200 or pencil query like
do you want to execute this query is I want to
and execute that query. So now the table we
created as people. I will go back to Local Host and
done localized 3000 / people. You can see it prints out
a blank area over here because the table is created. Although it doesn’t have
any records on it. So now add the chords
to this, baby. So I’m using the editor inside
for quench for adding the cards. So just click
on what Concord and say Praveen and he’s gonna do so I get the value
for first record and the second record
apply the changes. So once you apply the changes
it will give you the queries that will be executed. So it says insert queries
for any card just by the queries and confirm that the queries have been
triggered it successfully now go back to your browser
and refresh you’ll see that the It started coming up. So these are the two records
which I ran into the database. So now we have a successful
reader base connection and it’s able to retrieve
records from the database so we can use different tools
like Postman to verify our apis so I can just open Postman
and send the request to this URL which is local
or 3,000 / people. Send request a get request to it and you can see it means
out a Json response. Where where do I
get a Json response? So This my SQL package
actually converts whatever the record you’re getting
from the database it converts into a Json response. So you don’t have
to manipulate all the array or the table structure. It provides you a picture which can be directly used
in your application, which is very good. So you can use different
types of structures. You can reuse any kind
of queries with mySQL and package and run them
on your bicycle server. So this is example
of running a MySQL server with node.js Today
we’re going to learn how to use mongodb
and node.js along each other. We want to set up
a mongodb environment and create a cruddy API based
on node.js and mongodb. So before we start
with the coding part, we’ll just go with some slides
and see what’s mongodb. And what are the other options
in the market to use along with mongodb or node.js. So the agenda for today’s session is
we will see what’s nosql. What are the nosql databases
introduction to mongodb? Installation of mongodb and building a crud application
with node.js and mongodb. So the first topic is
what is nosql. So nosql means not only SQL. So here you’re not going to work
with a traditional database. It’s not going to be
a relational database where we use our SQL or a structured query
language to get data. In this case. We are going to work
with the databases, which do not follow
defined structures for keeping the data over here. We don’t use
a query language is to be used methods or properties which are Little
on the databases and we’re going to get data
from them the key advantage of using a nosql database as a nosql databases actually
have an unstructured data which can contain a mixture
of different types of details like different models or including a key value or different types
of crop format of data. And these schema like
are the way we are going to push data into a database
is going to be very Dynamic. So in a traditional database
like MySQL SQL or Oracle, what we do is table structure
will be defined. Like how many columns
can be added into this table? And what are the values of the data which you are adding
to the columns in the cells, but in this case no sequel most of the databases don’t require
a specific schema. So even if you have one table or a collection
in this new sequel, I can have different schema for every row which you have
or every record can have its own different schema
from the other one. So it’s very flexible. It gives the developers lot of power to adapt their schemas
based on their requirements. Whatever records they’re
going to push in. It reduces the number
of unnecessary record in the database and it also allows developers
to manage database very easily because they don’t have
to convert one data form into another form. Like when you’re sending
some information from the server or the database
to the client side like a web application
or native application. They don’t need to reformat
the structure again into Json or XML most of the databases like the nosql databases
actually store in Json and whatever the information
is stored on the database without out doing
any formatting can be directly pushed to a client or a web client
or a native application. So it’s very easy for the developers
to maintain nosql databases. Also some benefits over here. Like what are
the benefits again? They don’t follow
any specific schema. So no specific schema
for nosql databases. The next point is
no query language. So they don’t use
any query language to get data. They just depend
on Intel methods and properties to get data from the database and the third
and the most important point. Here is nosql databases. Do not follow relationships or
some of them have compatibility to create relations
between different tables. But as a design practice when we architect
that complete database, we don’t create relations between the mongodb or
the nosql databases the tables which we have or the databases that you have generally
everything is stored in one record. So the next topic is what are the different nosql databases
available in the market. These are some of them databases
which are available mongodb. HEI base Cassandra
redis and new for Z. This is a very small list
of nosql databases, which you have
in the market right now. There are a lot of other
databases in the market which are following or cql approach and some of them
are proprietary to the company which has been provided them like AWS has its own
nosql implementation Microsoft has its own and other
companies have their own way of implementing a nosql database So based on your requirements
are based on the platform, which you are going
to use you can choose which know SQL database you’re going to pick so
in this example, we’re going to use mongodb. So we’ll see how mongodb
can be connected with node.js and we can make
a credit play with it. So introduction to mongodb. So what’s mongodb mongodb
is open source, non-relationship database that stores data
in a form of Collections and documents in mongodb. It’s like open source. It does not creates
in relationships. So what we do is we store
everything in form of Collections and document. So a collection can be
considered As our table and a document can be considered
as a row for a normally debased when we create a table. We Define how the structure of the table will be
and all those things. But in this case
when we create a collection, we don’t Define
any schema over here. We just tell that okay. This is a collection and this will hold
what type of Records which we want. I will create a collection
named people and I’ll just read the people connection
and add all the persons which I have in that collection. Okay on the other hand documents
are equivalent to our But in the same case documents
do not have any schema. So Rose have a schema of inputting data
into the database. But in this case a document
does not have any schema. So one document can be
completely different to another document. If you create two persons, the schema of the person
can be completely different from the other person but document will always be inside a collection document
cannot be free flowing here and there they always have
to decide under a collection. In this case, if you have two documents and like person 1 and person to those will be
inside the people collection next part is it preserves most
of the functionalities while offering horizontal
scalability so we can scale our mongodb database horizontally and we get most
of the features working and it does not limit Us
in all the features and we are excluding the system. Next thing is it
stores Json documents in the form of Collections
and having Dynamic schemas. So like I said earlier all
of the documents, which we are going to create
like person One person to person 3 they can have
a different schema. They don’t require
to follow the same schema. So a schema means like
if I have a record if I’m creating a person and I have added a key a key
will be a value in the system. So the key name can be
first name for the person one for the person
to it can be first – name or in the person
3 it can be named also, so you’re not fixed with a specific schema
for storing records it? It can be anything. It’s very powerful. It can help the developers a lot because they don’t need
to store null values or unnecessary records into the database just
to standardize everything. They can keep a dynamic schema, but it also creates problems
for the developers because like I said
in one record or in one document a particular key can be named
as something like first name and in the different document
it can be written as name only so it creates a lot of problem when the same database is been
used by different developers because they still have Follow a specific schema
when they’re importing the data. Otherwise, they might have
the same value for the record on different keys in the system. So we’ll explain you
this an example later on but we’ll cover this
on the code example. So the other thing is we don’t
create relations in mongodb. So the benefit of this
is we don’t need to look for the same record
another table and get the data and combine them
using join queries and all in a mongodb environment when we develop we record or we store everything
for A particular entity in one document so example
for that will be like, I have one person record and that person has a salary so
instead of reading a relation between a salary table and the person’s
personal information table. We will record the salary of
the person in the same document. You don’t have
two different tables for storing the personal
information or the celery. You just have one document which stores the celery
and the personal information, which is very helpful because now you’re not creating
any queries on you are not creating any joint. He’s between both of the tables
and complicating stuff and this also reduces the effort which the database
has to process when writing queries. The queries are much more faster because everything is
at one place. There is no relation
between different locations. There are differents workarounds or solutions to Duke relations
between the mongodb collections, but generally they
are not practiced a lot features of mongodb first feature
is indexing replication, either queries schema-less
and shorting. These are some of the features
which were going to discuss later on the sessions and see how these can be used
in our applications. So we’ll start with
mongodb installation will see how we can install mongodb
on system and run a mongo DB server and create
cruddy Pi based on that. So so the first step is we have
to go to the mongodb site. So it’s called mongodb.com
and over here. You will go
to the download Center. So the URL will be mongodb.com /
download Center / Community because we’re going You download
the Community Serve over here. That’s why we are using /
compete in the URL. So you will find details about
the community server the version of it the operating system and which type of package
you are going to download. So the operating system
is based on your choice, like which operating
system you are using so you can pick it
from Windows Linux Mac and different releases of Linux
different variants of that and choose the right one. So because I’m using
a Mac machine, so it says Mac OS X 64 x 64 so that’s the correct version and I will download the TG
said file from here. So it’s taking some time
to download let’s do it. So this package is
already download on the system. So I’m not going
to download it complete. I’ll just cancel it
and I will show you where it’s going
to be downloaded. The download package is
in my downloads folder, which is this one. Okay, so we just
download this package from the mongodb side. This is a ZIP file. So you can just uncompress this file and get
the content extracted from this. It’s a different version
of type of a zip file so you can just extract
it and get the content. So once you open the folder, which you get
from that zip file, you’ll find a bin folder. Okay. So based on the operating system you are using we will get
executable files over. So the name of the executable
files will be seeing for all the operating systems, but The type of the file
will change so these are all Unix executables when you
are using a Windows machine, you will get exe files
or Windows executable files and you can use them
to start your Mongo DB server and connect to it. So the first thing which we need to do is we need
to start the Mongo DB server. So from these files we
can start the Mongo DB server. So the first five if you want to use is
the mango tea this one, okay, so this is executable file. This executable can be used for
Starting the Mongo DB server. So this is the Mongo
DB server executable. So how to start this file or execute this file
on a Windows machine. You can just go to command
prompt and run this file same for a Mac machine. So using my terminal
I will navigate to this folder over here. You will see that I have
these files over here Mongo Mongo atom bomb buddy over here enough
to run this executable. I will just run Mongo d – dashdb path equal
to the Mongo database. The DB path is location
where your DB will be stored. So by default it stores
at the root URL which is /data / DB but I don’t want
to store it on the root URL. I want to store it
inside my user folder. So I will just tailed which means the user
folder / data, which is the data folder
and the DB folder. So these are not created on my system by default
have to create them. So I’ve created them earlier
so I can just use them again. So I’ll just run this command and to start
the database server. for me So when this command I got an error the reason is
the command is not correct. So you have to write like this X in the editor question
mark over here. So just turn like this and this will start
your Mongo DB server. So once the server is started you will get
this line at the end which is waiting for connections on Port
two seven zero one seven. That’s the default port
for Mongo DB server, and it’s looking
for connection to this URL and you can find all the other details like where
Are the files are available? So it the files are available
under users Parveen /data / DB and all those things. So we’re going to
start a connections to this mongodb database now. So one approach is using
the Mongo executable in our code will open a new tab in my terminal or if you are
on a Windows machine, you have to create
another command prompt window. So the first tab is running
the Mongo DB server. You don’t need to close
this the second one. It’s a new terminal
window over here. I will run all the commands, which I need
to connect to Mongo. So the Mongo DB
server is running. So now I have to execute
a different file which is mango
and the bin folder. So mangodi starts. The database. Mongo is used to connect
to the database. So just run Mongo. So once you’re on this
this starts executable, which is are repelled
for mongodb and over here. I can write mongodb queries and functions over here
to get data from it. So I can just say show DBS which prints out all the DBS
or the databases in the system. I can say use admin. And say show collections
which prints the number of collections which I have
in the system same way. I can just go to show DBS and create a new Direction
like use to Rekha if the collection is
not available it automatically creates the collection otherwise
switches to that collection. So now the next thing
is using this collection, so I’m already in the database and I’ll just run
commands show collections. So I don’t have any collections
in this database right now. I do request a new database which has created by using
The use command now we’re going to use this database
and add records to it and create collections to it, which will be used
in our criteria. So before moving to
the development part of our API, we’re just going
to show you another rule which mongodb provides us, which is called the compass
mongodb compass crisis as a UI to connect to the database and see
what records we have or what databases we have. So you can download
as a community Edition from the mongodb site and install it
on your system by default if you’re using the community
to provide Rides is read-only access so you can do any read
operations from the system. But you cannot use it to store
any data into the database. It starts with the
by default URL which is localhost and the port and these are
the default URL configuration for our mongodb system
running on a machine. So we’ll just connect to this. So now you can see
it provides us information about what databases we have. So these are the default
databases which is admin and can go back to our data set
and the local database which we have and this is
a collection in that database so And just go to this collection
and see what records I have in the system. So these are the records
which I have. These are about the starting
of the system went into mongodb server
started on my system. It started at 11:56. So these are the information which is mongodb is touring
by itself for debugging and all but we can create
our own databases over here and store data into our system. So I’ll just go to the home
and create a new database name it as a do-deca. So it’s not showing up
the idyllic a database. We’re here because I haven’t
added any collections to it. So it Just created
a database in memory, but didn’t physically created once I create
collections into it. It starts storing information. The database I cleared again
is Erica and Collection image people create database. Now, you can see upgrade the database and it has
a collection called people. So now from here I
can insert records and see how the rules will be stored
in the database, but just create one record,
which is ID. This is a unique ID
for that particular record. So this is auto-generated. You don’t need to read it again. And these are the values
which I want to store. Now. I have this record store
into my system with a particular schema which has ID and name
and I will store another record which will be like first name
will be Ahmed and sell re any so can see it
didn’t give any errors that to the schema is not correct or you have
some problems with it. It allows to store
the data as whatever the values you want to so
it’s completely schema-less and you can record these values and even also run
queries over here, so The right a filter query
to find out a particular record from the system. So you can see it as a list
you can see it as a table. So table is for people who are very familiar
with the relational databases. So they can at least see how the structure
is been cleared in a table, but most of the time we
don’t use this table kind of a layout just a layout to see how the data structure so
this is mongodb compass. So we have to now use
this database to get data into acrid a pi. So to create a node J square d p i we need to create
a node.js project. So we’ll just close. Compass over here and go
to my terminal exit this and go to my user folder some
with my user folder right now and I will create
a new project or a folder. So just right. Okay dir node Mongo API go
to this shit folder and you have to create
another node project. So when you need to create
anything as anode project what we do is we run npm in it. That’s the first step so that creates our package
or Json file. Okay, so we run. That so it also like
what subject name so we’ll say among Epi the version of it description all
the files everything. So it gives us a preview
that package is in file will look like this. So it is good. Just press yes and our package is envelop
you created for us now. I will open this project folder
in my visual studio code click on open node Mongo Epi, so it hasn’t package.json file and now we have
to create other files which we require. The first file is going
to be the index dot JSP. That’s why we start subject and then we have
to install all the packages which we require. So this project
is going to be dependent on different node packages. So we’ll have a dependency
on Express will just write those over here Express
handlebars Mongoose body parser and not mom. So these are the dependencies
for this project. So we’re going to install all
these dependency No Object. So for that what we have to do is I
will write and p.m. Command, which is npm install. – – they’ve Express Express
handlebars Mongoose body parcel and node one So I’m installing
node mon as a dependency to this project. It can be added as
a global dependency also for your system. But in this case, I’m just using it as a local
dependency for my project so that it doesn’t installs
it as a global package once all these
packages are installed. You will get these added as dependencies
in your budget over here. You can see what packages are installed which version
of them are installed over here. Now, we need to connect
our node.js project whether mongodb so I’ll just go to the index dot JS file
remove all these packages. Names those are not required. So first we’re going to use
a package called Mongoose. So Mongoose is
used for connecting the node.js environment
with the Mongo DB server. So it’s a adapter
which connects to it. It requires very basic
information to connect to it. It can extend it
to very complex implementations, but for starters, we just have to Define
what URL we need to connect and it automatically
connect to it for us. We will just write require Mongoose because we installed
Mongoose in our system so we can just require
it and we’re Code so once we require this
will get the package and we’ll assign that package
to over constant, which is named as Mongoose. The next line will just
write Mongoose dot connect for cleaning very
simple connection without any configurations. We just need to provide
the mongodb URL. So in this case
because I’m setting it up on my local system
after right Mongo DB: / / localhost because it’s on locally
installed on my system 2701 7x the boat / Database so I created the database
as any Rekha, so we’ll just try
to recover here. So this is the database name,
which you need to connect. Okay, so I don’t need to pass
any user information or anything because there is
no authentication set up on the database so I
can easily connect to it. But once you add any
authentication to the database or the Mongo DB server, you may need to provide
those information on here. So the next thing we have
to define the Callback, so is the database connection
working or not. This will help us know
the connection status. And so in this callback
we get error if there is any error between the connection
it will just give us in the database connection
is not success tour why it was a problem if it is successful, we won’t get any error and we can start
accessing mongodb database. So in this case, I’ll just write if not error
console DOT log success and else console log error
connecting to the database. Okay. So make sure you read
a base is running so you need terminal window go
back to the same term. Window and make sure
your database is up and running once you’re sure
that it is running. You can just start
this node.js budget. So in the earlier examples, what we did is
we ran the command which is node index dot JS. So give me an error that the current URL
parser is depreciated. So I have to pass
another option to it. So it’s a configuration which I need to pass
in the Mongo client. So that has to be passed over here is you can parameter
I’ll just pass it over here. This configuration is
required to remove this. Morning, so just
edit this configuration and will restart the server. So before restarting
the server will show you that it printed of success. That means the connection has
been successfully established between the node.js
and the Mongo DB server. So we’ll just close
the node.js server and then the server again so it prints out success and
there is no warning over here. So now we’ll stop
this again and instead of using node index dot JS
will write a different command which will be settled
in our script part of are so right stop. At the start command for us to start command will run
node Mall index dot J’s. So instead of running
node space index dot JSP in Run npm start now. So this engine start will point
to this scripts over here and looks for the start command
and runs this command over him. It’s the same kind of output
but what’s the difference over here is if I go
to the index dot J’s file, I will change
the code over here. So Arjuna to success connected
and save this way. So what happens is the nude
mon actually watches our files. So it will see if the file
has been saved again. It will start the node.js
code again for us. So based on your file changes, it starts the code
again and again, which is very helpful. You don’t need to restart
the node.js code again again by yourself. It automatically handled
by the system. We are connected
over mongodb database now, we need to get the data
from the mongodb. So for that what we do is
we create models models are for creating a schema. And for accessing the data
from a particular collection. So in this case, I’m going to create
a new folder name it as model and and this will create a new file which will be
courses dot model dot JS. So this is a model
for a particular course and over here have
to import Mongoose again. So when you are working with
node.js and you have dependency on a particular package and then Tim package
you have to import that in every file, which you need it. So dependencies in node.js
are not declared AS Global they
have to be perfect. If index dot JS requires Mongoose shaft
included in Mongoose. And if course dot module or JS requires that have
to import it over here. Also some just write this line const Mongoose
is equal to require more goes after create a schema. So I will write
where course schema because new Mongoose schema, the way Mongoose works as Mongoose helps us
in creating a schema for Mongo DB so like mongodb
Does not care for the schema how you’re extorting the cords and that can be
a problematic thing. Like I said same collection
can have different documents with different keys
for the same type of record. So example for that will be like if I’m storing something
and if it has name and somebody else
towards it as first name now, although things are
same the values are for the same property, but they have the different key and if you have lots of keys
which are completely different from one record
to another record. Then it gets very hard for developers to
standardize the code. Or they have to write condition that in this record
or the Scott in the document. So just to remove
that kind of a confusion or an unnecessary
burden on the code. What Mongoose providers
is it provides us a way to create a schema and Define how the data will be
added for that card. So in this case, we want to define a schema
for a course schema will be like course name course name
is equal to object and this object will
Define the type of it. So the type is equal to string
and as it required or not. So we’ll just Define
it as required. So this is the message which we
get to if the field is stored as not required like or blank read next thing
is the course ID. So this can be ID which is
or the internal use. It’s not the same ID
which we get from mongodb. So it can be internal ID which agriculture uses
for naming their courses. So well write string again for the time course duration
will be type strain and course we abstract. So this creates a Schema now
will Define the skiba as a model for Mongoose will write
Mongoose dot model and the first parameter is
the name of the collection. So refine as course and the ski operate
when we go to store any record to mongodb
this particular collection, which is named as course will require
this schema to be store and it will only drag the data
based on the schema. So now we have
this model ready now. We have to create
Express application to connect with our browser and send responses back
to us or get data. So the first thing which we
have to do is now go back to the index dot J’s file. So instead of keeping the
connection details over here, we cut them from here and create a new file with set
the mongodb folder and name – connection dot JS a sort of natural energy is let’s
rename it to index dot JS because then you
are creating a folder in the get node.js application. It should. Have a because that allows us
to export different content or different information
from that particular folder. So just create
an integer J’s file. And in this we just have
the connection details now in this file. I’m going to import
the course module which we created. So just say constant course is
equal to require course model. So it automatically
choirs the code and it starts up the model for
us now in the inductor JS file will import connection
equal to require model. So this creates the working
As you can see once I put it in the file
and see success connected. That means the mongodb database
has been connected to this application. Once we’re done with cleaner. We have to start eating
our express application. So for creed express
application first, we need to import Express
7 bowed Express from Express. Alright constant Express equal to require Express now, we’ll
create express application. So we’ll write legation
equal to X press like this. Okay, we will import
other packages which we need. So we need Path
on the path package. So path is until package to node.js so we do need
to install it it’s available when to install
the node.js code. Then we need Express handlebars. So I just try
to express handlebars require extra handle bars
this contest body part, sir. So that’s used for parsing
body information from on code. So just improve that so these are
all the packages which we require in our code. Okay. So the next thing
is some configuration on the explicit. Fishing so first we have
to say application dot use and we want to use
body parser dot URL encoded and extended
equal to true now. We will set up
our application to send for handle out the root URL. So for that I will write
application dot get and this is for setting up
the get request for system. So we’ll write /
and a callback for that. So in the Callback will receive
requests and response. And in this callback
will just send response dot send some HTML. So I’ll just write
H 1 that’s right. Hello world and
start this operation. So once done I will go
to the end of this code and Define application
Dot listen to four which is three thousand
and Define a callback for it. So if it is successful, it will just
sprained sour started. This is the server for Express
and this is for the connection which is done by
using node.js Code. So both of them are working now. We’ll go back to my browser
and see what’s the output. So I’ll just run local 3000 and you can see it’s
prints hello world. So this is kind of
from simple HTML string which we have and we’re going
to use handlebars in this code to create a proper views
by using handlebars first after go on top of my file and write application
dot set of useful. So this is a folder which we want to create and this
will be storing the views. So right path. Join directory name
and the be useful. So what I’m doing is I’m setting up the views folder
for this application the name of that is views
and we have a path. So the second term
it is path over here. So the underscore DNA over here. This is a global variable
which is provided by node.js. So this provides us the
information about the directory in which this court
has been executed from so which I can just get it. From here and from
that particular directory. I want to access
the used folder. Okay, so that will be equal
to the Views folder which I have over here and I
just created this folder. I have to configure
handlebars and this is it. So right application dot engine. So this is for
the application engine which is handlebars
or Jade this case. We’re going to use HP S or handlebars and we’ll
Define Express handlebars and Define all
the options for it. So it requires configuration
about Kind of extension. We need to follow
that will be H PS and the default layout for it. So that will be no views folder. So I’ll just write
main layout with here and then we Define
the layout directory like where is this layout? Just dir name + W / layouts. Yeah. Now I’m to tell node.js or Express that it has
to use the view engine which is handlebars. So for that will just
Define application dot set view engine HPS now
our handlebar server is sick and now we can configure it to work with the Mongo
DB connection first. I will create a new file
which is index dot h BS because this is going
to be the root URL. So it’s index.php s and I
will write hello World – eh Yes. Okay. Let’s see. If this fight now I’m going
to change the code over here which is instead
of sending the response like restaurants and I’m going
to use restore Trend or response to a tender. So it’s going to be response
to render the name of the temple which I want to round over here which is going to be
index and any data, which I want to send
to the template so I don’t want to send any data so I can just leave it as
blank object and I will go back to my browser and see what the output
so refresh and it says that there is Error it’s not able
to find the layout. The layout is something which we declared
in our code over here. Actually, I need
to add the view spot. I have to add the layout so layouts are and of
Base templates for our template so they can be used for setting up default configuration default
scripts default CSS elements for all the pages which you want to create. Okay, so they kind
of Master Pages for us and for creating a layout
first the layout are to be inside the views layouts folder, so I will create
a layout folder over. In that the default layout
is main layout so I will just write mainly out of HPS
include this laboratory. I have to define
a base structure for the layout. So I’ll just write div ID
is equal to main layout and right triple equal to body. So this body is
an internal variable which is provide bike handlebars this variable actually
holds the template which I actually requested for if I’m requesting index dot h BS
or index template in my render. That means this body will hold
that template so intently when this code runs it
automatically picks all those templates and put them
inside the layout. So now I’m going to run
this code and browser. So I’m just going to refresh
this page and you can just see that the layout is div ID main layout
and it has a text which is handlebars HPS, which is coming
from the index dot h PS file. So just change it
to handlebars save it and refresh again. So now our Are configured
and now we need to connect to mongodb and get
the data from mongodb and show it on the page. So first thing which I need to do is I need
to create a controller. So as a developer, we should not create all
the routes and our index file. It gets very complicated to add all the routes
over here sin think of it. Like if your application
has 50 routes, you cannot keep
50 routes over here. It’s going to be
very complicated for you later on to manage. So the best practice
is create a folder called controllers were here and in this controllers
folder Define on all the routes which you want. So in this case,
I’m just want to have courses. So I will write courses
dot JS and this will be the file which sets up the routes like all the information
about the route and also connect
to the mongodb to get the data and return the template. So the first thing which we need to do is
we need to import Express and will write require
Express and mongoose. So once we imported Express, we need to configure
all the routes what routes are to be handled
by this controller. So for that what we need to do
is we need to set up our router. So I will try to Define constant
equal to express router. So this enables us
to give the access to the router the express router
now I can Define router dot get router dot whatever the URLs
I want to configure. So in this case, I just want to use
router dot get and Define a route, which is the / you URL
will Define the Handler for that which will be a factor
of function and Quest and response from here. I can say response
dot send courses controller accetta the route
for the course controller, but I haven’t use
this controller anywhere in my application. So what I have to do is I have
to go back to my index dot JS and when I’m writing
application dot get this F2 right location
dot use slash chords and the course controller. So the controller have
to import in my files. Alright course controller
require controllers / courses have to import
it over here. So this tells node js
or the express that which controller will be used
over here once done. This configures the course URL
and now in my code, I will Define how the mongodb
can actually created. So one thing I missed
over here is in the courses file the controller file
after export the router. Otherwise, it will not work. So if to export it
from here and you can see it’s successfully
starting the application. So everything is working fine. Otherwise, it was
giving me error that the router dot
used were not working. So not Willis
configured correctly. You have to export the router from the control of I now I
will go back to my browser and search for courses
or course over here. So you can see
it’s printing the result from the course controller. Now you already imported the
Mongoose package in our code. So now I have to create a
From that for that I will just write
const course model like this and Mongoose dot model course. So that’s the model which we already
created in our file. So as the models while this one so the name
has to be same course and the course folder course, so we get the access
for the course model now on this course model. I can write methods to get data
from mongodb first. Let’s create the URL for lest like to create a list
of all the courses. Which are available
to a delicacy database for that. I will write
course model dot find and Define a callback to it in this I will get
error and docks. So error is any error, which I received when the connection is made
or when the query is running and docs is the document which are received
from this collection. So this course model actually
points to a collection in our database. So anything which is
inside this collection over here will be as documents and will be received in this
documents argument over now. It’s just The basic
simple check over here if there is no error then respond should send this course
controller again is to see if everything is working. So now I have to look
for course / list. So how it works is when we Define the controller
in index dot JSP file. We Define a base URL for that. So it’s course. Okay, and once we start
writing code or the get request in our controller, we defined which URL it
should be pointed toe. So if the base is
course / list over here, that means it’s going to be
Like this course list, so I’ll just go back
to my browser and mr. Coats over here. Alright course lest
refresh the page. It means the same content again. So it’s able to make
a connection to the database and it’s working. Otherwise if there was any error
this put in a printed out and we could have just enter the header
like small dots and errors. So if it is working fine, that means everything is correct and it’s able to connect
to the database and work now, I will just see
how many records do I get? So I’ll just write console DOT log docs refresh
the page once a refreshable. Go back to my console over
here the terminal window you see there is a blank area
coming in that means there are no records
in a database like there is nothing so
after set up that data so for that initial data set
up I will just go to mongodb Compass and set up
some basic data over here on this connection screen. I will just connect
to the Lucas mongodb instance in the Elder Erica database after need to
create a collection. So the collection name
will be Course and clear this lecture now in this collection have
to make some records go to my schema and see what is the scheme
of my records. So first I have
to have course name. So I will write
course name node js with Mongo course I’d is going
to be one course duration is going to be one course we
is going to be one insert. So these are the records which are added back
to my database now. I will go back to my code again
and refresh the same page. So once Refresh it should get
the data for the latest courses. So go back to our system and refresh this it still
pointing out to null like blank array. Let’s see why
some connected Erica and requires course model
whose model defines like this. I think there is a problem
with the model. Let’s try to do one thing
promo code only will try to add a new record to it. I’ll write a simple code snippet
for setting some records, of course is equal
to new course. Course model course dot
s copy the names from here. It’s name is equal to
node.js course ID is equal to 2. Okay, and I write course dot save to see
the record back to the mongodb. So when this URL and we’ll go back
to the database and see what we have. So the reason is I think the collection name
got written as courses that while it was not working
just comment this code. And go back to my course model. So it is able
to store the record. You can see like it’s able
to added a new courses record over here. So we’ll debug this out later. Now. We know like where the data
is been getting stored. I will just get the data
back from there. So I’m just going
to refresh this page again. This is a single record which was added by
the same query which we ran. So now we have a record
which is in the database. So now what you have to do
is we need to create the list of all the courses so for that I will
just try to respond start render. And I right-click on the data. I’ll just pass data is equal to talks and might use folder
to create a new template which is list dot
h PS over here. I will just get the data which is going to be used
in my template. So just write HTML content and courses and just writing
a very basic HTML you can extend it to write use bootstrap or other CSS Frameworks
in it so courses and I’ll create a table over here and each row
is going to be having. The record so I just write
TD first is going to be the th which is table heading. So will you find th
over here and ID and name? So the next record
is going to be the record which I want to get
from the Mongo DB. Alright, so this
is going to be titrated. So we hear handlebars
provides us methods, which can do I trations for us. So we just write curly brackets
double curly brackets each. This will I tried
the content so in the courses when I was writing this
I was sending data. So I’ll just write data over
here and where I need to end the iteration for the HTML
just Define it over here. So this defines that whenever
this page loads look for data and if it is an array, I treat this content
multiple times so over here now I can write this dot coats
ID and course name and instead of th and use Deedee’s now refresh
the page now again, see Spending results which are ID name
and to is the ID and no cheers. So now we’re getting data
from mongodb and putting it inside our HTML content. So we have completed example
of getting data from mongodb. Now. We need to push data from
our page to the so for that. What I will do is I
will just create a new template. I will just write a new template and Define it as ad course
in this ad course. I’m going to create a form which will send data
to the over mongodb or node.js API. So we’ll just write
some basic information. Like what’s the action
action will be cores and Define the method for that. We post this right over here now to create a forum for sending
the course information. So I’ll just write P input value is equal to blank name
is equal to korres name and copy these records create
a labels for them label course name
it copy this layer. Will this will be
duration and peace. This one is not required or just remove this last one
to wish and peace. Let’s see how this prints
on the page. So first have to configure
the route for that. So I’ll just write router dot k that’s for creating
the post page which is going to be at had the right response dot render
had course now, let’s go to the add page. This is not defined
that cell reason is I haven’t passed it
over here refresh and it prints this form. So the CSS or the layout
for this form is not correct, which we can fix later, but I will just write
p r over here to at least make a deal sense. Now, we got this form and without a submit button
at the end of this form. So let’s try to add
course over here and defined as type some so
now this form is created now, I have to preface this form and And press submit
when we submit the form. It says cannot post
course add over here. The reason is we
haven’t configured the URL for that so we only
configure the get page but we haven’t configured
the post URL. So for that I’ll go back
to my router and also configure the post URL so get or post will render
the form again. So if I add something
like this over here, it shows the same form again, but doesn’t do anything
on the actual form like it will not add anything. So if I put a list you can see
it’s only one list over here. I have to write the code
for In the records in the database and retrieving or navigating back
to the list Pearson. So for saving the course already wrote
in this small code of here, we’ll just use this code. So we’ll write new
instance of codes save in this save method you
can pass a callback over here, which will let us know
that is the record saved in the database or not. So now I can check if the record has been saved
or there is a problem with that. So we just write error or talk. So Doc is the document
which is saved. So I’ll just write
a condition to check. If not her then do this else. If it is a real State then do this so successfully able
to store the records. I will just write
response dot d direct. Okay like this and send
it to course / list. Otherwise just right
response dot send error occurred so it could store the records, but we haven’t used
the correct values which we need to store so over
here I have to write request. Body dot the value. So let’s see what we get
from the request. I’ll just plug this code
not to run this code completely and I will just try to console DOT log to see
what values to be get. It’s going to be
shown over here. Just send some Chun characters
from here response or the same. So these are the values
which are getting sent from the client side
to the server side. So we can just write
response dot body dot codes name of this code and response Dot right duration
request dot body dot duration and course fees. There was another field
which is course ID, which we haven’t received
from the client side. So we’re going to put it
from the client side over here on the so I’m going to write
math dot random create a random number multiply that with a big number and get a really random number
math dot seal it does. So just to create
a random number for every ID which we want to store and add
it to the course ID over here and save this code. So let’s run this code now. So first I will write node. Js Mongo DB direction is
something we see something see if the record course list. It’s not able to navigate
to the Sewer. Let’s see why it says
cannot set headers after they are sent to the call. All right. The reason is cause at the end of go die
forgotten to remove this line. So actually don’t
need this line. I will just remove this now that was for showing
the form again when I post the page
whenever sending a request from the client inside and it’s getting stored
into my database that’s a asynchronous call. And in that case that mongodb
will respond later on which we are going
to receive in the Callback cause I had this line at the bottom this forces
the response to be sent to the client side the moment
it receives it and again after mongodb stores recorded
sends the response again, so technically it was sending two responses from the server
to the client side. That was actually feeling so I have to remove
the last line over here you Listen one spots from this code. So run this code again
refresh the list page. Now, you can see it has
a new record added which is node.js mongodb. Now. I will go back to my code and say had so
we’ll just add some records like chunk characters
over here and add course. So it added at course also, but still you can see the ID
is not printing over here. The reason is
because we have defined that the ID should be
in strength and what happens is right now. This is not coming as trying
this is coming as a number. Because I’m using Mac dot seal and Method random
to create a random number for the ID that’s why
it’s not skating stored. So what I have to do is I have
to convert this into a string. So I’ll just add
a blank string to it. So it converts everything
to a string and hope it works. So I’ll go back
to the add course. It’s still not adding the value. Let’s see. Do we have something
wrong in the schema? The schema says course ID with that be small and
when I’m sending this request, I’m actually using course ID
with a d capital. That’s also another thing. So when we are using Mongo or mongodb and Mongoose Mongoose
follows scheme approach, so it forces you to define
a schema for that if your schema is not correct. It will not store the data and it will not retrieve
the data from the database. Also, it only looks
for the values which are correct matching
with the schema. So, okay, let’s try again. Add course now you can see
it added a random value to it. So just to check
if it allows different types. Also, I will just
remove this conversion to string and run this code. So I will go back to the add
I’ll just write checks string that’s the codes name
ratio fees course. Okay, so it allows you to save
the data and get the value. I think it’s internally
converting the value to string and formatting it as string
to store in the database now, I will go to the compass just
do a refresh over here and you can see
all the Zotero here and all the values are stored
as strings over here. So you can see like some
of the values are different some records have some key
some records have other keys, but all of them are
the same collection, so it’s completely schema-based. Only Mongoose is enforcing
a schema over here in this example
and your application. You will use Mongoose
to create the scheme. Otherwise, you don’t need a schema for mongodb
as a best practice. You should create a schema or are Mongoose
greater connections. Otherwise, they will
be some issues. Like people are using different
keys for different name like in same example. I was using course ID
with a d capital and actually the implementation
was course ID with Addie small and it was not working correctly if that schema check
was not there. The record would have been added
and I would have received like Bell use or undefined values for that
and that’s a problem. So that’s why Mongoose is good
to create schemas and all in our code. All right. So this covers our basic example
of setting up a secret operation with mongodb and notes. Yes. So the part which we added over here which is ad
with Awestruck method that is actually example
for creating a API also. So even you can treat this
as a CPA or a restful API, you can just instead of Simply
to use directing the record from here to a different URL. You can just respond
with a message or something that record
has been successfully stored. So that will be equivalent
to creating a restful API from this code. So instead of saying you direct
I will just write press Jason and Define message successful
and status is equal to okay? So this way this can be also
converted into a restful service and you can use this
in your react or angular or native applications, which you are going to create. So this is example
of connecting mongodb with node.js and creating
a crude operation with it. So we have lamented get and post or add
or receive operation or find operation
by using mongodb. Welcome to the notice Docker session for this
particular session today are Then du is to
learn about Docker. What is darker the basics of Docker see how we
can use Docker with node.js and why we should use it. Also in the end will be having
a small Hands-On session where we’ll be using node.js along with Docker will deploy
a small node.js app on Docker. So let’s begin. So what is Docker? Well a definition of Docker would be Docker is
a containerization platform, which is used for packaging and application and
it’s dependencies to whether within a
Docker container this ensures the effortless
and smooth functioning of our application
irrespective of the changes in the environment to put it in little simple words
Docker is a tool designed to make it easier to create
deploy and run applications by using containers
now no matter where you learn about Docker whether studying it online, even on the official website
of Docker the documentation, you’ll always come
across The word container so it’s obvious that the question
might arise in your mind. What is the container but before
moving to that let’s start with this me my very
popular mean regarding Docker as you can read. It says it works on my machine. Well, then we’ll ship
your machine and that is how doctor was born in exactly. This is the reason why Daka was
made no matter what technology or platform you are studying. If you read about its history
why it was made if you go for the question
why why it was made you’ll get to know it better. You will get to
understand it better. So yeah, the problem that many of the developers
were facing all are still facing were not using something
that Docker is that if you have ever done, let’s say web development
using PHP or.net or node.js or any other web technology. You must have heard
or used the phrase, you know, while development
the someone is saying but it works on my machine
each programming language or any server platform. Has their own configuration
files and settings. Let’s say if you are developing
a web application using PHP. There is a PHP dot ini file
for configuration. If you are working
on dotnet dotnet projects have a web.com fig file the servers that you use let’s say
Apache or nginx. They have their own
configuration files. So it’s highly probable that these configurations
are different on your local development machine
and the actual server. Where you are deploying them and it can be a time-consuming
process a massive headache for you to get things
working on the server to solve these problems. What Docker gives you
is a very easy tool. We’re in you’re deploying
application on the server and Docker takes care of everything
regarding the application where it’s configurations and settings and
everything remains same on your local environment and the server as well
and many other things. So this diagram is
a very good illustration of how doctor works. This is the
host operating system which in my case is Windows, it can be windows or Linux or Mac Docker runs
on any operating system. So upon that over the host OS we
have the darker the application and doctor can have
multiple containers where each container
is running an application. So this container
can run a PHP website the other container
can run a.net website and they both are independent of each other
the Trees and everything. So it’s like isolated
environment separate from one another over here. You might have
a question regarding like how is Docker different
from let’s say virtualization. You might have heard
of platforms like VMware or let’s say virtualbox those platforms or
that technology is the diagram that you see on the left where it’s a machine
level virtualization, you know, so over your actual Hardware you
Stall multiple operating systems that’s very heavy on resources. Obviously if the virtualized OS
that you’re installing if it needs four gigabytes of RAM your OS and your platform
should have more than that to give away
four gigabytes of RAM to just a virtual OS and running multiple ones means
you need a lot of resources ran CPU storage cetera et cetera. That is also a kind
of virtualization, but that is machine
virtualization in our case. What we are doing is just a operating
System-level virtualization where the OS our OS let’s say
in my case Windows gives kind of a virtual space to
just an application. That’s it the
but the application still uses the same shared resources
of the underlying OS so the case on the right is like a diagram of
a containers container system, which is similar to Docker. So the app 1 f 2 F 3 these
running in their own containers and this container engine
you can imagine as talker. So again these share the things
of the operating system. So they The same Ram they
share the same storage, but they have their own kind
of isolated container in which they run. If you want to read more
about this in depth, you know, you can go and read
about os-level virtualization. But again coming back to today’s
agenda will continue with seeing how dark our works and then
we’ll have a look at the demo. So moving on to Docker Basics. So to put it simply
there are mainly three things you need to know
about in Dhaka the dockerfile. Docker images and
the docker container dockerfile to put it simply
is just a text document that text file has a list
of commands for Docker where Docker will know
exactly what to do. That’s it. It will say in our demo
how to create that and what kind
of commands it needs Docker image Docker images
a read-only file the talker creates for itself, which then it will use
to run your application in any container Docker file
is a text document so it’s not read only its Human
readable and editable images that’s kind of a compiled file that Docker creates for itself
and it’s read only file and finally Docker container is
where all the action happens. It’s actually the running
instance of a Docker image. This is where the
real thing happens and you know your
application runs now moving on we’ll have a look at the Hands-On demo of running
an odious app in Docker. But before that, let me tell you something
about the prerequisites. First of all, if you are using Windows go to task manager go
to Performance tab over here and you should have
virtualization enabled over here to running Docker
on your system. If you don’t have
that, you know, you can just search
for like Windows 8 enable virtualization or Windows
10 enable virtualization. There is a high chance these days it will already
be enabled but just in case if it’s not you can search
for this will usually get a link like this of Microsoft website. If you open it and it will have
a step-by-step instruction of how to enable this. This is obviously for Windows 8
if you’re using Windows 10, you want to see and what steps
need to be done for that. That’s one thing
if you are using Windows 10, you’ll obviously if you go
for Docker download you’ll see they have this product
called Docker desktop. Now this the prerequisite
for this is for Windows runs
only on Windows 10, not any version below that if you are using
any operating system below At let’s say like me which is Windows 8 you
can use Docker toolbox. So yeah, even over here
it tells about checking the virtualization
is enabled or not. And over here is the link from where you can download
the docker toolbox. It’s a GitHub link so you can download the exe
from here the latest one from here install it and once you have it installed
you will see these three icons. Will interested in the first one if we open that it
might take a few seconds. So yeah, you should be able to see the standard
the docker way like on so if you see this, it means your Docker
is successfully installed and you’re ready to go. Okay? Let’s start with our Docker
and node.js tutorial. So why use node.js with Docker? So as it speeds up the
application deployment process deployment becomes easier where the a lot of things which you don’t need to look
at while deploying if it runs locally on your Docker container surely
it will run on any other machine or any other server on any Docker container
application portability increases you’re developing
on Those deploying on a Linux you don’t need to care
about all that if it works in One controller, it’ll work in another
container simplifies the Version Control process promotes componentry use
very light footprint and puts less
overhead on the app. Now, let’s start
with a simple node app. And to npm in it create
an empty project. So we have a empty package dot. Json file will go ahead
and install Express. So we’ll try to create
a very simple hello world application using Express. So it will be another
application Express is a very popular web framework
created on node. So you’ve now if we Open
the package dot Json. We can see that the name
of the application and mainly the dependencies. We have Express
listed over here. Okay, we’ll create
our app dot J’s file. That will be our app. That’s right all
application over here. First we’ll import
the express module. We’ll use that object of the express module
to create our app object will use the app object
to listen for this is where we actually starting
our HTTP server and yet as to listen
on a particular port. You can use any port
number over here. Mm, the port numbers
that are commonly used are three thousand eighty eighty or
eighty eight eight four times. Basically any number any port
that is open on your system. And finally we’ll just
create one route where the app would be giving
a responsive that route. Is it in the browser? We’ll just send. Hello from darker. Okay, I think that’s all we need is a simple
demo will try and run this. Okay, I’m listening
on 3,000 letters will open this. Okay. Hello from Dhaka. We have it great. So I’ll close this now now let’s
talk her eyes this application. So as you remember
the three basics of Docker dockerfile
Docker image Docker container. We need a Docker file over here. Just right dockerfile. No need to give any extension. Okay still a text file
will open this. This is where we will tell doc
of what to do before moving on with the commands. This is the docker
Hub website Hub dr.com. I suggest you please create
account on this website, or I guess when you go to install when you
go to download the docker, I think it will tell you
to create account or Docker ID. So once you have an account
on half.com you go to explore and this is where you see all
the popular images of talk. Are so anytime you’re working
with any language platform. For example, let’s say notice
in our case or PHP or dotnet or using a database is let’s say
like over here post career or couchbase all
these standard Technologies. They have their own official
Docker images already on the docker Hub. You don’t need to create images
for these from scratch because these are
already available to use. So in our case right now, we need to use the node
on the docker. So you just search for know. Okay, and as you can see we have
node is an official image of official image means
this particular Docker image is created by the people who create node. Okay, like Mongo Express. So this Docker image
is created by the people who actually create
the Mongo Express Library. Okay, and then obviously
there are verified Publishers, like for example, Microsoft and anybody
can upload a Docker image over here in the hub. Okay, and you can also So
filter them by the categories the kind of image that you want the supported OS
supported architectures, etc. Etc. So for us all we need is
this node Docker image. So this is the image upon which
our image will be built and the container
would be working. Okay, so coming down
how to use this image. Okay. So you need to go
to this guide will open the Jitter page of node. Okay. So over here create
a Docker file in your node.js app project. The node base image
with your desired version. This will be the first line
of our dockerfile. So this is the official
Docker image that we want to use and this is the version of that Docker image
not the version of node. You can have a look at all the supported
Docker image versions of node. I think 10 is good for us. I think it is the latest one. We don’t actually
need to go and dig into which version supports what but this is good
for us for now. Okay moving forward now,
what do we want? Want to tell Docker to do. First we’ll create
a working directory for the docket for this is where we are telling
Docker to like create a app directory for itself
where whatever work or things. The doctor needs to do for our application doctor can
put it inside the app folder. Will tell talker to copy
our package dot Json and if you remember dockerfile is
inside our app folder. Okay. So this is inside
our app folder. So we don’t need to give
any paths for any files that you are referencing so I can directly say copy
package dot Json inside the app. Okay run npm install. So this is like very easily
understandable commands copy package dot Json
inside the app folder and then run the npm install. So this command will be run. Inside this folder
of the docker container and what npm install would do
is whatever packages are listed inside package dot Json if you remember we have Express so those packages
will be installed. Okay, then we tell Docker to now dot over here means
the current directory the file is in so this
is our current directory and metering Docker
to copy everything from the current directory
to the app folder and then run the command node. So this is the commands
which even we used to run the node app. Okay. So this is what Taco would run
inside its container and lastly if you remember
we had a port number which we were using for us. It’s 3,000. So we need to expose
a Number as well, okay. Yeah, so I think this is
it we are done now. We’ll try and run
this inside talker. Okay. So first we need to build
the docker image. We only have a dark
of file right now. So next step is
to build a Docker image from the dockerfile. We need to give a name
to the docker image. I’ll name it. Hello Docker. Okay, because the mistake that I did is to put
a dot in the end. So just taking the current
directory we are in okay. So as you can see, these are the commands
that is running one by one. It’s going through all
the steps from no time. What dir copy package.json
run npm install and this over here, it might take a while because it needs some time
to download the packages listed in package.json. Okay, and then it went
through everything fine. Okay. Now we have
the image created now, we need to run the docker app. So the Almond
for that is Docker run. Also again, we do have
the docker documentation online, which you can refer any time. Okay, so any language
any platform technology, you learn always try and refer
to the official documentation, if you can like refer
from it study from it. That is the best always
another thing even in the terminal in the console. You know, you can always look at help it will list
out all the options that you can give
to the command. So these are the options
for the Run command not the docker command
the Run command. Okay all the options for the the docker command
would be darker help. Okay. So these are all
the sub commands and options that you can use with Docker. So moving on we’re going
to run the image occur run now, it means we want the docker
to run in an interactive shell. So if you look at the options, that’s not required,
but it’s good. Okay, so – i – I interactive keeps
standard input open even if not – And T allocate a pseudo
TTY TTY means or terminal so Docker run. And we want to tell Docker which Port we exposing
and which Port we are using inside the application. So again, if you look at help, there is this P option
publish lists publish a container sport to the host. So we’ll be creating
a Docker container, which is running
on the host host is over Windows operating system. We need to tell which Port the docker
is exposing and which Port is being used by the app
inside this port 3000 over here and this 3000 over. And with different okay,
it’s not compulsory that there should be
the same all the time. We can have 8,000 over
here 3000 over here. And all we need to do is
map it in the command. But over here is the same
so Docker run. So the it what basically
would do is I’ll show you when the command runs. So this first 3000 is
the port number of we listed in the dockerfile the port
exposed by doctor. And the second one is
what our app is using. Okay now our image
name was hello. Soccer let’s run this. Okay. We have some error it says
it’s already allocated. Okay, we’ll try another put
this may be due to because I was already
doing some tests and running this now once I
have changed the dockerfile, I need to rebuild my image. So we’ll run
the build command again. Okay. Now I will run the image
Docker run interactive 80 teeth and mm inside the application. Hello talker. And we have running now. Let’s try and run again. Okay side can’t be reached. Now. Remember we have
this talker toolbox thing open and it has its own machine
IP 192.168.1.1 hundred 192.168 attend and
hundred port number 3000. Okay, so I’m sorry, but I had another instance
running already on that Port that is why it did not allow
me to run that Docker image on that particular Port I
changed my dockerfile port to double it double it and that is the one that is being exposed. So for us over here
the port number should be double a double eight. And that is
where I see the message. Hello from Docker, which is what actually
we have used. Hello from Docker. The reason why
3000 is working is because there’s always
The another doctor instance running in the background, which I hadn’t closed
and it says hello world. So there’s actually
another Docker container already running on that port and that is why it did not allow
me to run the docker container on this port. So as you can see I
already had one running which I forgot about
sorry about that. So our app is running right now
on the port double it double it and that is the port exposed by
talker to my operating system, which is Windows, but the port on which
the app is running inside the docker
container is 3,000. So the app is still running
on three thousand but Docker is exposing
our app to port ww8. And that is what we
have mapped over here and – – IIT means it’s interactive
right now this message what you’re seeing is
actually from a console inside the docker container. This is not from our own CMD
or command line. Okay. So now I can press
control C to end this Okay. Yeah, so as you can see as I did okay, so I was able to give
it the command to end it and that came and went
to the console of the container. So I hope you enjoyed
it the small demo. Okay. And yeah, this is what we actually did basically
create the know Jesus. I have created dockerfile build
the image and then execute it. How to build a means
that application from scratch so before moving ahead let
us discuss the agenda for today’s session so we’ll start with what is the
main application then we’ll talk about mongodb Express angular
2 and node dot J’s basically mean is an acronym for Mongo DB Express angular
2 and no dot J’s. So we were talking about all
these four Technologies. They will talk
about crude operation and at last we’ll start
building our application which is contact list me nap. So let’s start with very
first topic of today’s session which is what is
the main application? Mean stack refers
to a collection of JavaScript based Technologies
used to develop web application. So from client to server
to database everything will be based on JavaScript. If you see the diagram which shows a simple
client server architecture that will be following
in our main application. If you see there you will find
angular 2 that we’ll be using for our client site development. Then we will using Express which is based on no dot DS
for server-side development and at last we’ll be using
mongodb as our database. So this is the architecture
that will be following. So let us talk about this for Technologies
starting with mongodb. Mongodb is an open source schema
list nosql database system. So if you are working
on Mongo DB you will be working on Collections and documents. A collection is
a group of documents. Whereas document is a set
of key value pairs. You can refer to a collection
as that of a table in our dbms system. So basically all the data that will be working
on inside mongodb will be in the form of key
and values mongodb says data in binary Json format, which makes it easier to pass
data between client and server. So next on our list is Express. So Express in the lightweight
framework used to build web application based
on node dot JS. It provides you
a setup robust features, which can use to build
single page application as well as multi page application. The idea of Express
came from Sinatra, which is a very popular
framework based on Ruby. So basically Express provides
you boilerplate by encapsulating the functionalities
of node or chairs. So that the whole app
building process become easier and fast now, let us talk about angular 2 using angular 2 you
can make application for your mobile as well as
your desktop angular 2 is by far the most popular JavaScript
framework available today. And with the introduction of
typescript angle 2 is bringing a true object-oriented
web development into mainstream. So we will be using
our interface through which our client will be interacting
using angular 2. So at last let us talk
about no dot JS. So no dot JS is a server-side
JavaScript execution environment built on Google Chrome’s
V8 JavaScript engine. It follows an
event-driven architecture where there is a single thread
mechanism to process your events and even can be clicking
a button or filling a form in our website. So all these events
are then sent back to your server for processing. And then the server after the processing
is done generate the response the processing
of these events are done. Asynchronously that is if we have multiple events
in our event queue our node Server doesn’t wait for a particular
event to complete so it can process or serve multiple requests
at the same time. So these very whole
event-driven architecture makes our node.js server
highly optimized and scalable. So now let us talk
about credit operation. The bread is an acronym that stands for create
read update and delete. So basically these four
operations will be performed on our database. So we will be using post method for inserting new data into
a database will be using get for retrieving data
from a database will be using put for modifying
or updating any data that is there in our database and at last we’ll be
using delete method for deleting resources
or deleting any data that is there in our database. So we will be implementing all These for functionalities are
operations using restful apis that will be learning while building our
whole application. So guys let us start with building our
application before that. We need to install manga TV, and no dot JS locally
into our system. So let us go ahead and do that. So for installing know dot JS
you need to visit. This website called
node.js dot o– r– g– go to the download section then according to your
operating system choose the correct installer
and download it. So I’m going to go
ahead with Windows. So basically installing
node.js or longer. Even your system is pretty easy. You just have
to go through setup. Click a bunch of next button
and specify your location where you want to install
it or go with the default and that’s it. So my download is complete. I’m going to installer. I have already downloaded
and install no dot JS in my system. So I’m not gonna install it but I’m gonna show
you the procedure or way to do it. It’s pretty easy. And then we’re gonna
stall Mongo DB. I think I have also installed
mongodb in my system, but I’ll tell you
how to do that. So specify your location here then click on next next
and then finally install. So once your installation
is completed click the Finish button and open
the node dot JS command prompt. So basically we will be using
this command prompt node.js command prompt
to build our entire project. So let’s check out
the node version. Then you can go ahead
and check out the npm version. Npm stands for
node package manager, which is there for
managing your dependencies and their versions. So whatever dependencies you
need to run your application or for your project, even I use NTM to manage that now let us install
mongodb into our system. Do you go to mongodb.com? You click on download then again as per your operating system
choose the correct installer. I’m going to go ahead
with this very version and I’m going
to download the MSI. It’s a big file 1 48 MB. So I have already downloaded it. So I’m not going to go ahead
with the download. You have to run this setup. Click on next I have Mama TV
already installed in my system. So I’m going to remove it first then you can show
you the installation. I could have done
that with no dot J’s as well. But it’s pretty same like you just have
to click bunch of next have to specify your path. And that’s it installed it
then gone next agree next. I want complete install you
can With the custom options as well where you
can specify the location and other configurations, but I’m going to go ahead
with the default mode or the complete installation and the mongodb files
will be installed in my program files if you’re using Windows and you
can go ahead and check out that so I’m going to show you that once the installation
has been completed. Yeah, so click on finish your father will be installed by
default in your program files. If you’re using Windows go
to Mongo DB server 3.4 then so this is where you’re all the files
reside in your system. No for running
your mongodb service, for example, whenever you
are creating a mean app, you need to run
the mongodb service so that you can make
connection to it and perform data operations. So for that what you can do is go
to dogs in mongodb.com because you will get
the whole procedure or step by step process to do that. Look mama DB server installation Maori
Community Edition windows. We have already
computed this very step that is install
mongodb Community Edition. Now, we need to run
this mongodb service. So for that we need to create
two directories called as data and one subdirectory steamy. So let’s go ahead and do
that will open command prompt. Let me delete the data file because I had a mongodb
already installed in my system. I’m gonna delete it. So yeah, DB has been created now the next step
is to tell mongodb that this is the part
where my database data or data for the database
will be deciding. I’m going to copy this command
and I’m gonna paste it here. I’m going to change my path
which is see And / Vita, that’s it. So my connection
has been started. My service has been started
and sweating poor connections on port 27 0 17, which is the default port
for mongodb to connect if in case like if you go ahead
with this very step and it doesn’t work out and you have to execute
the Mongo d dot exe, which is the third step. You can do that as well. So we have mongodb ready
and no dot J’s ready. So now we need to
create our project. So I’m going to go ahead
and start building my application for that. I’m going to go
to my project directory which is in 3 Drive. Yeah, so I’m going
to make a folder where I’ll be putting
all my source codes. Server side as well
as my client side. So let’s name it as the name
of our application which is contact list. I’m going to use npm and it command to build
my project as well as create package dot Json thyroid which will contain
all my dependencies and their version. So you go ahead and flip npm and it will ask you
your project name. version description
of the project that you’re building simple
mean application entry point is the file from with where your server side
exhibition will start. So let’s name it as AB dot JS. You can go ahead
with index dot J’s as well. I always use app dot j. So it’s up to you to name. Whatever you want
to name that file. You can do that. You can put your name as author. I’m going to put mine right now. So let’s open
this folder project. Okay. I have already opened
in my abs core Sam gonna use vs code as an editor. You can use any editor
of your choice. So this is your
package dot Json file which contains all the
information regarding projects. We haven’t installed
any dependencies yet. That’s why you’re
not seeing any dependencies or anything like that. We’re going to go
ahead and do that. So for that I’m going to use
the command + BF installed. So for installing any dependency or module you use
npm install command followed by the name
of that particular module, which is Express. Then you can use – – say flag in that way, whatever
dependencies or modules. You’re installing. It will be written
to your packages on fire. So I haven’t told my Express and you can see over here
in the dependencies section. We have Express
of version 4.1 5.2 and oh, so this is how you do things. So now if you want
to share your project with your friend or colleague, so you don’t need to send
the entire project along with the dependencies. So you just need to send
them the source code and this package dot Json file so that they Download
the dependencies or installed a dependencies and start
working with your project. You can also install
multiple dependencies or modules or at the same time. What I mean to say is using
a single command install go ahead with Mongoose and then we need course
we need body parts. So Mongoose is
an object document mapper for using mongodb and body
parts are used for parsing. The incoming Json data
and course is power because we will be having
our server code running at Port 3000. Whereas our client side code
run on 4,200 4,200. So that’s why we need
to enable course so that we don’t get any error. Which is not allowed by default. As I told earlier we have our entry point file
called AB dot J’s from where our server side
execution is start. So we’ll start server side code
by making that verify locating that verify the first place. So we’ll go ahead and do that. AB dot DL then what we will do we will import
the modules or dependencies that we need to build our project or our server
side code so importing. models So first we need Express. We need mongoose. We need body parcel. with it calls And we need part. We haven’t installed part because it’s one
of the code module. So you don’t need
to install it separately. Now now for making
a express application now using Express we need
to use express method and assign it to some variable. So we’ll assign it to app. Okay. Now we need to define
a port number. Now we need to bind our server
with this port so. These are callback you can go in
and I’m using Arrow function. You can use function as well
the JavaScript function. After a successful connection, we need we want to say
that server started at home. Concatenate the port number. Now what we need to do is
we need to add our out so that if we go to localhost 3000
then way to specify our server what it needs to render
for that home page route. Did I testing server because we’re gonna hit
on create all our doubts in a separate file
called route dot J’s or something like that so that all our hours are in particular file and it’s
not in our AB dot JS file. For creating route you
need to use get method. For example, like this is
how I am defining a route and the route is
for my homepage. That’s why I’m using / the next argument
is request response. Stockton now, let’s go
ahead and run our server. For that you need
to execute note space your entry point file name. So our server has been started
go ahead and check that. So we have a full bar. So everything is
running fine now. So remember I told you that I want all my routes to be
in a particular folder. So I’m going to create
a folder called route. We can simply put it in a file because it’s
a simple application. So we do that or we
can create a folder as well. Get outdoor chair. Now we want to use
this very particular folder. So what I’m gonna do
is I’m gonna Define this particular file
with a variable. So first I’m going to use Rao. Bye. We need out / now
we need to use it. So. So I want all my heart which is proceeding with /
contact or / API or / ABCD to be forwarded to that
for a particular file. So for that, for example, I want
it to be / API. So all the routes with / API slash ABC let’s say
will be directed to my route dot J’s file
and where I have defined what to do with that
particular route called. For example, I’m having / API slash are contact
for adding the contact. So whenever a client
needs to add contact it will make a call to this very
API slash API slash add contact and I would be defining my Logic for adding contact
to my database over there in my route dot J’s file beside
that very particular route. That is / API slash at contact. So that’s how things work. Right? So we need to Define what? So that we can make call to it and we can perform
our data operation. So for now this is the reason
that we are making the roads. So it’s done now we need
to add our middleware so that we can parse our data
and we can use korres. So let’s go ahead and do that. So we need cars. We need body powder as well. Do you want to parse
the Json data? sad and we need
to do one more thing. We want all our static files
to be in a particular folder. So for that I’m going
to create a folder called. Public and inside that I’m gonna create
my file index dot HTML, which will be rendered by
our front-end application. so static files join Basically,
I’m defining the path here. You can directly go and specify the path
of this folder public folder or can use underscore
underscore dir name which will point
to your current directory and you want to join
it with public. So guys we are done here now. Let’s create some route. So we need to bring Express. You can use cost
or wear whatever you want. But in general we use cost because the value of Express
is not going to change. I mean the value of variables Express is not going
to change in our code. Same with router we going
to use express routers. Am I told you that we’ll
be adding routes here. So whatever call
I’m making two / API, which is there. Let me show you /
API slash contacts. It will be directed to this very
particular dot get method. request response and next Now
this is my / contacts folder. So. And I’m going to use
this very route for retrieving the contact list. Okay. So every time you make changes to your server side code you
need to restart your server or what you can do. You can go ahead
and install node mode, which will continuously
watch your source code files for any changes Whenever there
is a change it will restart your server on Stone. So we don’t do that. Now you go ahead with node mon. Come on to start your server. So we are getting an error. So whenever you’re creating route, you need to export
your router as well. So basically what it does it
goes to your package.json file and where it finds the node mon that our main entry point file
is uploaded years and finally start the server
using this very particular file. So if you see over
here the same command that we use to
execute for starting a server node app dot JS
has been used by note mode. Now if I go ahead with / API slash contacts See
we can have the data. So now what we gonna do you’re
gonna connect to our database and then here we want
to write our Logic for retrieving data
from a database and that’s how we will send data to
our client-side application, which will make a call
to this very particular API and it ripped
the list of contact. We need few more methods
for example for reading data. We need a method
to add data at contact. For that we’ll be
using post method. Then we need a method
for deleting contact. pretty similar syntax or the signature
of these methods So how I am going to delete
a particular contact is by referring to that very
particular contact using its ID. So now what we need, we need to create
schema for our contact that will be inserted into our database and we need to
make connection to our database. So we’re going to make
a folder called models. inside that we need
file contacts dot chair So this very particular file
will be having all our schema or contact schema. So that is the reason
I am making this file. So we need to bring
in Mongoose for since we are going to make
schema for a mongodb. So in to bring mom goes through
which will be talking to mongodb or through that will be using
our mongodb database. contact schema Mongoose schema So this is a function
and I’m going to pass the schema that I’m going to use
for my contact. So I’m going to go ahead
with first name and last name. Drive strength but crew
then we need last name. Let’s make it required also. You can make it falls as well. But since it’s a demo, so I’m going to go
ahead with true. Last field will be
for contact number or phone number. So let’s keep it string only. Now we need to export
this very schema. So it’s done. Now what we need is we need
to create connection or way to connect
to our Mama tv. So we’re going to go
ahead and do that. So we have
Mongoose already here. So now for connection will be using Mongoose
dot connect function. So let’s go ahead and do that. connect to Mongo DB Mongoose
dot connect then we need to pass the URL along with port number to which
we’re gonna connect to our mongodb. So it is mongo DB. Local Host two seven
zero one seven, so we’re going to go ahead
with our default port. Now on successful connection we need to display
a message that yeah, the connection has
been established successfully. on connection we
need to say mongoose. connection. On connected then
we want to say that. connected to database
Mongo DB at yeah, that’s it now in case of error. Suppose we have an error
while doing the connection or while establishing
the connection. if error we need to know that what is the error
that we are having? Prints a message as well. Data is connection. Yeah, you don’t need it. So it’s done. I think now we can check out if we have made the successful
connection or not yet. So we have been connected to our database at Port
two seven zero and seven. Now, what we need
to do is create apis for retrieving contacts
are adding contact or database or deleting
contact from our database. So you’re going to do that. So first we need to bring
the schema the contact schema that we have created
in our contacts dot JS file. Now for retrieving contact we’re going to use
find method or function. Then we’re going
to pass a callback. So this is one other way
to define a callback or function the JavaScript way. Yes torches on contact. So after achieving
the contact all the contact or the contact list will be
saved in our contacts variable which we are sending or responding back
to our client in Json format. No, we need our post. So first what we need, we need to create a new contact
which will be adding to our database. So I’m going to make
an object new contact. Let’s go to new contact. It will be of type contact. name I won my first name to be
from my request body. dog first name and last name? So it will be lost. Then we need phone number. That’s it guys. Now we want to insert this very
new contact into our database. So for that we’re going
to use new contact dot safe and we need a call back. Let’s go ahead
with the arrow function. if Earth Way to send
a we want to send. message fail to add contact else contact added
successful That’s it guys. Now for delete what I’m trying
to show you is that if you are inserting any data
into your database, what are mongodb does it
creates an ID for each document or each contact? So using that for
a particular ID, we gonna refer
to that particular contract which we want to delete and
will perform art-lete operation. So that’s how we
want to do things. So from client side
will be receiving that very particular. Quest for deleting a particular
contact then using that very ID, we’re gonna issue
or delete command. So Contact. The move. Now as I said,
we need to retrieve the ID. This ID and then a call back. else that’s it guys. So our delete method
has been added to our doubt file successfully. So we have 3 methods now later on we will also
add our update method. So it’s done. We have no error or no issues now for checking
our apis we gonna use Postman, which is a Chrome extension, which you can add
to your web browser Chrome browser and go ahead
and check your apis. You want to go
with Postman crew? Here you will get an option for adding it to your browser
or to your Chrome. Now first of all, we need to insert some data
so that we can fetch it. So, you know what with our post method
then we want our header as content type to be Json Then
we need to send draw later. So. first name Bruce last name
well then phone number. Okay, I’m going to go
ahead with string. But yeah, let’s add
some random number here. Now. We have to enter the URL
or the URL of the EPA that you want to make the call to so that it can be directed
to that particular post method. So we’re going to go ahead
with Local Host 3000 / API slash contact
so contact added successfully. Let’s add some
more contact Peter. Now let us retrieve the contacts that we have already added
into our database. For that we’ll be using
API slash contact. So we have these two contact. So that’s what I
was talking about. You can see over here. We haven’t added any ID field
while inserting our contact. So this is automatically
generated by mongodb. And this is what
we are referring to in our delete method. So what we’re going to do is
copy this very particular ID. No, we’re gonna issue
a delete command. So now our method
will fetch this very particular ID from our URL and it will refer
to that very particular document for that very particular
contact and deleted so the contact has been deleted. Now let’s check that. We are having issues here
so we can see that our Peter Parker
has been deleted. So this is how you
can check your apis. Now what we’re going to do. We’re going to make
our client application and where we will be making call to our apis for performing
various kind of data operations that we have to find
in our server file or server side code. So that’s it guys are
server-side code is complete. Now. We’re going to go ahead
with a client-side application using angular 2 and
we’ll be creating view templates and all to show
a contact list will add buttons for delete for adding contact and all that. Let’s go ahead and do that. So for that I will
be using angular CLI or command line interface that will be using to build
our entire client side project. So go ahead and open
a new new instance of node dot JS CMD then go
to the project directory. So now we need to install
angular CLI for that you’ll be using again
npm install a dread angular. / CLA So basically angular CLI
will help you to provide all the boilerplate
or all the files which is necessary
to run your client site and your application. It will also provide you
the code node modules or core modules, which we are going to use
in our angular app. Yeah, so npm
install has completed and our angular CLI
has been stalled. Now. We’re gonna go ahead
with creating a angular project. So for that you will use NG new
and the folder name where your angular app will
be deciding so ingenue client. I’m gonna name this kind because
it’s a client-side application. So as I said, it will provide you boilerplate
or all the files. So these are the files that will be created using
your ingenue command. So you’ll have a basic component
called as AB dot component and you will have
a basic model app dot module where you will be specifying
all the modules which are going to use
across the application as well as your components and services. Then we’ll have index dot HTML which is the file
that will be rendered in the first place
to your web browser. So which we have specified in our public folder then
we have packages own. There are other files as
well as you see over here. We have protractor for testing and for linting
we have t es Lindo Json and although configuration for our angular CLI
is in angular – CLI dot Json so
couple others are there so which is not required for now, like don’t need
to know all of that. So our project has been
created successfully now, let’s go inside that very
particular folder. No what we need we need
to create components. Okay, let me just give
you a quick walkthrough. So inside our client folder,
we have Source then app. These are component
our basic component called as AB dot component. So if you look
at index dot HTML file, which I told you it’s going
to render in the first place. So here in body. We have app – Ruth. So this is a selector or tag which will load
our app dot component for their so basically
We’ll have AB dot component or app component class to be
rendered in the first place. So here you can see
we have AB – route as selector. So for building
an angular 2 application. We use typescript as language, which is based
on object-oriented features. So here you can see that it’s a typescript class
called as app component. Now, we have something
called as decorator at the red component decorator
which tells angular that this very class
is Component and we have template URL over here which specify the template
for our component which is over here. And then we have
CSS for styling. So let’s run this very
particular application. Npm start is the command
or you can go ahead with NG serve. now you go ahead
and 4200 we are having that for a particular
thing as app works. So basically first our index
dot HTML file is rendered which is calling are approved
or app component. So if you check out
the app dot component template to in this template, we have what we
call as interpolation or one-way data binding so we are winding this value
as app works so left. Let’s make it welcome
to Ed Waiter. It has been changed. So like for in case
of server-side applications while we were creating
the express app, we had to run server every time
whenever there is a change and for that we use
node more similarly in our angular side application. There’s a difference is that we don’t need
to restart our application or it continuously
watches for any changes that we make in our source code and it re-enters or restart
the whole application each time so you can I
didn’t check that. So if you see over here,
then we had a change over here. So it recompile it
and start the whole application. All these TS files are converted
to JavaScript files or transfer into Javascript file. And then our render now
we need a basic component called as a contact for listing our contact
as well as there will be a form of over there which will have three for adding
contact then we will have button for deleting contact. So let’s go ahead
and do all of these. So first of all,
we need to create a component. So I’m going to stop it for now. We’ll be using NG G or NG generate for generating
component or services. Name of our company will be
contact we need a service to so basically it’s considered
to be a good practice to separate your business logic or your data retrieving
audit operation logic in a separate file, which we call as a service then what we do is
we inject those Services into a component over
where it is necessary. So as to use those services Now
you can see a warning here that service is generated
but not provided. So basically whenever
you have to use or in whichever component, you have to use the service
you have to provide it in your at the rate component accurate. So there will be a field
here called as providers and we will add the service
that we will be dating now. So we have contact
or service dot DS or contact service class where we’ll be writing
our business logic or our data retrieving
or write operation. Logic, and we’ll have
contacts component where we’ll be creating
our template and all the logic behind that so we’ll start
with creating our service so that we can retrieve
the data first then we’ll start with creating
these contacts dot component. Now one more thing we
need is we need a schema for our contact. So let us go ahead
and make a file call as contact or chairs or Ts that will transfer into jai’s
so you do that contact dot DS? No. We’ll have a class called. contact which will have
four Fields ID. We have tried string. Then we need first name. print last name String
and phone optional field now. We need to import our HTTP model
for all getting all HTTP methods for retrieving data or for sending data or in
other words contacts. so import http We
need headers as well. from angular http then we need our contact class
to be imported here so that we can use
the schema boat. contact from Then we need
reactive jest operators basically will be
using nap operator. So it’s done now. We need to use our HTTP
module so for that. Thunder now, let’s create the methods
for retrieving contacts. Then we will add contacts
and we will delete contacts so we will have
grieving contact. So which is get contacts. And I want to return. All the contact
call the contacts or put the contact list, which I will be getting through
while I’m calling to my API. Then the API URL you
can see over here. This is the signature or syntax
of your get method or signature. You can call like we will have first argument
as a URL string so we’ll have as HTTP / localhost. / 3040 Ember then
we’ll have a pi then we have contacts then
you want to map it to? Json format So we have our
get contact method radio here. Now we need add contact method. relax and contact which will be taking
new contact as an argument. We wanna up and the content type
in our header, which is So now
I want to return. the response and I want to make
a call to my add contact API, which is / API slash contact. I’m going to copy
this very same thing. and then I’m going
to pass the new contact. Then the headers. Will map it response as Json. how either delete
method Tim I told you that for deleting a contact will
be using ID so as to our effort that’s very particular contact
in our database, so we need ideas are parameter. Then I’m going to append
or concatenate the ID. So we have
our service ready now, we will be using
all of this method or injecting these Services
into our component and then we want to use all
of this method to retrieve data and finally do something on it. For example, if you want
to show the contact list, so we’ll do that then for adding contact leaves
this add contact method. So that’s how
we’re doing things. So let’s go ahead
and create our component. So we’ll go to our contacts Dot. Go TS. Private contact service. Yeah, but we need
to import it first. So we have contact service. from dot slash contact Yeah, so now we need
our contact class or schema. That’s it. Now. We’re going to initialize
our contact service or we want an instance
of that for you contact service class
so that we can use the method that we have defined
their so for that what we’ll do we’ll Insight will pass the argument
as private contact service of type contact service class. So this very process is called
dependency injection or that’s how we inject our services
into our component and second thing is you need
to provide your service. So as to use it. providers now let us go ahead
and retrieve data. So we’ll be quoting are retrieving data logic
inside NG on init method which will be initiated. Once your component is being
loaded into your browser. So every time you
load your component, it will automatically call this very particular method
to retrieve all the data. So we’ll be having
this dot contact service and we want to use
get contact method so you can see over here. That we have 3 methods add contact list contact
and get contacts. We gonna Escape contacts and since it will be turning
an observable in to subscribe that we want
our contacts to be save as restore contact now
it’s showing an error because I haven’t made
any variable for contact. So let’s go ahead and do that. So it will be one tax type. Contact and to be an Arab. Yeah, so we have done that. Let’s get some more variable
that will be reading it wrong. We need one then
we need first name. texting Then last name? then phone number That’s it. Remember, I told you that the bootstrap component is
AB dot component. So we need to specify
in our app component at this particular component
should be loaded so we will go to template and We’re going
to use app – contacts. Because if you go
and look at the selector, it’s app – contact
so contacts works. So guys, we have retrieved
the data here. Now, let’s get our template so that we can display
this very particular data. So I’m going to go
ahead with my contacts dot component dot HTML and So before doing
this we need to refer to our bootstrap style sheet so that we can incorporate it
into our template. So we’ll go ahead
with boots watch. then these are the size that’s available
which you can use. So I’ll go ahead
with something super hero. So you can go ahead
and click the download and copy this very thing and put
it into your HTML or index dot HTML file. So we’ll go there. I will copy this very thing. And they sit here and then we need
to write stylesheet. and we’ll remove
this very particular field and we’ll copy it
here in our Hof so that it can refer
to it So it’s done. Now we can go to our component and here what we want is we want
to iterate over our list. So for that we will be using
something called as ng4 which is a structural directive. So strict and G.
electric contact on contacts so we’ll be iterating over
contacts using contact variable. So each contact in our contacts array
will be retrieved in this very particular variable and that variable will be displaying as a list
so Let’s go ahead and do that. So we need our class. md3 again, I’m closing div tag. So now again, we will be using
that very interpolation or one big data binding. to display our contact. First name then we need similar
columns for our last name and phone number Sexton
so let’s check out our yeah, so we
can see over here that our list is being generated
will put some more style so that it will look
good in the end. So what we’ll do right now, it’s will add
a delete button using that we can delete any contact. So we’ll go ahead and do that. So for that I’ll be copying
this very thing again, and I need my foot type. Be a button. Even click which will call
a method call as deal contact. – we haven’t created yet,
but we’ll do it soon. Now what we want is we will be referring to that very
particular contact using ID as I told you earlier, so Eddie so the value should be
the we’ll be using a class. Call us. button Danger So it’s done so we have this very
particular thing over here. Now, what we need is
will add few breaks so that we are iterating
over the list. So yeah, we’ll do it
later on this stylings. So now we’ll add
our lead method over here which will be using
and we also need something called as adding
contacts add method or a contact method. So let’s go ahead with our delete first
so we need contact Let’s assign ID as
any type type any now, so basically we’ll be calling
our service again. So this Dot yes. Contact service dot
delete contact and then we’ll passing the ID and since it
returns an observable. We need to subscribe
that very particular observable. So we’ll go ahead and do that. Now what we need to do is even if we have polluted the data
or the contact from our back inside or from my database. I need to remove it
from my contact at as well. This very particular contact where all the contact
has been safe. So we’ll go ahead and do that. if the delete operation
was successful then gonna iterate over
perhaps dot length then. Okay, so I’m having an error because I haven’t defined
the variable contact so contact skull to
this dot contacts sister Now we need to increment our iterator. so if contact I Dot Underscore
ID is equal to the ID that has been provided
to my method. Then I want to remove it. splice iPhone so
our delete contact is done. No, we need to add our new
method for are adding contact. So delete functionality is done. So let’s add
our add contact method so that we can add more contact
and delete them and see if everything is
working fine or not. So what that I’ll be using
the add contact and this I will be creating new contact. Are I need first name
to beat the start? First name? Then I need last name
to be this dot last name and I need phone to be. this thoughtful now I need to
provide this particular contact that I have created
to my service or in other words. I need to call the method
that is defined in our service. So for that we’ll be going ahead
with a contact service. . Add contact and will you pausing
this particular variable? We want to subscribe it. Just start contact dot push. So we are pushing
the new contact into our array. We are having here, okay. So it’s not able to find
any not finding the contact and I don’t know why we
are having this error. Okay, let’s check it out. So yeah. So it’s done. So these are
at contact method now. We need to call it
from our template. So we want to
modify our template so that we can add a form
on top of our class and which will have three Fields
one for name one for last name and then for one for phone and then I submit mutton
for adding the contact finally. So far form we need to use
form tag or submit event. And then we’ll add add contact. So this is the method
that we going to call when the form
has been submitted. Close the tag form tag now. We need three field guys. So the first one will be
of power first name. So let’s go ahead and do that. So for that will be using
the class equal to the classical to form group now we need
to close this particular tag, and I want my label
as first name Yeah, now I need input type
equal to text. Then we gonna use something
called as NG model for two-way data binding. So whatever changes that we’re going to make
there should be reflected in our list as well. Where will be showing
the contact list. So we’re gonna use that. and G model goes to name equals 2 first name glass bowl
to form So as it’s done, we have our first three four. The first name similarly. We need it for our last name
and then for our phone number, so we’ll get it as last name and then we need to write
it as last name here. Finally we need
for phone number. So phone number so you
want to put this value to our phone variable? Yeah, everything is done here. Now we can add
something goal is required. So if you are not putting that very or feeling
that very field, then it will pop up a message or they will be a message
popped up stating that you need to fill that very particular form
or particular field so we can use
this require here now. We need one button also
for our submit button for submitting our form Now on top of that we need
to add some heading stating that this is the section
for our add contacts. Oh It’s done. Now. We have everything here. All we need is a horizontal line
or too low Yeah, so basically these are
at contact form using which we can add our contact
and these are delete button everything is there let’s try
my name you’re having a tear but yeah, it’s there
and the second thing is that if you are seeing this that every time we are adding a data we need
to reload it is happening because this very method
for retrieving the contacts. So basically whenever you
are retrieving the contact and displaying it
at that particular time, like your browser never allows
you to block your UI, so that is
what is happening right now. So what we can do to fix it is we can copy this very
particular thing over here and we’re going to put it
inside our add contact. To be called just after we
have pushed the contact. So now if we go ahead and Do that then you
can see where okay, we should add some BR tag so that it looks good
or presentable over here so you don’t do that here. So this is so far now. Let’s check the
our delete button. It’s working as well. So our whole
application is working. Let me give you
a quick walkthrough or a quick recap so that we can be fresh
all the things that we did for creating this very
particular mean application. So what we did we started
with our server side coding where we had our app dot Json 3.5 where we imported
all the models that we required for application then we use middleware
for applying course. And a body parser to
parse the Json data and then we put all our routes in a separate file call
as rho dot J’s, which is over here. Now there we created routes
for each crowd operation. And then we created schema
for database for contact. Like for example, first name will be
like this last name will be this and phone this now we
have our client side or client folder. There we started
with creating our component. So these are component and then we had a service call
as contact service where we have mentioned all
the data operation logic and we have injected this way
service into the component that we need to use
or we want to use. So that’s it guys. This was the whole angular
application and express and Mongo DB welcome
to edu Rica webinar on GS interview questions and I will walk you
through all the questions which will help you to crack
the interview related to node.js so let’s
get started the very basic question the question
number one differentiate between JavaScript and node.js so we should understand. What is the difference
between JavaScript and node.js so these are
the basic differences that you can say JavaScript is
a programming language whereas node.js you can say that it’s Interpreter
and environment for JavaScript. JavaScript is mainly used
for any client side activity for a web application. Whereas node.js is used for accessing or performing
any non-blocking operations of any operating system. So basically I can say that JavaScript is
a programming language which runs in web browsers. Whereas node.js is an interpreter or running
environment for JavaScript, which holds a lot of you
can say other libraries and all JavaScript is basically you can say one standard
defining programming language. It can run on any browser with default browser
running environment. It is a very strong
language normally used for you can say
a web application or any verification or for
any specific business logic, which we need to append in the screen without
page refreshing node.js if I talk about
node.js holds a lot of other relative libraries, which we normally use in JavaScript for General
purpose programming language. It is a kind of environment or I can say it is
interpreter for JavaScript which can represent
JavaScript or run. Any JavaScript program node.js. Basically it helps
us for executing some non-blocking operations
of any operating system just like a special information
like certificate details or Hardware details. We may use node.js on the same which helps us to do it and
JavaScript normal programming will not helps us on Same if I talk about
the running engine JavaScript users Spidermonkey
JavaScript code V8 engine. Whereas no a node.js users
V8 Google Chrome engine. So let’s move to next question. Now the question arises what is node.js so
node.js we can say it’s a powerful JavaScript framework. It’s an open source
server environment. It is developed on Chrome’s
V8 JavaScript engine and just please remember
node.js is absolutely free. It compiles JavaScript directly
into the native machine code and it is used for creating
server-side web applications at the same time. It is a right pick for the data-intensive
real-time applications. So we can say that node.js is an open
source server environment, which is free. It can run on various platforms. For example Windows
Linux Unix Mac OS Etc. Node.js uses JavaScript
on the server end and it uses basically
asynchronous programming. Now the question arises that what can node.js
to so node.js basically it can generate
Dynamic page content. It can create open
read write delete and close the files on the server node js
can collect form data and at the same time node.js
can add delete modify data in your database. So next question, what are the major benefits
of using node.js what benefits you get out of node.js. So first of all, as I said that it’s an open
source server environment. It is very simple and fast
basically node js is used for asynchronous programming or you can say
asynchronous input output. It is highly scalable in nature. And again, the most
important part of node.js is that it is single
threaded it uses. V8 engine now, what is V8 engine that
the engine which is used in node.js implementation. It was originally developed
for the Chrome browser, which is written in C++. So Chrome’s V8 is used
to compile function which is written in JavaScript
into machine code and it does the job
at a very impressive speech. So there is no buffering and as I mentioned
that it’s cross platform, so it runs on various platforms
for example Windows Linux Unix. Unix Mac OS Etc now again, what is the difference
between angular and node.js again a very very
important question in terms of node.js interview that what is the difference between angular and
node.js so angular. We know that it’s an open source web application
development framework. Whereas node.js is a cross-platform runtime
environment for applications. Angularjs is completely
written in typescript whereas node.js is
written in C C++. This and JavaScript
language has angular is mainly used to build SPS. Now. What is SPSS Piazza single page? Client-side web application whereas node.js is
used for building. You can say server-side
networking applications where we have
Hardware interaction software interactions and input/output
kind of functionalities angular itself is a framework so you can say it’s
a web application framework whereas node.js has
many different Frameworks in it. For example Express dot J’s. L dot JS sales dot JS and so on angular is ideal
for creating highly active and you can say
interactive web applications which can very well
interact with the users. Where is node.js is ideal for you can see it developing
small sized projects angular is helpful in splitting an app
into MVC component. So as we know that angular is completely
a web application framework, which is based on MVC model. And what is this MVC
model view controller? There is no chase. It is helpful in generating
database queries, basically and last but not the least that angular
is suitable for developing. You can say
real-time applications because there is
a very good interactivity between the system and the user and it is helpful for creating
highly active applications. Whereas if I talk
about node.js node.js is suitable in situations where something faster
and more scalable is required. And so these are just
the basic differences. You can say between angular and node.js now the question
arises again in an interview that why node.js is single threaded you
might have heard that node.js a single-threaded
nodejs users basically node.js uses a single threaded model because it supports
asynchronous programming. It’s absolutely true that all JavaScript
executed by node.js is run in a single thread node.js
is a single-threaded language which Is it in background? You can say uses multiple threads to execute
the asynchronous code. So by asynchronous processing and application can perform
better and is more scalable under web loads. So node.js basically is
non-blocking which means that all functions or you can say the callbacks
are delegated to the event Loop and they are executed
by different threads that is handled by
node.js at runtime. So it makes use of a single
thread model approach rather than you can say
It implementation. Now the question arises
that how do node.js Works. How does it work
at the back end? So please remember first of all that node.js is
a virtual machine that uses JavaScript as
its scripting language and it runs on Viet environment. That is Google
engine V8 environment, but please remember
node is not a web server. It’s not a web server by itself. It does not do anything. It does not work
like Apache server. There is no. Config file where you pointed
to your HTML files, but if you want it
to be HTTP server, you have to write an HTTP server
by using its built-in libraries. So basically it works
on a single thread mechanism or you can say
single-threaded event Loop and a non-blocking input output which provides High rate as it can handle High number of concurrent requests
at the same time and by using HTTP module
node.js can run on any Standalone web server so you can say that Node.js is
a JavaScript runtime which is build on Chrome’s V8. JavaScript engine. So node is
a runtime or environment. Please remember it’s
not a framework. Now the next question arises
that where node.js can be used. So it has many
applications Frankly. Speaking node.js can be used
in real time web applications. It can be used in network
related applications in distributed systems and obviously in
general purpose applications, so there are multiple People
benefits of using node.js now how many types of API functions
are there in node.js. So basically there are two types
of API functions in node.js when we say as asynchronous, which is non blocking functions
and one is synchronous, which are called
The Blocking functions. So, please remember when it comes to node.js Terror
only two types of functions, which you can create whether they will be
asynchronous or synchronous now the question arises that what is the It’s between
asynchronous and non-blocking. What is the difference
between a synchronous and non-blocking? So by asynchronous functions
using these types of functions, we can make a synchronous
you can say HTTP request that do not wait
the server to respond. So basically a synchronous refer
to something you can say, which is done in parallel say
you can say it another threat. So basically these functions
continue to respond to the request for which it has already received
the server response. Because they do not wait
for the server to respond. They keep on responding basis
the request which ticket whereas the non-blocking
which are synchronous. They are used in regards
with input/output operations. So you can say the non-blocking often refers
to you can say polling that is checking whether
given condition holds or not. Now, the next question is
very very important question when it comes
to node.js projects is what is package dot Json so you can say package dot. Json is the heart
of entire node js base. First project you can say
it is the introduction of any node based project. So when I say introduction so
for my introduction, what is my name? My name is Ripple Water. What I do what is my description and so on so similarly
for any node.js based solution or application? The first introduction comes
in the package dot Json so you can say it is
basically the Manifest file which contains the metadata of the project where we defined
the properties of a package. So this is the Structure you
can see on the screen of default or a general package dot Json where it has metadata
like who is the author? What is the name description? What is a virgin we are talking
about are there any some scripts because there are
some default scripts for example start
what dependencies you have in your solution what will be
the dev dependencies? So all this information
is basically carried out package dot Json in any node
js paste project now. Next question. Is that what we understand
by even driven programming? So even driven programming is a you can see
programming approach that heavily make use of events
for triggering the functions. So node.js also
uses events heavily and it is also one
of the reasons that while node.js is pretty fast as compared to other you
can see a similar Technologies and in event-driven programming in node.js it merely
follows the publish And subscribe pattern, so generally what happens
that As soon as the node started
server it simply initiates its variables declares functions
and then simply wait for the event to occur. And basically, please remember that event driven
programming make use of basically two concepts. So there will be
an event handler which is you can say
a callback function that will be called when an event is triggered
and there will be a main Loop which listens for even triggers and calls the associated
event handler for that. So we can say that publish/subscribe battle
is used in software architecture to decouple the modules that would otherwise be dependent on each other by
using a message queue Paradigm. Now next question is that what is an event Loop
in node.js and how does it work? So event Loop is
what allows node.js to perform. You can say non-blocking
input output operation. So basically event
Loop handles all the you can say asynchronous callbacks
in an application. Ian and it also follows
the publish And subscribe button so you can see over here that there are event emitters which are emitting
the events and there are some event handlers which are handling those events
and this goes into a loop which we say as event Loop. So even Loop is
what allows node.js to perform non-blocking
input output operation despite. The fact that JavaScript
is single threaded so you can say by offloading operations to the system kernel
whenever possible So basically when node.js starts it
initializes the event Loop just event Loop gets initialized the processes
the provided input script which may you can say make some asynchronous
API calls schedule timers or call some methods and then
it begins the processing of the event Loop. The next question is that what is repl
in the context of node.js what does repl so
R stands for read? He stands for evaluate or you can say P stands
for print and L stands for Loop. So in node.js are is it reads? The user input parses it
into JavaScript data structure and then store it in the memory. So no just comes with you can say
a virtual environment which is called repl. This is also known
as you can say node. Shell. Please remember repl
also known as node shell. So repl basically stands
for read eval print Loop it is a quick Easy way to test
you can say JavaScript or node.js code
and to launch the repl which is the node shell you have
to just open the command prompt in the Windows machine
or terminal and then type node. So evil is it receives an evaluates the data structure
T. It prints the final results and L it Loops
the provided command until Ctrl + C is pressed twice. So this is repl which is also known as
node shell in node.js. The next question is
that list down the task, which should be done
asynchronously using the event Loop. What are the tasks which should be done using
asynchronous method using the event Loop in node.js so there could be
some input/output operations. There could be
some heavy computations which you can do
with asynchronous event Loop and at the same time you
can do some blocking operations with node.js next question is
that list down the steps using which control flow controls
the Should walls in node.js now, what are the various steps
that how the control flow basically controls the different
function calls in node.js. So first of all, the control flow, it controls
the request of execution. So this will be the first step that you have to control
the order of execution. The second is that you have
to gather the information which we say collect data. So the first step was to control
the request of execution or you have to control
the order of execution of the In functions, and then you have
to collect the data or gather the information the third step is to
Breaking Point simultaneousness. So you have to limit
some concurrency over there. And finally you have
to call the following stage in a program. So which means that you have to invoke
the next function or next step in different function calls. Next question. Is that what do you understand
by a test pyramid? So what is a test parameters
the you can see on the screen? These are the basic Components
of a test pyramid which our unit test
integration test and end-to-end which we say e 2E test. So what our unit test unit
test you can say they test individual units
of code in isolation. So for example, you can say individual
function unit tests. They are very fast and you
usually have a lot of them. So basically you have
multiple unit tests when I say integration test
integration test the integration between the different units. It’s for example, the interaction between
one module and its dependencies so that we say
as integration tests. Last is e 2 e test it
we means end-to-end test we test the system as a whole means complete system
will be tested from the UI to the data store
and probably back. What is the concept of an integration test
is very flexible. Please remember depending
on your application and choice of testing Paradigm. Let’s move to the next question. The next question is
that what is An error first call back in node.js very
very important question. So, please remember there is
really only two rules for defining an error first
call back the first rule or you can say
the first argument of the Callback is reserved
for an error object. You can see here that function it has
two arguments one is er R comma my instance so error first callback means that the first argument
of the Callback. This is a callback function
the first argument Mint of the Callback is reserved
for an error object. If an error occurred, if an error occurred,
it will be returned by the first ER argument. Now, the second argument
of the Callback function is reserved for any
successful response data. If no error occurred this ER R, which is the first argument
will be set to null and any successful data
will be returned in the second argument. So this is the concept
of error first. Call back in node.js now. What is the purpose
of module dot exports in node.js so you can say a module
in node.js is used to encapsulate all
the related codes into a single unit of code, which can be interpreted by
shifting all related functions into a single file. So module dot exports we
can say it as exports. So module dot
exports or exports. You can say it’s
a special object which is included in. Every J’s file in the node.js application
by default module when I say module you
can consider it as a variable that represents the current
module and exports is an object that will be exposed
as a module. So whatever you assign
to module dot exports or exports will be
exposed as a module. Next question. Is that what do you understand
by reactor pattern in node.js now what is reactor pattern
so Equally reactor pattern in node.js is a concept of non-blocking
input/output operations. So you can say it’s
an idea of non-blocking input/output operations
in node.js this pattern which is the reactant pattern
provides a Handler in case of node.js we can call
it as a callback function that is associated with
each input output operation. And as soon as the input
output request is generated it is then submitted to a demultiplexer then
this demultiplexer is the notification interface which is capable
of handling concurrency in non-blocking input
output mode reactor pattern in node.js also helps in collecting each and every
request in the form of an event so you can say this resulting in the generation
of an event queue. So reactor pattern helps
in the generation of an event queue in node.js
the next question is what is the difference
between front-end and back-end development. So from a developer standpoint
everyone thinks about what is front end. What? Back end. So front-end development means that users markup
and web languages you are using some bad language
browser language which browser understand for example, HTML CSS or JavaScript, but when we compare it
with back-end development, which means that
you use programming and scripting languages like python Ruby Pearl
Etc front-end development. It is completely
based on Ajax calls, or you can say
asynchronous programming or asynchronous request whereas back-end development
is completely based on server architecture front-end development provides
you better accessibility in terms of interacting
with the web application and back-end
development provides. You enhance security
front end development is mainly used for SEO that is search engine
optimization and back in development is used
for backup purposes. Now, what is the LTS
release of node.js and what is LTS LTS stands
for long-term support. So this is just a you
can say type of version of node.js this type of work. Of node.js it receives
all the critical bug fixes along with security updates
and performance improvements. It is supported
for at least 18 months and then it mainly focuses
on the stability and security. So if you go
to node.js official website that is node js dot o– r– g– you will see
on their home page. There are two versions which are listed
one is 80s version and one is the current version. So LTS stands for
long-term support version. Next question. Is that That list down the major security
implementation within node.js how can you implement
security within node J. So you can Implement
authentication and you can Implement error handling with a node.js now
next question arises when we talk about node.js that what is callback hell what we understand
by callback hell so callback hell is also known
as the parameter of Doom it is a phenomenon called back. Hell is a pattern or you can say it’s a That
afflicts a JavaScript developer when he tries to execute multiple asynchronous operations
one after the other so you can say it is a pattern which is caused by
intensively nested callbacks, which are unreadable
it typically contains multiple nested functions
or nested callback functions, which in turn make the code
very hard to read and debug as I mentioned that some people call
it to be as a parameter of Doom also next. Ocean is that what is live UV
so leave you Visa you can say multi-platform support Library. It’s a library for node.js
which is majorly used for asynchronous
input output operation. So leave you these are
fantastic asynchronous input output Library. It has very you can say
hi efficient event Loop so you can see it as
a full featured event loop back with it. It also has separate solution for input/output you
can say In operations and it could has
internal worker thread pool for Io blocking operation. So it has very very good or great performance
through combining. Both asynchronous event Loop and thread pool
for non input/output blocking and input/output
blocking operations. It has in it some file system
events asynchronous TCP and UDP sockets and it has
some child processes. Also you can say
that this This type of Library. It’s a very good choice
for high performance server. Now. This is again a very
interesting question that what is the concept
of middleware in node.js. What is a middleware so
middleware is a function which receives the request
and response objects. What are the commonly
performed tasks of a middleware? So you can say
middleware functions. These are the functions that have access
to the request object that have access
to the response object as well. And they’re also access
to the next middleware function in the application
request response cycle. So basically what are
the various commonly perform tasks of a middleware that they execute
any type of code, they can update or modify the request as well
as the response object very very important then take an excess
the request object as well. They can access
the response object as well. And finally the help in finishing the
request response cycle the next middleware function in any Request response cycle it is commonly denoted
by a variable named next so basically middleware
functions can perform various tasks like we talked
about they can execute any code. They can make changes
to the request and response objects. They can end
the request response cycle and they can even call
the next middleware in the stack or in
the request response cycle. Now, the next question is that what is the concept
of URL module? What is a you? Module in node.js so URL module of node.js provides
various utilities for url, url resolution and parsing
so you can say that URL module splits
up a web address into readable parts. So it’s a built-in module that helps in splitting
up the web address into a readable format to include the URL module
in node based projects. We use the require method so you can see here very Where
you are L equal to require URL. So this is a built-in module. You can say of node.js now
what we understand by es length. What is this? Yes link so you can say
it’s an open source project which is initially developed
by Nicolaus in 2013 it aims to provide a lilting utility
for JavaScript through a plug and linters in node.js
you can say are good tools for searching certain classes or you can say bug classes, especially which are related
to the Scope of a particular variable now again, there is a question that why node.js users
Google V8 engine. So Google uses V8 as
its Chrome runtime engine that converts JavaScript code
into native machine code. It’s very fast. It makes your application
very very fast and it is speeds up
the application execution and response process and it makes the application
very fast running. That’s why node js
uses Google uses. V8 engine now, we will see the working
of control flow function. So we talked about
the control flow that what does control flow to so we talked
about that control flow. It controls the request
of execution it gathers the information it helps
in breakpoint simultaneousness. And then it called
the following stage in a program control
flow function is basically you can say the code that is executed between the various
asynchronous function calls. So first of all the order
Execution must be controlled. The required data
needs to be collected and then the concurrency
must be limited and next step of the program must be invoked. So we talked about that how control flow
helps in node.js. So we list down the what are
the various steps it controls the request of execution. This is the main function
of a control flow in the Second Step It
gathers the information. So basically it gathers
the required data and is it third step it
maintains the concurrency. So which means breaking
the point simultaneousness and finally it called
the following stage or the next stage in a program
or you can say next step which needs to be invoked
in any node.js application. Next question. Is that what are
the two argument that a sink dot Q takes us input
a very very important question. So two arguments that a sink dot Q takes as
input as the task function. And the concurrency value now, what is the difference
between span and Fork methods in node.js the span is used
to launch a new process. If you want to launch
a new process in node.js you will use a spine method with
the provided set of commands. Whereas for it’s a you can say
a special type of spine method that executes a new stints of the Chrome Google
Chrome V8 engine. There is a very basic difference
between the span and Fork you can ants
a fork is a subset method or subset function
of span method now, what are the global objects in node.js so Global’s
are the objects which are Global in nature. They are not local to the scope. There is scope is not local and they are available
in all the modules of the application. So if you have multiple modules and you want those objects
to be available in all the modules
of your application, you have to declare
those objects AS Global and then you can use
these Your application rather than having
to include them explicitly. So if you want to include them
explicitly then you don’t have to declare them as Global but the best way
to declare any object and which you want to use it in all the modules
of your application. It’s better to declare
them as Global. So the global objects,
they can be more duels. They can be functions. They can be any strings
or object at the same time. Some of these objects can be
in the module scope. Head of the global
scope as well. Now. What is the concept of stub
in node.js steps are nothing but the functions stubs
you can say are the small small programs. They are basically
the small programs or functions that are used for
stimulating the module or component behavior during any test cases
steps provide the answers for the functions. So these are small programs which are used
basically in testing. Now, how is it works in node.js again
a very important Concussion how as artworks in node.js. So is it is mainly
used to write test. It only provides feedback only when any of the running
test cases fails, so whenever you do testing where we talk about
the Pyramid of the testing which includes the unit test
the integration test and the end-to-end tests. So assert is mainly
used to write the test and it provides the feedback
whenever any test case fails and this module gives
you a set of exertion. Tests, which are then used
for testing invariant. So for example, if any of your test fails
and then you want to test again, so this module will give you
some assertions some you can say in technical terms. You can see some test cases which are then used
for the further testing. It is basically used
internally by node.js but by using the required module
we can again use it in our code by require a zerg. It can be used
in another application. As well now, we’ll talk about the concept
of test pyramid and we will see the process to implement them
in terms of HTTP apis. So what is the test parameter s
parameter is basically a concept that is developed by Mike
on the concept says that you should have a higher
number of low-level unit tests as compared to high
level end-to-end test that running through a GUI, which means that whenever
we are doing testing of any node project. You should have more
number of unit tests because unit tests
will be specific to small small modules. So high high level testing
or you can say end-to-end. Testing test cases could be
a little bit small, but you should have
a higher number of low level unit test cases
in terms of HTTP API. It can be defined as test parameter can be defined
as a higher number of low-level unit tests for
each model lesser integration. To test model interactions
and lesser acceptance test for testing the actual
HTTP and point. So the core or heart
of this test parameters says that you should always
have a higher number of low level unit test going
at a very low level of the testing for each model. Now, what is the purpose
of expressjs package expressed or JS is a framework which is built
on top of node.js. That facilitates you
can say the management of the flow of data between server and routes
in the server side applications. It is a lightweight
and a flexible framework that provides a wide range
of features for the publication. So just like we are
sales dot JS partial dot J’s. We have another framework
in node.js which is called expressed or chase which helps in the data flow
between the server and routes into the server
side application here. You can see it is kind
of a middle way. And it is developed on the middleware module
of node.js called connect. If you are working
with any of the connect based middleware modules tell
you can easily integrate it with Express dot JS. Next question is
that differentiate between process dot next tick
and set immediate methods? So just like in the previous
days of JavaScript programming. We used to have a method for the timers Etc called
settimeout just like that in node.js we have
two methods called. Process dot next stick and set
immediate the major difference between set immediate
and process dot next take is that set immediate you
can see Q’s its call back on the event Loop while process dot
next egg does not cues. So both are functions
of the timer’s volume which helps in executing the
code after a predefined period but when it comes
to the difference the process dot next tick as I said that it does not wait. The execution of the action till you can say the next pass
around in the event Loop, whereas the set
immediate you can say that it waits for the execution of action till the next pass
around in the event Loop. So there is a very
thin layer of difference between these two methods but both are these functions are of the timer module
of node.js next question is that what is the use
of a buffer class in node.js buffer class? As the name suggests that it is used for restoring
the raw data just like we have area of integers. For example, it corresponds
to a raw memory location that is located
outside the V8 Heap because V8 engine
is the main engine of node.js buffer class. It’s a global class that is easily accessible across all the modules
in the application without importing it
at each module level. It is used because Pure JavaScript is
not compatible with binary data. That’s why before class is used in node.js now the next very
interesting question is that how does node.js
handle the child threats, please remember node.js is
a single-threaded application. We have already talked about that node.js is
a single-threaded process and does not expose the child threads or
thread management methods tell how does node.js handle
the child threads. This is a question so you can still make Use
of the child threads using spawn method for some specific
asynchronous input output task. If you still want to use
a Threading Concept in your application, you have to include a module
called child process explicitly in your node.js application. Now what is streams
in node.js streams in node.js you can say
at the collection of data similar to arrays and string just like
in JavaScript arrays and string streams are having
the same concept there. Objects using which you
can read the data from a source or write data to a destination
in a continuous manner. So extremes are especially
useful for reading or you can say writing operations or you can say processing
a large set of data. So there are basically
four fundamental types of streams in node.js first is readable writable
duplex and transform. So basically streams
in node.js they are the objects through which you can read
the data from a source or you can write the data
to a destination in a continuous Manner and you can use
these four types of streams which is visible on your screen
readable writable duplex or transform again, very interesting question. What is the use
of node underscore EnV? So there are
some environment variables which are often used
for distinguishing between environments like for example production
environment or development. Vitamin, so if the project is in the production stage node.js
promotes the convention of making use of node underscore
ENB variable to flag it. What is the use of it? This will helps
in taking better judgment during the development
of the projects. So basically depending
on these kind of variables and application may turn
debugging on or off connect to a specific database or listen
on a specific port in node.js. There is a convention To use
a variable called node underscore env2 set
the current mode of the project whether it is
in production stage or it’s in the development stage setting your node
underscore env2 production. It will make your application
very very fast. Now, what is the difference
between read file and create read stream in node.js read file is
a fully buffered process. Whereas create read stream is a partially performed
process read file as a name suggest. Returns the response only when the complete file
is pushed into the buffer and it is completely
red in the buffer. Whereas the create
read stream method. The entire file is split
into chunks of data which are then processed and then they are sent
as a response one by one because that is why it is called partially buffered
process read file method. It’s a Memory intensive process. Whereas create read stream
is a memory effective process now various timing features of
Node.js settimeout clear timeout which we also used
in the JavaScript. This is again, you can use in node.js
setinterval clearinterval again. We used it in JavaScript the all
these methods can be used in node.js as well. Then we talked about
process dot next tick and the set immediate method
and we talked about that. What is the major difference between process dot
next it and set immediate which is also clear immediate. Please remember the major
difference between Set immediate and process
dot next tick method is that that set immediate it cues? It’s call back. It maintained its call back
on the event Loop, whereas process taught next
take it does not maintain any Q for the Callback functions. Now, what is the concept of
punycode in node.js punycode is an you can say encoding syntax that is used for converting
utf-8 string of character into basic ASCII
a sky string of correct. Stir it is very important as the hosting can only
understand the ASCII characters from node.js version, which is zero point
six point two, it was bundle up
with the default node package. So how we write it that punycode equal
to require po decode. What is the difference
between node J X and ejects? So please remember node.js is
a server-side technology. Whereas a Jax is
a client-side technology node js is required to develop
the server software that Typically executed by the server instead
of the web browsers. So node.js has nothing to do
with the implementation of the web browser. It’s a server-side technology
server-side framework, which always works
at the back end which are executed
by the server’s whereas a Jax is
a client-side technology and it is required for updating or modifying the
web page contents without having to refresh it. So when I say web page contents, these are contents which is visible to the user
and user on the web browser and we want to update or modify them without refreshing
the complete web browser. That’s why Ajax calls are used
as compared to node.js which is a server
based technology. Now another very
interesting portion does node.js provide any debugger. The answer is yes node.js do provide a simple tcp-based
protocol and debugging client that comes built
in with node.js. Now what our exit codes
of node.js exit codes and node.js are you can say
specific set of specific codes, which are used for finishing
a specific process since node.js is
a single-threaded framework. So you have to finish
the specific processes. So what are the exit codes
for example unused fatal error and Court battle exceptions internal JavaScript
evaluation failure. All these codes are
internal to node.js Now what is an event emitter in node.js even tell meter
is a node.js class that includes all the objects that are capable
of emitting the events then how we write these events
in node.js program. So we write on Steven tomato just object is equal
to require events. And then this class my emitter
extensive enter meter. So this is the parent class
and this is a custom class. So basically in
node.js event amateur includes all the objects. That are capable
of emitting events. So in this particular example, I am omitting event my emitter which is the object dot
on and this is dot m it even so these are some some of the events because of which
I am using here in this code because I am using a class called event emitter again
a very interesting question is cryptography supported
in node.js again, the answer is yes. No J’s support cryptography
to to a module called crypto and this module provides various
cryptographic functionalities, like Cipher decipher sign
and verify functions Etc. So cryptography implementing
cryptography application is also possible with no chairs. Now, this is the last question
for this webinar on node.js interview question. Explain the reason why
Express app and server must be kept separate. What is the reason the reason is that faster testing execution
the testing would be fast if we will keep the app
and server as separate entities and then it allows
testing the API in process without having to perform
the network calls and there is always
a better separation of concerns and the code
will be very cleaner and it will be helpful to read
and debug and you can say Say getting wider coverage
metrics of the code so you can read the code
and you can debug the code. If you keep the express app and server as separate entities
last what is the reason that it allows deploying
the same API under flexible and different network condition. That’s it. Thank you very much. I hope you have enjoyed
listening to this video. Please be kind enough to like it and you can comment any
of your doubts and queries and we will reply them at the earliest do look out
for more videos in our playlist And subscribe to Edureka channel to learn more. Happy learning.