Lab Work 1 (1)
Lab Work 1 (1)
js
Objective
In this lab, you will create a simple Node.js application with CRUD (Create, Read, Update, Delete)
functionality using Express and EJS. This lab will demonstrate how to manage data dynamically and
serve HTML views through EJS.
Instructions
npm init -y
crud-lab/
├── node_modules/
├── public/
│ └── style.css
├── views/
│ ├── index.ejs
│ ├── add.ejs
│ ├── edit.ejs
├── server.js
├── package.json
└── package-lock.json
app.use(express.static('public'));
// Routes
});
res.render('add');
});
items.push(newItem);
res.redirect('/');
});
item.name = name;
item.description = description;
res.redirect('/');
});
res.redirect('/');
});
o views/index.ejs:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Items</title>
</head>
<body>
<h1>Item List</h1>
<ul>
<li>
<button type="submit">Delete</button>
</form>
</li>
</ul>
</body>
</html>
o views/add.ejs:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Item</title>
</head>
<body>
<label for="name">Name:</label>
<label for="description">Description:</label>
<br>
</form>
</body>
</html>
o views/edit.ejs:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Item</title>
</head>
<body>
<h1>Edit Item</h1>
<label for="name">Name:</label>
<br>
<label for="description">Description:</label>
<br>
</form>
</body>
</html>
o public/style.css:
body {
margin: 20px;
ul {
list-style-type: none;
li {
margin: 10px 0;
a{
margin-right: 10px;
button {
background-color: red;
color: white;
border: none;
cursor: pointer;
node server.js
● Understand and implement basic CRUD functionality using Node.js and Express.
● Create dynamic HTML views using the EJS templating engine.
● Learn to manage routes and handle HTTP methods (GET, POST).
● Practice styling with basic CSS.
Expected Outcome: