Instructor: In the last section,
we did some initial setup for our server.
Let's continue with our app setup.
The first two lines
that we're gonna put in here for our app setup
are just a little bit of boiler plate.
They're kind of those gotcha things
that you just gotta put in without really knowing
ahead of time,
having a good inclination of why you might.
So just bear with me as we write the code down
and then we'll talk about exactly what it's doing.
The first thing we're gonna do
is we're going to tell our app to use Morgan
and then we're gonna pass Morgan a string of combined.
We'll do one more and then we'll talk about what's going on.
Gonna put app.use bodyParser.json.
I'm going to invoke json opacity object,
which is gonna have type star slash star.
Make sure you gotta a forward slash in there
between the two asterisks.
Okay, so in both cases,
Morgan and body parser
are what we refer to as middleware in Express.
Middleware in Express
is something that any incoming request
is gonna be passed into.
So any incoming request into our server
is going to pass into Morgan
and it will also be passed into body parser by default.
That's what the app.use function right here does.
It registers them as middleware.
Morgan right here is a logging framework.
So previously,
when we start up the server in the last section
and I went to local host 30 90.
I'm gonna restart my server
by pressing control C to stop it.
I'm gonna restart it
and now I'm gonna go back to my browser
and refresh the page.
And not quite happening yet.
That's okay.
Oh, I didn't save it.
Save the file,
restart the server.
My mistake.
And I can now refresh in the browser
and you can see that I'm getting some logging in here.
So that's what Morgan does.
Morgan is a logging framework.
You can see that it's logging that there was a GET request
made to forward slash.
And we responded with a 404 to it.
So again, Morgan is just about logging incoming requests.
It's mostly, we'll be using it for debugging.
Body parser is another middleware
that is used to parse incoming requests.
Specifically.,It's gonna parse them into json
and it's going to attempt to do so
no matter what the request type is.
So this might lead to some problems down the line
if say we're trying to post a file
or we're trying to make an HTTP request or what have you.
But for right now,
it's gonna work out just fine.
Basically, any request that's incoming,
it's gonna be parsed as though it were json.
That's all we need to be concerned about right here.
So one thing that I hope you just noticed
was me just making a mistake and restarting the server
without having saved the file was kind of annoying, right?
That could probably be a little bit better.
So there is a fantastic helper module called Nodemon
that's gonna help us out with our development.
Let's install it,
get it running,
and we'll talk about exactly what it's doing.
But I guarantee you,
you are gonna like Nodemon.
So back in my terminal,
I'm going to close the server with control C
and then I'm going to run npm install
dash dash save nodemon.
N-O-D-E-M-O-N without any spaces.
So what Nodemon does
is it watches our project directory for any file changes.
If a file ever changes,
it's going to immediately restart the server
so that the server is updated
with whatever changes we just made.
And so what that means is
we can just go ahead and edit this file
or edit anything in our server as much as we want.
And whenever we tab back over to the browser
and say we tried try to refresh the page or what have you.
When we refresh the page,
Nodemon will have already restarted our server
and we'll get access to all the most up to date code.
So it looks like our Nodemon install is done.
All we need to do now is wire it up.
To do so, we're going to make a new script
inside of our package.json file.
So you can see the scripts directory here.
The purpose of scripts are just to define command line.
Basically commands to run at the command line
in an abbreviated format.
So we're going to add one command in here.
I'm gonna put a comma at the end of the existing script
and then we'll put inside of double quotes
dev and then colon.
And then we'll say nodemon index.js like so.
I'll save this.
Flip back over to the terminal.
And to run that script that we just named Dev.
We just run npm run dev.
So this is gonna start Nodemon up.
You can see the Nodemon command right there.
It's watching the current working directory
for any file change
and it's going to start node index.js.
So just to verify that the servers still up.
I'm gonna make a refresh here.
I see the,
I get the console log over here,
it's still in air, totally fine.
But let's see what happens
when we make a change to our file.
Let's say any change is going to trigger Nodemon.
So it can even be a comment.
I'm gonna add in a comment.
I'm gonna save it.
If I flip back over,
we see restarting due to changes.
I can go back over to my browser and boom, here we go.
I'm gonna put in some new lines just because.
I'm gonna delete the comment,
save the file,
and boom, Nodemon restarted my server again.
So basically with Nodemon,
all we ever have to do is run npm run dev,
and we're pretty much good
for all development on our node server.
We don't have to mess around with anything else
or restart it at any time.
So it's a fantastic little tool to have around.
Okay, so with that nice little tip out of the way
and our app set up,
all done with our Morgan middleware
and our body parts or middleware.
Excuse me.
Let's get started in the next section
where we're going to figure out
how to make actual requests to our API
that get responded to with json.
I'll see you in the next section.