Experiment #3 · Node.js Lab
Book Rating and Review System using Node.js and Express.js
Build a simple REST API with Express.js that lets users add books, submit reviews and ratings, and view every book on record — all in one lightweight project.
Objective
The objective of this lab project is to develop a Book Rating and Review System using the Express.js framework of Node.js. The system provides functionality for users to:
- Add a new book
- Submit feedback and ratings for a book
- View all available books along with their ratings and reviews
Software Requirements
| # | Requirement |
| 1 | Node.js |
| 2 | Express.js |
| 3 | Visual Studio Code |
| 4 | Web Browser (Chrome / Edge) |
Step-by-Step Setup
step 1Create the project
mkdir BookReviewSystem, cd BookReviewSystem, npm init -y, npm install express
step 2Create server.js
server.js
const express = require("express");
const app = express();
app.use(express.json());
app.use(express.static("public"));
let books = [];
// Add book
app.post("/book", (req, res) => {
books.push({
title: req.body.title,
review: "",
rating: 0
});
res.send("Book Added");
});
// Add review
app.post("/review", (req, res) => {
let book = books.find(b => b.title == req.body.title);
if (book) {
book.review = req.body.review;
book.rating = req.body.rating;
res.send("Review Added");
} else {
res.send("Book Not Found");
}
});
// Show books
app.get("/books", (req, res) => {
res.json(books);
});
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
step 3Create the public folder
Inside the project folder, create a folder named public, and inside it create a file named index.html.
step 4Create index.html
public/index.html
<!DOCTYPE html>
<html>
<head>
<title>Book Review System</title>
</head>
<body>
<h1>Book Review and Rating System</h1>
<h2>Add Book</h2>
<input id="book" placeholder="Book Name">
<button onclick="addBook()">Add Book</button>
<h2>Add Review</h2>
<input id="title" placeholder="Book Name">
<input id="review" placeholder="Review">
<input id="rating" placeholder="Rating">
<button onclick="addReview()">Submit Review</button>
<h2>All Books</h2>
<button onclick="showBooks()">View Books</button>
<pre id="output"></pre>
<script>
function addBook() {
fetch("/book", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title: document.getElementById("book").value
})
})
.then(res => res.text())
.then(data => alert(data));
}
function addReview() {
fetch("/review", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
title: document.getElementById("title").value,
review: document.getElementById("review").value,
rating: document.getElementById("rating").value
})
})
.then(res => res.text())
.then(data => alert(data));
}
function showBooks() {
fetch("/books")
.then(res => res.json())
.then(data => {
document.getElementById("output").innerHTML =
JSON.stringify(data, null, 2);
});
}
</script>
</body>
</html>
step 5Run the application
node server.js
You should see: Server running at http://localhost:3000
Open your browser and go to http://localhost:3000.
Execution & Output
1. Add Book
Input: Book Name → Java. Click Add Book.
2. Add Review
Input: Book Name → Java, Review → Excellent Book, Rating → 5. Click Submit Review.
3. View Books
Click View Books.
Output
[
{
"title": "Java",
"review": "Excellent Book",
"rating": "5"
}
]
Result: The Book Review and Rating System was completed successfully using Node.js and Express.js. Users can add books, submit reviews and ratings, and view every book already entered through a simple REST API and lightweight front end.
Node.js
Express.js
JavaScript
REST API
Web Development
MCA Lab Project
Thank you for the clear explanation, sir. Very helpful.
ReplyDeleteThank you so much for your wonderful comment, maa! 😊 We're really happy you enjoyed the post. If you found it helpful, please share it with your friends and family. Also, don't forget to click the **Follow** button to stay updated with our latest blog posts. Your support and encouragement mean a lot to us. Thank you for visiting, maa! 🌸
ReplyDelete