Passing Data to EJS
We can pass data using EJS tags
rolldice.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RollDice</title>
</head>
<body>
<h1>dice value: <%= diceVal%></h1>
</body>
</html>
index.js
const express = require("express");
const app = express();
const path = require("path");
const port = 8080;
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.get("/rolldice",(req,res)=>{
let diceVal=Math.floor(Math.random()*6)+1;
res.render("rolldice.ejs",{diceVal});
})
app.listen(port, () => {
console.log(`server is listening on port ${port}`);
});



Comments
Post a Comment