-: In the last section we added our middleware
of morgan and bodyParser.
One thing that we still haven't done yet
is set up our server to handle any actual routes
(mouse clicking)
or respond with any data.
So no matter how I make a request to the server
it's always gonna respond with cannot get slash.
(mouse clicking)
Let's fix that up by adding some route handlers
to our express application.
I like to keep all of my route handlers
outside of this main index.js file
and just keep index.js around for
basically initialization of my server.
Instead, I'm gonna move all of my route handlers
to a separate file, which I'm gonna call
router.js (keyboard tapping)
So I'm making a new file called router.js
inside of my server directory. (mouse clicking)
Now here's how this router is going to work.
We're going to export a function from this file.
We will import that function into this file
and then we will pass app into that function.
In other words, we should just basically assume
that we're going to have access to the app over here.
So to export code in a node js environment
(keyboard tapping)
We write module.exports
and I'm gonna assign that to a function
that's gonna be called with our app.
So let's wire this up now.
Back inside of index.js. (keyboard tapping)
I'll say const router is require
and this is the file we created.
So we'll use dot forward slash router
and then down inside of our app setup,
we will call our router.
Excuse me.
we will call the router with our app.
Cool.
So now over here, inside this function
we have access to our express app,
which is basically the brains of our entire application.
(mouse clicking)
So remember what we're trying to do here is define a route
that the user can visit.
Just something that we can say, yeah, this is,
you know our app is working so far.
So to add route handlers to express
we say app.get
forward slash
function like so.
(keyboard tapping)
So this is a very repeatable unit functionality right here
and it'd be really good if you got a solid grasp on this
kind of set from the get go.
On app we call a function called get.
Now this get right here maps directly up to the type
of http request that's gonna be issued
and we want to handle.
So we can say app.get if we are expecting a post request
we would say app.post. (keyboard tapping)
But in this case we're gonna say that we're expecting
a get request to come in.
The first argument to .get is the route that's going to be
you know the route that we're trying to handle.
So if our user just visits our app
which is essentially foward slash, then run this function.
So there's a very tight mapping between this string
and the function that we pass.
The function that handles our route gets called
with three very important arguments.
They are req, res, and next.
Req is short for request, and it is an object that
represents the incoming http request.
So it's got a lot of data about the actual request
and you know where it's coming from
what route it was looking for
just a bunch of data about the request that was made.
The res, which is a second object here,
is the argument that we have access to,
To essentially it represents the response
that we're going to form up
and send back to whoever made the request.
So whenever we are dealing with the response
this is our area or this is where we get
our ability to respond to our users in some fashion.
Finally, next, next on here is mostly for error handling.
We're talk, we'll talk about it in a little bit
when we start working with some actual users.
Again, for right now, I wanna just respond
with some very static, very plain JSON, just so we know
that everything is working fine for right now.
So to send back some amount of JSON
to whoever's making this request
we can just say res.send.
And then basically whatever we wanna send
in this case I wanna send, how about just an,
an array of strings. I think that'll do.
So I can send an array of about water bottle, Phone, paper,
and if you were curious about what's on my desk right now
as I record this video it is a water bottle, phone,
and some paper just in case you're curious.
All right, let's give this a shot.
So node mon probably just restarted our server.
Looks good.
It's restarted, it's screen right here.
If you have any typos, you'll see them in the
in the terminal right here.
You can see that as I was going through and saving work
I probably made a typo or two
and so I got an error message in there,
but node monitor restarted
and everything recovered just fine.
So my expectation is that I should be able to make a
get request to forward slash and I should see an array
of water, bottle, phone, and paper.
Let's give this a shot over in Chrome.
I'm on local host:3090.
I'll refresh and sure enough,
I got water bottle, phone paper in an array,
just what I wanted.
Now, if whatever you're looking at looks a little bit
more garbled than this or you see everything
on a single line is just plain text, that's totally fine.
I have a plugin for Chrome that just formats JSON
in the browser very nicely, as you see right here.
All right, so this looks great.
This is exactly the functionality that we're gonna
duplicate again and again and again
to make route handlers inside of Express.
So this is very good so far.
Let's continue in the next section
and start working on authentication.
I'll see you over there.