Hey fellow developers! π
In my last post, I mainly focused on upgrading the frontend UI and UX. While it looked much better, it still lacked the full-stack feel of a real application.
So this post is all about the backend β where things actually started getting serious.
β Why Adding a Backend Was Necessary
Initially, I wanted to add a backend to practice and improve my backend skills. But from the appβs perspective, a backend was essential for:
- π Large amounts of quote data
- π€ Future AI scalability
- ποΈ Category-based filtering
Handling all of this purely on the frontend wouldβve been messy and unrealistic.
With a backend? Clean and manageable.
βοΈ Backend Setup & Structure
I used a simple and efficient stack:
- Node.js + Express β REST API
- MongoDB (Compass locally) β Database
- Render β Backend deployment (more on this next post π)
π Project Structure
/
βββ index.html
βββ style.css
βββ script.js
βββ backend/
β βββ server.js
β βββ models/
β βββ Quote.js
βββ README.md
π§© Schema Creation
At the core of the backend was the quote schema:
const quoteSchema = new mongoose.Schema({
text: { type: String, required: true },
author: { type: String, required: true },
category: { type: String, required: true }
});
This schema made everything possible.
Hereβs how it works:
- A category button is clicked on the frontend
- A request is sent to the backend
- The backend filters quotes by category
- The text + author are sent back
- The quote is displayed to the user
This is where the magic of schemas really clicked for me β¨
π Connecting Backend to Frontend
Once the backend was ready, it was time to connect everything.
Inside index.js, I created a fetchQuotes() function:
async function fetchQuotes(category = null) {
try {
let url = category
? `${API_URL}/quotes/category/${category}`
: `${API_URL}/quotes`;
const res = await fetch(url);
quotes = await res.json();
usedQuotes = [];
usedColours = [];
newQuote();
} catch (err) {
console.error("Backend not available:", err);
}
}
fetchQuotes();
For now, API_URL points to localhost.
Later, this will switch to a deployed backend URL β which leads perfectly into the next post π
β Final Result (Locally)
Andβ¦ voila! π
Everything was working perfectly on my local system:
- Quotes fetched from the backend
- Categories filtered correctly
- No more hardcoded data
All that was left was deployment.
π Whatβs Coming Next
In the next post, Iβll cover:
- π Frontend deployment β GitHub Pages
- βοΈ Backend deployment β Render
- π§ͺ Live-data debugging
- π MongoDB Compass β MongoDB Atlas
Be stoked for the next update β see you soon! ππ₯
Top comments (0)