Sending HTTP Requests (e.g. Connecting to a Database)Entry 1 of 7
How Frontends Talk to a Database
A React (or Angular) app never connects to a database directly. It sends HTTP requests to a backend API, and the backend reads and writes the database.
Notes
- Code in the browser is visible to anyone. Database credentials in frontend code would be exposed.
- Browsers can't speak database protocols anyway; they speak HTTP.
- The backend (e.g. Node/Express, Go/Echo) validates requests, applies business rules, and talks to the database. The frontend only calls its endpoints.
- Where the request code lives differs by framework. In React, it's common to fetch directly inside a component (in
useEffect), optionally extracted into an API module or custom hook. In Angular, HTTP calls are typically placed in an injectable service; components inject the service and never touchHttpClientor URLs directly. Every Angular example in this chapter follows that convention.
React / Angular app ──HTTP──▶ Backend API ──▶ Database (browser) JSON (server)Quick reference
| Layer | Responsibility |
|---|---|
| Frontend (React/Angular) | Send requests, show data, handle loading and errors |
| Backend API | Authenticate, validate, run business logic |
| Database | Store data |