In the last section we put together the bare minimum amount of code required to get our express server
running and then we accessed it inside the browser.
I wanted take a little bit of time to now go back and analyze every last piece of the code snippet that
we just added inside of here.
Now before I start that you might be thinking seaven.
This is really basic express stuff I already know express just get on with it.
Well I want to remind you that this course is really for everyone who has previous experience with react
and redux.
And I'm kind of assuming that if you're here you might be a little bit newer to express.
So I just wanted to cover everything from absolute basics and then have a really solid foundation and
go from there.
So if this stuff is review for you.
Great.
Hey it's Review.
Just take the review.
Otherwise hopefully you're learning something as you go through.
Anyways let's get back to it.
So we're going to analyze this whole segment of code right here.
Now I've got a little diagram that breaks it down piece by piece.
So in this diagram you see app get forward slash rec rez.
Well that matches up with app get Ford slash rack.
RAZ And so on.
OK.
So let's go through this thing piece by piece.
First off is the app object.
Remember this represents the underlying running express server the Express server has some number of
Route handlers associated with it by calling app dot get like calling that function get.
We are creating a brand new route handler.
So this entire segment of code right here we would refer to as a route handler.
Next is the get function.
So we called out get this tells express that we want to create a route handler that is watching for
incoming HTP requests with a very specific method.
HTP request methods are used to indicate the type or kind of what the request is attempting to accomplish.
So we made a route handler associated with the get method Express has access to several other methods
as well.
So we used to get good is associated with getting information about some particular record.
We can also set up road handlers that are associated with other methods as well.
So app dot post or app dot put update delete after patch.
Each of these request methods again are associated with some intent.
Now we're not really required to follow these very explicitly.
They are more of kind of convention of what we follow so that it's really clear at just a glance what
any given requests overall purpose or overall goal is.
So we would say that any request with a get type method is attempting to get information a post wants
to send information to the server A put wants to update some properties of something a delete wants
to delete something and a patch wants to update update.
Just like one or two properties of something.
So over time we're going to be using some of these different methods to create different route handlers
that are associated with different purposes of incoming requests.
OK.
Now the next one the forward slash the forward slash tells express to watch for incoming requests that
are s that are attempting to access some very particular route.
And so we refer to this forward slash right here as the route portion of the handler.
This specifically instructs instructs express to say if anyone visits the route localhost Colan 5000
slash.
So whenever we type into our browser local calling 5000 we don't you know I don't really write on slash
right here but the browser is just kind of interpreting or assuming that we are attempting to visit
localhost Coel and 5000 slash.
So this is kind of an implied slash right here and express really takes that to assume if anyone comes
to like our route route that's what the forward slash here is really indicating we could just as easily
have a route of something like Slash greeting like so and then to create an equivalent route handler
over inside of our code base.
We would say slash greeting.
So now a request to local host Culin 5000 slash greeting would execute this route handler right here
OK.
But right now we're going to leave it as just slash back over here.
So now onto the arguments to the arrow function which is the second argument of the Get request handler.
The first argument is referred to as WREC.
This is shorthand terminology for a request.
It's javascript object that represents the incoming request.
So it has a bunch of data that says a little bit about who is making the request and some associated
data with it.
Next is the rez object.
Rez is short for response and it represents the response or the data that is about to be sent back to
whoever made the incoming request.
Finally is the body of the arrow function we put in there.
Reza not send and then we provided a plain javascript object that tells express that we want to immediately
close the request and send back a response containing the Jaison data.
Hi Colin there.
Which is why we saw in our browser Hi Colin there.
So this is a little bit of juice on data right here.
Now one thing you'll notice with the actual request handler having broken all this code down is the
second argument was an aero function the Aero function is called automatically by express.
Any time some request comes into this route right here a forward slash.
And so over time this function right here might be getting called many hundreds or thousands or tens
of thousands of times in a single day or maybe hundreds of times in a single minute or maybe even dozens
of times in a single second.
So this function right here we executed every single time a single request comes and attempting to make
a get request to the route of forward slash.
Now the final thing I want to point out is the last line here of ductless and 5000.
This line instructs express to tell node that it wants to listen for incoming traffic on port 5000.
So even though it's express that it looks like is actually listening to the port behind the scenes Express
is still telling node to listen to port five thousand seven over this diagram we had said that there
is some port or some little door through which traffic is flowing on your machine notice waiting for
traffic to flow in there and then that traffic is routed onto Express.
So again even though it looks like this is express listening to the port Express is really just kind
of turn around and telling no the run time hey watch for any traffic coming in on port five thousand.
OK.
So that's the basics of this little segment of code right here.
Now again if you're already familiar with express this might seem like a little bit of review and hey
that's totally fine.
If this is completely new to you and you've never worked with Express before.
I've got great news for you.
Everything else that we do with Express is going to look nearly identical to this little segment of
code almost everything else we do is going to be small variations on a theme with this segment right
here.
OK.
So that's basically the you know basics of Express.
Let's continue with the next section and start talking about the first task that we are going to attempt
to accomplish with Express.