Use EJS as Template Engine in Node.js Use EJS as Template Engine in Node.js Use EJS as Template Engine in Node.js
Use EJS as Template Engine in Node.js Use EJS as Template Engine in Node.js Use EJS as Template Engine in Node.js Use EJS as Template Engine in Node.js
/////////////////////////////// ////////////////////////////////////////////
command line
npm install ejs
etemp.js
// Set express as Node.js web application
// server framework.
// To install express before using it as
// an application server by using
// "npm install express" command.
const express = require('express');
const app = express();
// Set EJS as templating engine
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
// The render method takes the name of the html
// page to be rendered as input.
// This page should be in views folder
// in the root directory.
let data = {
name: 'Akashdeep',
hobbies: ['playing football', 'playing chess', 'cycling']
}
res.render('home', { data: data });
});
const server = app.listen(8080, function () {
//console.log('listening to port 4000')
});
///// view/home.ejs /////////////////
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<style type="text/css" media="screen">
body {
background-color: skyblue;
text-decoration-color: white;
font-size: 5em;
}
</style>
</head>
<body>
Hobbies of <%=data.name%> are:<br />
<ul>
<% data.hobbies.forEach((item)=>{%>
<li>
<%=item%>
</li>
<%});%>
</ul>
</body>
</html>
Comments
Post a Comment