A High Level Understanding
What is a Backend, why do we need it, and why not put everything on the frontend?
What is Backend?
Backend is a Centralized Server application running on a computer that listens for request via HTTP, WebSockets, or gRPC from clients (like browsers or app).
Its primary role is to serve content (like static files or JSON data) and accept data from client, managing all necessary logic and interactions in a secure environment rather than on user device.
Real Architecture Flow
Port 80/443
localhost:3001
What beginners think it looks like
Too simplified! This model ignores DNS resolutions, Firewalls, Load Balancers, and Reverse Proxies.
Why Backend?
1) Data Persistence & Management
Backend components are crucial for saving data securely and reliably to a database.
2) Centralization
Provides a central hub to manage user data, application state, and business logic coherently.
Why Not Run Everything on Frontend?
1) Security
Browsers are sandbox environments. They cannot access the file system or sensitive environment variables.
2) CORS Restriction
Browsers powerfully block requests to different domains for critical security reasons.
3) Database Access
Only the backend can efficiently use native database drivers and stably maintain connection pools.
4) Computing Power
Backend can be scaled infinitely with more CPU/RAM, whereas frontend performance depends entirely on the user's device.
Frontend is an open book. Anyone can right-click and open "Inspect" and see every single line of code you've written.
- API KeyIf the secure key is in frontend, a user can easily steal it and exploit it to cost you money or data.
- Business LogicIf discount calculation is on the frontend, a savvy user can change it in their browser before hitting "Buy".
- ConsistencyThe Centralized Backend ensures that two people don't try to magically buy the very last seat on a flight at the exact same millisecond.