Transcripts
1. 1. Introduction to the Node.js Universe: Welcome to the Express course. My name is Shen Rogovinhi and I'm super excited to be your instructor
for this course. I have done my best
to create the most comprehensive and up to
date express course, it shows you all the modern ways to build applications
with Express. And I hope by the
end of this course, you will become an expert
Express developer. So are you ready to get
started? Then let's jump in.
2. 2. Understanding Node.js: A Primer: Node or Node JS is an
open source and cross platform runtime environment for executing JavaScript code
outside of a browser. Quite often, we use node to
build the backend services, also known as APIs or application
programming interfaces. These services are the power source for
client applications, like a web application
running in a browser or a
mobile application running in a mobile device. These services are nothing but what the user
sees and interacts. They are just a surface. They need to talk to some
kind of services sitting in the server or in the
cloud to store data, send emails, push notifications, pickup workflows, and so on. Node is ideal for
building highly scalable, data intensive and real time backend services that power
our client applications. Now you might ask, but Shen, there are other tools
and frameworks out there for building backend
services such as asp.net, Rails, Jango and so on. So what's so special about node? Well, node is easy
to get started and can be used for prototyping
and agile development, but it can also be used for building super fast and
highly scalable services. It is used in production by large companies such as PayPal, Uber, Netflix,
Walmart, and so on. In fact, at PayPal, they rebuilt one of their Java and spring based
application using Node, and they found that
the node application was built twice as fast
with fewer people, 33% fewer lines of code, and 40% fewer files. But more importantly, they double the number
of requests served per second while decreasing the average response
time by 35%. So node is an excellent choice for building highly
scalable services. Another reason for using Node is that in node application,
we use JavaScript. So if you're a front end
developer and no JavaScript, you can reuse your JavaScript
skills and transition to a full stack developer and get a better job
with a better pay. You don't have to learn a
new programming language. Also, because you can use JavaScript both on the
front end and back end, your source code will be
cleaner and consistent. You will use the same
naming convention, same tools, and the
same best practices. And finally, another reason
for using Node is that it has the largest ecosystem of open source libraries
available to you. So for pretty much
any features or building blocks you want to
add to your application, there is some open
source library out there that you can use. So you don't have to build any building blocks
from scratch, and instead, you can focus on the core of
your application. Next, we are going to look
at the architecture of node.
3. 3. The Blueprint of Node.js Architecture: In the last video, we
learned that node is a rundom environment for
executing JavaScript code. But what is a rundom
environment really? Well, before node, we
use JavaScript only to build web applications
that run inside of a browser. So every browser
out there has what we call a JavaScript engine that takes a JavaScript code and converts it to a code that
a computer can understand. For example, Microsoft
Edge uses Chaka, Firefox uses Spider Monkey. In Chrome uses eight. And because of these
varieties of engines, at sometimes JavaScript code can behave differently in
one browser for another. Now a browser provides a runtime environment
for JavaScript code. For example, you probably
know that in a browser, we have the window
or document object. These objects allow
us to work with the environment in which
our code is running. Up to 2009, only way to execute JavaScript code
was inside of a browser. In 2009, Ryan Dall, the creator of Node, came up with a brilliant idea. He thought that
it would be great to execute JavaScript
outside of a browser. So he took Google's VD engine, which is the fastest JavaScript
engine out there and embedded it inside a C program and called that program Node. So similar to a browser, node is a runtime environment
for JavaScript code. It contains a JavaScript engine that can execute our
JavaScript code, but it also has
certain objects that provide an environment
for our JavaScript code. But these objects
are different from the environment objects
that we have in a browser. For example, we don't
have the document object, instead, we have other objects that gives us more
interesting capabilities. For example, we can work
with a file system, listen for a request at
a given port, and so on. We can do stuff like that
inside of a browser, right? So in essence, node is a program that includes
a weird engine plus some additional
modules that gives us capabilities not available
inside browsers. We can work with a file
system or network, and so on. Both Chrome and node share
the same JavaScript engine, but they provide
different runtime environments for JavaScript. I have seen people
comparing node to C sharp or Ruby or some other
programming languages. But these comparisons
are fundamentally wrong because node is not
a programming language. It's like comparing
a car with an apple. By the same token,
node should not be compared with frameworks
such as sp.net, Rails or jango and so on. These are frameworks for
building web applications. Node is not a framework. It's a runtime environment for
executing JavaScript code. Next, we are going to take
a look at how node works.
4. 4. Node.js Under the Hood: Operational Mechanics: Earlier, I mentioned that node applications
are highly scalable, and this is because
of the non blocking or asynchronous nature of node. What I meant by asynchronous? Let me give you a metaphor. Imagine you go to a restaurant, a waiter comes to a table, takes your order, and
gives it to the kitchen. Then they move on to serve another table while the chef
is preparing your meal. So the same person can serve
many different tables. They don't have to wait
for the chef to cook one meal before they
serve another table. This is what we
call non blocking or asynchronous architecture, and this is how no
applications work. The er is like a thread
allocated to handle a request. So a single thread is used
to handle multiple requests. In contrast to non blocking
or asynchronous architecture, we have blocking or
synchronous architecture. Let's see how that works. So back to our
restaurant example. Imagine you go to another restaurant and
in this restaurant, a waiter is allocated to you. It takes your order and
gives it to the kitchen. Now they're sitting
in the kitchen waiting for the chef
to prepare your meal. At this time, they are
not doing anything else. They are just waiting
and not going to take order from another table
until your meal is ready. This is what we call blocking or synchronous architecture, and that's how applications
built with frameworks like asp.net or RAILS
work out of the box. So when we receive a
request from the server, a thread is allocated
to handle the request. As part of handling
that request, it is likely that we are
going to query a database. And as you know,
sometimes it may take a while until
the result is ready. When the database is
executing the query, that thread is sitting
there waiting. It can be used to
serve another client. So we need a new thread to
solve another client and imagine what would happen if we have a large
number of clients. At some point,
we're going to run out of threads to
serve the clients. The new clients have to wait until free threads
are available. Or if you don't
want them to wait, we need to add more hardware. So with this kind
of architecture, you are not utilizing the
resources efficiently. This is the problem
with blocking or synchronous architecture. And as I explained, that's how applications
are built with frameworks like
asp.net or by defact. Of course, in asp.net, it is possible to use
asynchronous architecture, but you will have to do
extra work for that. In contrast, node applications are asynchronous by default, so you don't have to
do anything extra. In node, we have a single thread to
handle all the requests. When a request arrives, that single thread is used
to handle that request. If we need to query a database, our thread doesn't have to wait for the database
to return the data. While the database is
executing our query, that thread will be used
to solve another client. When the database
prepares the result, it puts a message in what
we call an event queue. Node is continuously monitoring this queue in the background. When it finds an
event in this queue, it will take it out
and process it. This kind of architecture
makes Node idle for building applications
that include a lot of disk network access. We can serve more
clients without the need to throw
in more hardware. And that's why node applications
are highly scalable. In contrast, NO should
not be used for CPU intensive applications like video encoding or an image
manipulation service. In this kind of applications, we have a lot of calculations
that should be done by CPU and few operations that touch the file
system or the network. Since no applications
are single threaded, when performing the calculations
to serve one client, other clients have to weigh, and that's why NO should not be used for CPU intensive
applications. Should only be used to build data intensive applications
or real time applications. That's a lot of theory. Next, we will see node installation and build
our first node application.
5. 5. Setting Up Your Node.js Environment: This lecture, I'm going to
show you thousand store node. If you are on Windows,
open up Power Shell. If you are on Mac or Linux,
open up the term no. First, let's see if you are already using node on
your machine or not. So run node space dash version. You can see on this machine, I'm running node
version 16.16 0.0. This is an earlier
version of No. The latest stable
version is 18.16 0.1. Now on your machine, chances are you may not have node or you might have
an earlier version. The way, I want you to install
the latest version of K. So open up your browser and
head over to nodjs.org. On the homepage, you can see we have two
versions of Crowd. One is the most stable version, also known as long term
support or LTS version, which is recommended
for most users. At the time of
recording this video, that's version 18.16 0.1. And there's always a
new version which is experimental and it
might not be stable. I want you to install
the LTS version, also taking into account
that in the future, when you are
watching this video, chances are that the LTS
version might be Neva. If you're worried that this course is going
to get outdated, don't worry because
in this course, we are going to focus
on the fundamentals. So I'm going to work with the
core modules of the node. These core modules are stable. They have been there
for a long time. So the code that we are going to write in this course will continue to work with the
future versions of the node. Once you master
the fundamentals, you can always learn about the new features that come in every version by
looking at the change low. So let's not worry about the fancy new features in node and focus on
the fundamentals. So let's go ahead and install
the latest version node. You can see here,
I get a packet, run the package.
It's an installer. Ran the permission run, and the preparation will
start installed and all. It will take some time. We next, agree to the terms
and conditions and quick net. You may
change the part. I will recommend
to stick to this. Next, again, net. And you can check this spot whatever makes the dependencies. Istall again, it will take
some time to install, no. Now the installation
is complete. It's quite simple and easy, and it just take a few seconds. And now you have the terminal
to install dependencies. Just follow the instructions
and you're good to go. Now, back to the terminal, real write node, space, dash dash version one more time. And see, I've upgraded my
node version to 18.16 p one. Next, we're going to write
our first node program.
6. 6. Your First Steps in Node.js Programming: All right, now we are all set to write our first node program, so I'll quickly make
a folder and go into that folder with C
first underscore app. And now I'll open this in
VS code using code period. VS code is my
preferred code editor, but you may use any other code editor available
in the market. I'll quickly make
a file and the Js and I'll write a function
using arrow function. As, I'll say hello. This will take an
argument first name, and I'll just write sole log. Hello, and I'll pass the
argument first name. That's my first node program, and I'll just call this
program by writing say hello. I'll pass the parameter
as my name hi. Now, back to the terminal. I'll execute this code using Node and write the name
of the file app dot js. As you can see, it worked fine. Hello Shiva. Now let me
show you something else. I'll amend this out and console log window,
back to the terminal. I'll run node app
dot js again. H. As you can see, we got an error. This window is not defined. So node, as you before is
a C plus plus program. It includes Chrome's
Vd JavaScript engine. But this app the JS
file we passed node. Node gave it to
Vd for execution. Now, back to or error, window is not defined. As it you before, in node, we don't have the Window
or document objects. These are part of the
runtime environment that we get with browsers. In node, we have
other objects to work with files with
the operating system, with the network, and so on. And that's what
you're going to learn about in the next section. I hope you enjoyed this section, and thank you for watching.