π§ νμ€νμΌλ‘ μ ν: λ°±μλ λ° REST API ꡬμΆ
Source: Dev.to

Hey fellow developers! π
μλ
νμΈμ, κ°λ°μ μ¬λ¬λΆ! π
In my last post I focused on upgrading the frontend UI and UX. It looked much better, but it still lacked the fullβstack feel of a real application. This post is all about the backend β where things actually started getting serious.
μ§λ κΈμμλ νλ‘ νΈμλ UIμ UXλ₯Ό κ°μ νλ λ° μ§μ€νμ΅λλ€. ν¨μ¬ 보기 μ’μμ‘μ§λ§ μ€μ μ ν리μΌμ΄μ
μ νμ€ν κ°κ°μ μμ§ λΆμ‘±νμ£ . μ΄λ² κΈμ λ°±μλμ κ΄ν λ΄μ©μΌλ‘, 본격μ μΌλ‘ μ§μ§ν΄μ§κΈ° μμν λΆλΆμ
λλ€.
Why Adding a Backend Was Necessary
λ°±μλλ₯Ό μΆκ°ν΄μΌ νλ μ΄μ
Initially I wanted a backend to practice and improve my backend skills. From the appβs perspective, a backend was essential for:
μ²μμ λ°±μλ μ€λ ₯μ μ°μ΅νκ³ ν₯μμν€κΈ° μν΄ λ°±μλλ₯Ό λ§λ€κ³ μΆμμ΅λλ€. μ± μ
μ₯μμ λ°±μλλ λ€μκ³Ό κ°μ μ΄μ λ‘ νμμμ΅λλ€:
- π Large amounts of quote data
- π λ°©λν μΈμ©κ΅¬ λ°μ΄ν°
- π€ Future AI scalability
- π€ ν₯ν AI νμ₯μ±
- ποΈ Categoryβbased filtering
- ποΈ μΉ΄ν κ³ λ¦¬ κΈ°λ° νν°λ§
Handling all of this purely on the frontend would have been messy and unrealistic. With a backend the solution becomes clean and manageable.
μ΄ λͺ¨λ μμ
μ νλ‘ νΈμλλ§μΌλ‘ μ²λ¦¬νλ€λ©΄ 볡μ‘νκ³ λΉνμ€μ μΌ μλ°μ μμ΅λλ€. λ°±μλλ₯Ό λλ©΄ ν΄κ²°μ±
μ΄ κΉλνκ³ κ΄λ¦¬νκΈ° μ¬μμ§λλ€.
Backend Setup & Structure
λ°±μλ μ€μ λ° κ΅¬μ‘°
Stack
μ€ν
- Node.js + Express β REST API
- Node.js + Express β REST API
- MongoDB (Compass locally) β Database
- MongoDB (λ‘컬 Compass) β λ°μ΄ν°λ² μ΄μ€
- Render β Backend deployment (more on this in the next post)
- Render β λ°±μλ λ°°ν¬ (λ€μ κΈμμ μμΈν λ€λ£Ήλλ€)
Project Structure
νλ‘μ νΈ κ΅¬μ‘°
/
βββ index.html
βββ style.css
βββ script.js
βββ backend/
β βββ server.js
β βββ models/
β βββ Quote.js
βββ README.md
Schema Creation
μ€ν€λ§ μμ±
The core of the backend is the quote schema:
λ°±μλμ ν΅μ¬μ μΈμ©κ΅¬ μ€ν€λ§μ
λλ€:
const quoteSchema = new mongoose.Schema({
text: { type: String, required: true },
author: { type: String, required: true },
category: { type: String, required: true }
});
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 and author are sent back.
ν μ€νΈμ μ μλ₯Ό λ°νν©λλ€. - The quote is displayed to the user.
μΈμ©κ΅¬κ° μ¬μ©μμκ² νμλ©λλ€.
Connecting Backend to Frontend
λ°±μλμ νλ‘ νΈμλ μ°κ²°
Inside script.js (formerly index.js) I created a fetchQuotes() function:
script.js(μ΄μ index.js) μμ fetchQuotes() ν¨μλ₯Ό λ§λ€μμ΅λλ€:
async function fetchQuotes(category = null) {
try {
const url = category
? `${API_URL}/quotes/category/${category}`
: `${API_URL}/quotes`;
const res = await fetch(url);
const quotes = await res.json();
usedQuotes = [];
usedColours = [];
newQuote();
} catch (err) {
console.error("Backend not available:", err);
}
}
fetchQuotes();
For now API_URL points to localhost. Later it will switch to the deployed backend URL, which leads perfectly into the next post.
νμ¬ API_URLμ localhostλ₯Ό κ°λ¦¬ν€κ³ μμ΅λλ€. μΆν λ°°ν¬λ λ°±μλ URLλ‘ λ°λ μμ μ΄λ©°, μ΄λ λ€μ κΈκ³Ό μμ°μ€λ½κ² μ°κ²°λ©λλ€.
Final Result (Locally)
μ΅μ’ κ²°κ³Ό (λ‘컬)
Everything works perfectly on my local system:
λ΄ λ‘컬 νκ²½μμ λͺ¨λ κ²μ΄ μλ²½ν λμν©λλ€:
- Quotes are fetched from the backend.
μΈμ©κ΅¬κ° λ°±μλμμ κ°μ Έμμ§λλ€. - Categories are filtered correctly.
μΉ΄ν κ³ λ¦¬κ° μ¬λ°λ₯΄κ² νν°λ§λ©λλ€. - No more hardβcoded data.
νλμ½λ©λ λ°μ΄ν°κ° λ μ΄μ μμ΅λλ€.
All thatβs left is deployment.
μ΄μ λ¨μ κ²μ λ°°ν¬λΏμ
λλ€.
Whatβs Coming Next
λ€μμ λ€λ£° λ΄μ©
In the next post Iβll cover:
λ€μ κΈμμλ λ€μμ λ€λ£° μμ μ
λλ€:
- π Frontend deployment β GitHub Pages
- π νλ‘ νΈμλ λ°°ν¬ β GitHub Pages
- βοΈ Backend deployment β Render
- βοΈ λ°±μλ λ°°ν¬ β Render
- π§ͺ Liveβdata debugging
- π§ͺ μ€μκ° λ°μ΄ν° λλ²κΉ
- π MongoDB Compass β MongoDB Atlas
- π MongoDB Compass β MongoDB Atlas
Stay tuned for the next update! ππ₯
λ€μ μ
λ°μ΄νΈλ₯Ό κΈ°λν΄ μ£ΌμΈμ! ππ₯