APIs, REST, CRUD, Middleware
Explained
From direct DB access to secure
backend design
What is an API?
• - API = Application Programming Interface
• - Lets systems talk to each other
• - Your frontend sends HTTP requests to the
backend API
• - Backend talks to the database
Does API Directly Access DB?
• - NO! Clients never talk directly to your
database
• - API acts as a gatekeeper
• - Validates input, checks auth, queries DB
safely
• - Prevents exposing credentials or raw queries
Direct DB Access vs API
• ❌ Direct DB Access:
• - Exposes DB credentials
• - No validation or auth
• - Risky for SQL Injection
• ✅ API Access:
• - Client → API → DB
• - Secure queries with validation & logic
What are REST APIs?
• - REST = Representational State Transfer
• - Standard way to build APIs over HTTP
• - Uses URLs + HTTP methods: GET, POST, PUT,
DELETE
• - Data is exchanged as JSON
How to Create REST APIs
• - Use a backend framework (Express, Django,
Rails)
• - Define routes: /api/users, /api/products
• - Use CRUD operations with variables (e.g., id,
name)
• - Send JSON responses
Do APIs Connect Frontend &
Backend?
• - YES! Frontend calls API endpoints
• - API handles business logic
• - API talks to DB behind the scenes
• - Keeps DB private & secure
CRUD Queries with Variables
• - CRUD = Create, Read, Update, Delete
• - Each operation uses a query template with
dynamic variables
• - Example: SELECT * FROM users WHERE id = ?
• - Avoids hardcoding values
What are Middlewares?
• - Functions that run between request &
response
• - Read/modify request or response
• - Call next() to pass to next middleware/route
• - Used for parsing, auth, logging, validation
Middleware with GET & POST
• - Middleware works with ALL HTTP methods
• - GET: logging, auth, caching
• - POST: body parsing, validation, auth
• - next() passes control down the chain
Why Use next() in POST?
• - POST requests often need multiple steps
• - Example: parse JSON → validate → check
duplicates
• - Each step is a middleware with next()
• - Final handler saves data & sends response