To get started with a basic example start a new project folder, install express, and ejs. In this post I am using express 4.16.x, and ejs 2.6.x. In no major code breaking changes have happened this post should still be up to date.
As you may have noticed I made a views folder inside the root name space of the project folder, this is where I will be placing all my ejs template files. For my basic demo I just stared off with a single index.ejs file in the root name space of the views folder.
index.ejs would look like this for starters:
The main file that is called to start the project is often called app.js in an express.js project, often located at the root of the project folder. In this basic example of using ejs to render a template using express.js and node.js, I just need to set the views path to the folder where my ejs template are, and set the view engine to ejs.
When I start this and go to localhost:8080 in my browser I am greeted with the “Hello ejs!” message. I have another post on ejs in which I work with the ejs module by itself, which might be a good idea to do if you want to learn more about ejs. Ejs is just one template engine option when it comes to making an express view, there is also making use of the express static built in middleware as well for the purpose of hosting static assets that are to be used in a view.
If you liked this post be sure to check out my main post on expressjs in general.