An express view folder contains template files that are use for server side rendering. It can be used in place of, or in conjunction with other assets that can be served via express.static. In order to use a view folder there must be a template engine to use. I tend to prefer ejs, but there are many other options such as pug. So this post will be centered around setting up the beginnings of an express view folder, and maybe I will get into some more advanced topics when it comes to creating a client system for a project.
In this section I will be covering a very simple express view folder example that makes use of just express and ejs as a template system so that is all that will need to be installed when it comes to npm packages.
In this example there will just be a single express app.js file in the root name space of the express_view project folder, and a single index.ejs in the views folder as well.
In the main app.js file that will be called to start the server, I am using the app.set method to set the view engine to ejs. Now I can use the res.render method to render html using an ejs template file and some local variables. In this example I am just going to be passing some string literals to the template, but in a more advanced project this could be some data that is retrieved from a database or something to that effect.
When calling the res.render method the first argument that I give is the name of the file in the views folder that will be used to render the html that will be sent to the client. So in the next section I will be taking a quick look at the index.ejs file for this example.
Here I have the index.ejs file of this crude, simple, yet function express view. Notice that I am using the key names of the object that I passed to the res.render method in the app.js file. The values of those object keys will be what is displayed for the title and other elements in this example.
There is much more to write about when it comes to ejs, but I will not get into depth in ejs to much in this post as I have also wrote a post on using ejs with express, and I have also wrote another post on using ejs with just node.js by itself as well. There are of course many other options when it comes to template langauges, I tend to prefer ejs but another option would be pug. Still when this example is up and working it should help to give a general idea as to why ejs is useful compared to its serving static html assets.