In the last section we spoke at a very high level about the architecture of our application in this
section.
We're going to start off by generating a brand new node project we're going to install express into
it and then we'll talk a little bit about the differences between Express and node inside of our project.
So let's get to it.
Let's generate our project and install express inside of it.
I'm going to change on over to my command line.
I'm going to make sure that I'm inside of a directory that is appropriate for housing this brand new
project.
So on your local machine you might have like a workspace directory or a project's directory just some
location where you can put some new code inside if you're going to create a new directory called server.
I'm going to change into that directory and then I'm going to use the NPM command line tool to generate
a brand new project.
So I'm going to run the command and PM in it like so after running that command.
I'm going to get asked a couple of different questions here.
For right now we don't really care about the responses to any of these.
So I'm just going to use the default values by hitting enter enter enter enter and Silom get by running
that NPM and command we generated a package not Jason File which is used as kind of like the central
hub of our project and is going to be used to define a lot of the different dependencies that our project
is going to depend upon.
The first dependency that we're going to install is express.
We will install and suppress excuse me express by running the command NPM install dash dash save express
like so.
Now this should only take a second or two to install after installing express.
I can then look into my package not just on file and I will see Express reflected in there underneath
the dependency section.
Now just really quick.
Notice I use the cat command right here.
This is only available on Mac OS and Linux based systems is not available on Windows.
So if you run cat package out Jason you might see a little error.
Now this is probably the only time I'm going to ever use a platform specific command.
So if your are are on Windows don't worry we're not going to do anything like this in the future.
OK so clearly Express's now installed.
If you have a newer version that is totally fine.
Absolutely.
OK so we've now generated our project and install express inside of it.
But I still want to talk a little bit about what the differences between Express and node and how we're
going to be using each of them inside of our application.
So the first thing I want to do is give you a very plain English definition of node in a plain english
definition of javascript and then we're going to look at a different diagram.
That's going to communicate how the two of them work together.
So first off note this is a javascript runtime.
It is used to execute code outside the browser.
Traditionally javascript code has always been executed inside of some web browser because that's where
javascript first got its start.
It was used to execute code inside the browser on Web sites to give them a little bit of a sense of
interactivity or make them feel just a little bit more dynamic.
Over time people realized that they wanted to use javascript in other locations as in outside the browser
and that is the source of the no JSA project.
So node is used simply to execute code outside the browser.
In other words in some arbitrary environment like on my laptop or on your desktop or any other location
you can think of now express on the other hand is a library that is using the node J.S. runtime.
So Express is not a standalone thing.
You can imagine express as being a little collection of functions or helpers for making working with
the HTP aspects of no just a little bit easier.
So Express is really not its own standalone code base per se or its own runtime.
It is a library that has a collection of helper methods to make writing servers just a little bit easier.
Everything that you find inside of this express library we could in theory implement from scratch using
just node like just writing plain javascript and using some code that is included with no G-S so Express
is just a helper thing and its something that just makes our lives a little bit easier.
OK so that's notan Express and that's really important to understand the difference between the two.
Because the next diagram that we're going to look at is going to be a little bit crazy.
OK so this is kind of getting a little bit more towards the caliber of diagrams in this course.
I would say we're going to have some pretty crazy diagrams but don't worry.
And do my best to always explain exactly what is going on with each of them.
OK so let's go through it again.
Let's go through this diagram and we're going to be talking about some of.
What Express does in some of what node does.
So the first thing to understand is that when you are running some server on your local machine your
server is going to be listening for h t t h TTP traffic on a single individual port.
You can think of a port as being like a little door through which traffic like HTP request can be routed.
So we might have some incoming request being issued by say our browser also running on our local machine
and it might make a request Ricksen me that request might be coming in to some very specific port on
our machine.
We are going to configure node and express to listen to traffic that is attempting to access a very
specific port on our local machine.
Now no G-S specifically is going to do what is listening for traffic on that port and waits for some
information to flow in through it.
No it isn't going to take that information that flows in from this incoming HDTV request and handed
off to the Express side of our application.
So remember we can do everything with mode.
We don't need express notice the real core aspect of what's going on here and it is what actually handles
all the underlying HTP traffic.
However we want to use Express because it just makes our lives a little bit easier.
So no it is going to take this incoming traffic.
It's going to hand it off to express express is then going to look at the request and it's going to
decide what little bit of logic inside of the Express application that we're building is going to handle
or somehow respond to this incoming request and express we write collections of what are called route
handlers route handlers are used to handle requests that are asking for a very specific service.
So we might have one handler that is responsible for authenticating and user.
We might have another handler that is responsible for logging out a user and we might have a third one
that allows a user to create and save a new survey or a new campaign.
So again note is going to take the incoming traffic routed to express express we'll then figure out
where to send that incoming request to you and I are going to route the right to the route handlers
that will process the incoming request and generate some outgoing response.
Their response right here will then be sent back to running node process and notable then respond to
the incoming request with this response that we author and so eventually this object what we refer to
as rez which is short for response will be sent back to whoever made that incoming HTP request.
OK so that's kind of Noten express in a nutshell again the takeaways are that the underlying node runtime
is listening to this traffic on some very specific port when some traffic comes in.
It is then routed to the Express side of our application.
You and I are going to route some amount of logic that handles incoming requests and formulates and
sends out a response that gets routed back to whoever made the original request.
OK so that's great.
That's enough right now.
Let's take a break and then the next section will talk a little bit about exactly how these route handlers
are created inside of an express application.
And we're also right our first raw handler.
So I'll see you in just a second.