In the last video I mentioned that in this one we would be exploring handlebars of partials.
Now we are still going to cover partials but I'm pushing that back to the next video.
In this one I want to take a quick moment to talk about how we can customize how handlebars is set up.
I want to do that first because in the next video we're gonna have to do that further to actually get
partials to work.
So the goal in this very quick video is to learn how we can customize the location and the name of the
views directory.
Right now it has to be in that location.
The route of the project and it has to have the name views to get started.
Let's go ahead and change the directory name and watch things fail.
So right here I'm going to rename views over to something like templates.
Now both directory names let you know what's inside.
The problem is that express is not looking for a templates directory.
So if we were to go ahead and refresh any of the pages on our site that use a template we would see
an error message like the following error failed to look up view help in views directory and it's pointing
to where it's expecting the views directory to live at is in the root of the web server project in a
folder called views.
We had that previously which is why things worked.
We no longer have that which is why things are failing.
Now this is just the default location that express expects your views to live in.
You can always customize that but to customize it you have to tell express where to look.
And this is going to require us to create a brand new path.
So let's go ahead and define one similar to how we defined public directory path right here I'm going
to define a constant which I'll call views path.
And I'm going to set this equal to an absolute path to that views folder which I've called the templates
so I'll use path dot join like I did up above and we'll start with underscore underscore dir name and
remember that is the path to the folder.
This file lives it which would be the source directory.
That's not what we want.
So we'll go ahead and provide another argument to alter that path.
We're gonna use dot dot to go up a directory like we did when we needed to point to the public folder
and then instead of forward slash public it is forwards slash templates.
Perfect now that we have this in place we need to tell express to use this path and we do that by using
a another app dot set call.
So just down below app dot set and we're going to go ahead and set up a another setting for Express
and this one is called views.
We don't have to set it in order to use handlebars.
We've already proven that.
But if we do set it it's to the views directory we'd like to use.
And in this case we just created a variable that stores that path now that we are correctly pointing
express to our custom directory.
Things are gonna start working again down below after saving the file the server did restart over here.
I'll refresh the page and what do I get I get my template rendering once again like it was when we had
the views directory.
The only difference is that we've customized its name and we can now further customize its location
putting it in a nested directory if we wanted to we could put it anywhere we want as long as we update
the view's path to point to it now that we have this in place.
Let's take a quick moment to organize our Express configuration a little more.
I'm gonna break out these two lines into their own little section adding a comment to remind us what's
going on here.
We'll add other paths later.
So for now we can title this little section something like define parts for Express config.
Down below we have these three lines.
Now the first two are specific to handlebars.
And the last one is more general.
So I'm gonna take those first two lines and add a comment for them setup handle bars engine and Views
Location.
And then finally we could add a comment for this line if we want to remember what it does as it's not
very often we're going to end up changing it.
And it could be easy to forget something like setup static directory to serve
Excellent.
Now that we have things in place our application is looking really good.
And the last thing I want to do is walk you through the express documentation really quick so you can
learn how to use it to figure out all of this stuff.
Well we figured out throughout the videos the Express documentation as we explored earlier is over at
Express J S dot com express J S dot com.
And once we're here we are looking for the API reference page.
Now the API reference walks through every single thing available and it's a really nice set of documentation.
So over here we have a few very high level things.
We have the express function which we've already used as we can see here there's not a whole lot to
it.
We simply call it to generate a new instance of the application.
We also have application.
This refers to our app variable over here.
And if I were to click on that down below under methods we can see all of the methods we've used.
We have app dot get app dot listen and others like app dot set an app dot use as you can see there are
plenty of others but the reason I'm bringing you here is to quickly pull up documentation for app dot
set.
Now we already know how it works we call it we pass in the name of the setting we're trying to set and
the value we want to set.
But down below it also contains a table of all of the built in settings that can be set that express
is actually going to do something with.
There are about a dozen or so and near the bottom under V we have views and we have view engine both
of which we've already used in our application.
The documentation is fantastic it walks you through exactly what value should be set and what exactly
the setting is going to do.
Now we also have request response and router we're going to explore router later in the class request
and response contain documentation for the request and response.
Objects that are callbacks for a route get passed.
So if I click on request we can see all of the things we can do with that object.
And if I click on a response we can see all of the options we have here including a couple that we've
used.
Response dot send.
We could learn how to use that even though we already know how to use that.
And we also have response dot render just up above and here it talks about how to use Express with a
template engine.
So I just wanted to take a quick moment to walk you through the documentation for expect excuse me for
Express so you can get a feel for how exactly it works and how you can use it to discover new features
and customize your application further.
That's where we're gonna stop for this video in the next video as I promised previously we will actually
explore how to work with handlebars partials.
I'm excited to get to that.
So stay tuned and I'll see you then.