Curd Operation with static array in node js and ejs file
index.js
const express = require('express');
const port = 8001;
const app = express();
const path = require('path');
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(express.static(path.join(__dirname,'assets')));
app.use(express.urlencoded());
var student = [
{
name : "Jivan",
age : 22,
id: Math.round(Math.random()*1000)
},
{
name : "keval",
age : 33,
id: Math.round(Math.random()*1000)
}
]
app.use(function(req,res,next){
req.userName = "Manish";
console.log("middleware one");
next();
})
app.use(function(req,res,next){
if(req.userName == "Manish"){
next();
}
else{
next();
return res.redirect('/login');
}
})
app.get('/', async (req,res)=>{
return res.render('index',{
title : "index page",
student : student,
userName : req.userName
});
})
app.post("/insertStudent", async(req,res)=>{
// console.log(req.body.name);
var name = req.body.name;
var age = req.body.age;
var st = {
name : name,
age: age,
id: Math.round(Math.random()*1000)
}
student.push(st);
return res.redirect('/')
})
app.get("/updateRecord/:id", function(req,res){
// console.log(req.params.id);
let index = student.findIndex(st=>st.id==req.params.id);
return res.render('update',{
singleOb : student[index],
})
})
app.post("/editStudent", function(req,res){
// let index = student.findIndex(st=>st.id==req.body.id);?
for(var i=0; i<student.length; i++){
if(student[i].id==req.body.id){
student[i] =req.body;
}
}
// student[index].name = req.body.name
// student[index].age = req.body.age
// student[index] =req.body;
//student[index].id =req.body.stId;
return res.redirect('/');
})
app.get("/deleteRecord/", function(req,res){
// console.log(req.params.ddd);
let index= student.findIndex(st => st.id ==req.query.stid);
// console.log(index);
student.splice(index,1);
return res.redirect('/')
})
// app.get("/deleteRecord/:ddd", function(req,res){
// // console.log(req.params.ddd);
// let index= student.findIndex(st => st.id ==req.params.ddd);
// // console.log(index);
// student.splice(index,1);
// return res.redirect('/')
// })
//<a href="/deleteRecord/<%= s.id %>">delete</a></td>
app.get("/login", function(req,res){
return res.render('Login');
})
app.listen(port, function(err){
console.log("server running on port: ",port);
})
Views===> index.ejs
<html>
<head>
<title><%= title %></title>
<link href="/css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Index page</h1>
<img src="/images/pic1.jpg">
<h1>This is middleware data=<%= userName %></h1>
<form method="post" action="/insertStudent">
<table border="1">
<tr>
<td>Enter Name</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter age</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Add Record"/></td>
</tr>
</table>
</form>
<table border="1">
<tr>
<td>Id</td>
<td>Name</td>
<td>age</td>
<td>action</td>
</tr>
<% for(var s of student){ %>
<tr>
<td><%= s.id %></td>
<td><%= s.name %></td>
<td><%= s.age %></td>
<td><a href="/deleteRecord/?stid=<%= s.id %>">delete</a> ||
<a href="/updateRecord/<%= s.id %>">Update</a></td>
</tr>
<% } %>
</table>
<% if(false){ %>
<h1>This is true portion</h1>
<% }else { %>
<h1>This is false portion</h1>
<% } %>
<% for(var i=1; i<=10; i++){ %>
<h1><%= i %></h1>
<% } %>
</body>
<script type="text/javascript" src="/js/script.js"></script>
</html>
update.js
<form method="post" action="/editStudent">
<input type="text" name="id" value="<%= singleOb.id %>"/>
<table border="1">
<tr>
<td>Enter Name</td>
<td><input type="text" name="name" value="<%= singleOb.name %>"/></td>
</tr>
<tr>
<td>Enter age</td>
<td><input type="text" name="age" value="<%= singleOb.age %>"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Edit Record"/></td>
</tr>
</table>
</form>
Last Updated on : 15th Jan 2025 13:22:22 PM
Latest Update
- JavaScript Interview Question MongoDb Command Curd Operation Node MonogoDb Express EJSC Practice Question Example Data Structure with CPPCurd Operation with static array in node js and ejs filejava practice questionCurd operation with localstorage in javascriptComplete curd operation in react with json server and axios and router-domCPP Practice Question Java Pattern Program