How to use forEach loop in Node JS template engine


Hi friends, in this tutorial you will learn how to use forEach loop in Node JS template engine. Generally, ejs (embedded template engine) is known as the node js template engine. Therefore, we will use forEach loop inside the html files residing under the views folder by default.

Also read, EJS Template in Node JS with an Example

HTML files are created inside the views folder with .ejs extension which means the HTML files can be displayed on the browser directly without any .html extension just by including the name of the route.

Also read, How to render HTML files in Express JS

Steps to use forEach loop in Node JS template engine

Step 1:- First of all create a file with a .js extension inside the root directory that is your project folder. For eg:-
foreach.js

Step 2:- Now, inside the “foreach.js”, initialize the packages and modules of express js framework with the help of const keyword as shown below.

const express = require('express');

Step 3:- Now, declare another variable “data” to use the functionality of express js modules as shown below.

const data = express();

Step 4:- Now, call the ejs template engine using the set() method and the above “data” variable as shown below.

data.set('view engine','ejs');

Step 5:- Now, set up the route name “profile” using the get() method and the above “data” variable as shown below.

data.get('/profile',(req,resp)=>{
   const skillset = ['C','C++','java','PHP','Node JS','Python','React JS'];
   resp.render('profile',{skillset});
});

Please note that in the above piece of code, I have used an array of skillset=[‘C’,’C++’,’java’,’PHP’,’Node JS’,’Python’,’React JS’] so that it can be displayed in the HTML file using the forEach loop.

Step 6:- Now, create an HTML file (profile.ejs) under the default views folder using the .ejs extension.

Step 7:- Now, paste the below code inside the profile.ejs file as shown below.

<html>
    <title></title>
    <head></head>
    <body>
        <h2>Welcome to the profile page</h2>
        <h3>My Skillsets are given below</h3>
        <ul>
            <% skillset.forEach((val)=>{
                %>
                    <li><%= val %></li>
                <%
            }) %>
        </ul>
    </body>
</html>

Complete Code (foreach.js)

const express = require('express');
const data = express();
data.set('view engine','ejs');
data.get('/profile',(req,resp)=>{
   const skillset = ['C','C++','java','PHP','Node JS','Python','React JS'];
   resp.render('profile',{skillset});
});
data.listen(3000);

Step 8:- Now, go to your browser and type https://localhost:3000/profile and press ENTER and then you can see the output as shown below.

forEach loop in Node JS

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below. Download VS Code editor


Leave a Comment

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311