How to render variables in your ejs page?

Here, we will write a program to render variables into our ejs page using node js. For this we need to create two files, one app.js which will contain all the libraries and send the variable to ejs page and another will be index.ejs file where we will render the variables.

app.js file in root directory

const express = require('express');
const ejs = require('ejs');

const app = express();
app.set('view engine', 'ejs');

app.get('/', (req, res) => {
    const name = 'rahul';
    res.render('index', { name: name });
  });

index.ejs file in views directory

<!DOCTYPE html>
<html>
  <head>
    <title>My EJS Page</title>
  </head>
  <body>
    <h1>Hello <%= name %>!</h1>
  </body>
</html>