DEV Community

Cover image for Day 24 – Working with EJS (Part 1) - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb)
Muhammad Ali (Nerdjfpb)

Posted on • Originally published at blog.nerdjfpb.com

Day 24 – Working with EJS (Part 1) - Learning Node JS In 30 Days [Mini series]

Hope you’ve installed the ejs. We’re going to learn some more things about it.

To start with the ejs first we need to set our app to use it. We can simply add this using app.set('view engine', 'ejs')

Alt Text

This app.set('view engine', 'ejs') will look into views folder on the same path. So we need to create views folder for writing those frontend templates.

We are starting the profile page. So we’ll create a profile.ejs file inside the views folder.

Just create a basic html inside of profile.ejs file.

Alt Text

We’ll use res.render method to render the file.

Alt Text

It’s already know where to look. So we’ve render the files, but we didn’t pass our data to the html. How to do that ?

Alt Text

We can send a object in render method while rendering the template and use that object to get the value of username.

Alt Text

Now we can easily access the value using <%= person %> in the profile.ejs file

Alt Text

See the result in frontend

Alt Text

Can you do this now?

You can see the graphical version here

Originally it published on nerdjfpbblog. You can connect with me in twitter or linkedin !

You can read the old posts from this series (below)

Top comments (0)