README.md
md8 kBIn Lab2.zip
Lab 05-08: Book Library REST API
📋 Objective
Create a complete REST API for managing a book library using Express.js, Middleware, MongoDB, and Mongoose. This lab combines concepts from chapters 5, 6, 7, and 8.
🎯 Learning Goals
Chapter 5: Express.js
- Create Express server with routing
- Handle different HTTP methods (GET, POST, PUT, DELETE)
- Use
req.params,req.query, andreq.body - Return appropriate HTTP status codes
Chapter 6: Middleware
- Use
express.json()to parse request bodies - Create custom middleware (logger, error handler)
- Understand middleware order
Chapter 7: Database (MongoDB + Mongoose)
- Create Mongoose models and schemas
- Connect to MongoDB
- Perform CRUD operations (Create, Read, Update, Delete)
- Use query methods (find, findById, findByIdAndUpdate, etc.)
Chapter 8: Complete REST API
- Build a full REST API
- Handle errors properly
- Use async/await with try/catch
📝 Requirements
Your API should handle these endpoints:
Books Endpoints
- GET
/api/books- Get all books (with optional query filters)- Query params:
?category=fiction&minPrice=10
- Query params:
- GET
/api/books/:id- Get a single book by ID - POST
/api/books- Create a new book - PUT
/api/books/:id- Update a book by ID - DELETE
/api/books/:id- Delete a book by ID
Required Features
- ✅ Express server with proper routing
- ✅
express.json()middleware for parsing JSON - ✅ Custom logger middleware (logs method and URL)
- ✅ MongoDB connection using Mongoose
- ✅ Book model with schema (title, author, category, price, inStock)
- ✅ Error handling with try/catch
- ✅ Proper HTTP status codes (200, 201, 404, 400, 500)
- ✅ 404 handler middleware (catch-all for unknown routes)
🚀 Getting Started
Step 1: Setup Project
# Navigate to the lab folder
cd NodeJs_Lab_Chap_05_08
# Initialize npm (creates package.json)
npm init -y
# Install required dependencies
npm install express mongoose corsStep 2: Setup MongoDB
Option A: MongoDB Atlas (Cloud - Recommended)
- Go to mongodb.com/atlas
- Create a free account and cluster
- Get your connection string
Option B: Local MongoDB
- Install MongoDB locally or use Docker
- Connection string:
mongodb://localhost:27017/booklibrary
Step 3: Use the Template
-
Copy the template file:
cp index-template.js index.js cp models/Book-template.js models/Book.js -
Fill in the TODO sections in:
index.js- Main server filemodels/Book.js- Book model/schema
Step 4: Start Your Server
# Update the MongoDB connection string in index.js
# Then start the server:
node index.jsStep 5: Test Your API
Use PowerShell to test:
# Get all books
Invoke-RestMethod -Uri http://localhost:3000/api/books
# Get books by category (query parameter)
Invoke-RestMethod -Uri http://localhost:3000/api/books?category=fiction
# Get books with filters
Invoke-RestMethod -Uri http://localhost:3000/api/books?category=fiction&minPrice=10
# Get single book (replace ID)
Invoke-RestMethod -Uri http://localhost:3000/api/books/BOOK_ID_HERE
# Create a book (Method 1: Single quotes)
Invoke-RestMethod -Uri http://localhost:3000/api/books -Method Post -ContentType "application/json" -Body '{"title":"The Great Gatsby","author":"F. Scott Fitzgerald","category":"fiction","price":12.99,"inStock":true}'
# Create a book (Method 2: Using ConvertTo-Json - recommended)
$body = @{
title = "1984"
author = "George Orwell"
category = "fiction"
price = 10.99
inStock = $true
} | ConvertTo-Json
Invoke-RestMethod -Uri http://localhost:3000/api/books -Method Post -ContentType "application/json" -Body $body
# Update a book (replace ID)
$body = @{price=9.99} | ConvertTo-Json
Invoke-RestMethod -Uri http://localhost:3000/api/books/BOOK_ID_HERE -Method Put -ContentType "application/json" -Body $body
# Delete a book (replace ID)
Invoke-RestMethod -Uri http://localhost:3000/api/books/BOOK_ID_HERE -Method Delete
Or use VS Code REST Client extension with test-api.http file.
✅ Checklist
Express Setup (Chapter 5)
- Express server created
- Server listens on port 3000
- All routes respond correctly
-
req.paramsused for route parameters (:id) -
req.queryused for query parameters (?category=...) -
req.bodyused for POST/PUT data - Correct HTTP status codes returned
Middleware (Chapter 6)
-
express.json()middleware added (before routes!) - Custom logger middleware created (logs method + URL)
- Logger middleware calls
next() - 404 handler middleware added (after all routes)
- Middleware order is correct: json → logger → routes → 404
Database (Chapter 7)
- MongoDB connection established
- Book schema created with required fields
- Book model exported correctly
- All CRUD operations work:
-
Book.find()- Get all books -
Book.findById()- Get one book -
Book.create()- Create book -
Book.findByIdAndUpdate()- Update book -
Book.findByIdAndDelete()- Delete book
-
Error Handling (Chapter 8)
- All async routes use try/catch
- Errors return appropriate status codes
- 404 returned when book not found
- 400 returned for validation errors
- 500 returned for server errors
📚 Hints
Book Schema Fields
{
title: String, required
author: String, required
category: String, required (fiction, non-fiction, science, history, etc.)
price: Number, required, min: 0
inStock: Boolean, default: true
timestamps: true (adds createdAt, updatedAt)
}Query Filtering (GET /api/books)
// Handle query params like: ?category=fiction&minPrice=10
let filter = {};
if (req.query.category) {
filter.category = req.query.category;
}
if (req.query.minPrice) {
filter.price = { $gte: parseFloat(req.query.minPrice) };
}
const books = await Book.find(filter);Middleware Order
// 1. JSON parsing (must be first for POST/PUT)
app.use(express.json());
// 2. Logger middleware
app.use((req, res, next) => { ... });
// 3. Your routes
app.get('/api/books', ...);
app.post('/api/books', ...);
// etc.
// 4. 404 handler (must be last)
app.use((req, res) => { ... });🎓 What You'll Practice
Chapter 5: Express.js
- ✅ Express server setup
- ✅ HTTP routing (GET, POST, PUT, DELETE)
- ✅ URL parameters (
req.params) - ✅ Query parameters (
req.query) - ✅ Request body (
req.body) - ✅ HTTP status codes
Chapter 6: Middleware
- ✅ Built-in middleware (
express.json()) - ✅ Custom middleware (logger)
- ✅ Middleware order
- ✅ 404 error handler
Chapter 7: MongoDB + Mongoose
- ✅ Database connection
- ✅ Schema definition
- ✅ Model creation
- ✅ CRUD operations
- ✅ Query filtering
Chapter 8: Complete REST API
- ✅ Full CRUD API
- ✅ Error handling
- ✅ Async/await patterns
- ✅ API structure and organization
🔍 Testing Tips
- Start with simple routes - Get GET
/api/booksworking first - Test one endpoint at a time - Don't try to build everything at once
- Check the console - Your logger middleware should show all requests
- Use MongoDB Compass - Visual tool to see your database
- Check for errors - Look at console output for connection errors
📖 Expected Book Schema
Your Book model should have:
_id- Auto-generated by MongoDBtitle- String, requiredauthor- String, requiredcategory- String, requiredprice- Number, required, minimum 0inStock- Boolean, default truecreatedAt- Auto-generated timestampupdatedAt- Auto-generated timestamp
📂 Solution Files
If you get stuck, solution files are available for reference:
index-solution.js- Complete server solution with all routes and middlewaremodels/Book-solution.js- Complete Book model with schema definition
Note: Try to complete the lab yourself first! Only check the solution if you're really stuck. The learning comes from solving the problems yourself.
Good luck! 🚀