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.