Frontend and Backend Responsibilities: Node.js in Web Architecture
🎯 Introduction
By the end of this article, you will be able to decide which responsibilities belong in the page, API, and database.
🧱 The frontend handles interaction; the backend handles trusted data processing
Consider changing a nickname. The frontend displays an input, explains the format, sends a request, and updates the page. The backend checks the user's identity, verifies that they can modify the account, validates the nickname, saves the data, and returns the result.
Users can inspect and modify browser code. Disabling a button on the frontend does not prevent the operation: an attacker can still call the API directly. The server must decide whether the change is allowed and whether it was saved successfully.
| Task | Where it belongs | Reason |
|---|---|---|
| Warn that a nickname is too long while typing | Frontend | Let the user adjust it immediately |
| Check the nickname length again | Backend | Requests can bypass the page |
| Determine which account owns the nickname | Backend | The client's claimed identity cannot be trusted |
| Save the user profile | Database, accessed through the backend | Preserve data across requests and restarts |
| Display a success message | Frontend | Update the interface based on the backend result |
⚙️ Node.js runs JavaScript on the server
Node.js is a JavaScript runtime. In a web backend, it receives network requests, applies rules, accesses files or databases, and returns results over HTTP. HTTP is the protocol browsers and servers use to exchange requests and responses.
Browser JavaScript manipulates the page; Node.js can access server files and networking capabilities. It has no page DOM, and it should never return server database passwords to the frontend.
Node.js is neither a database nor a framework. A database persists and queries records. Frameworks such as Express and Koa help organize APIs, which still run in Node.js.
🔗 Understand the roles in a data request
When a user opens their profile, the page requests /api/users/1. The Node.js endpoint checks permissions, queries the database for the user record, selects the fields that may be exposed, and returns JSON. The browser receives the JSON and renders the name.
Browser page → HTTP request → Node.js endpoint → Database
Browser page ← JSON response ← Node.js endpoint ← User record
The arrows show data flow; they do not imply three separate machines. During development, these programs can all run on your computer. In production, they may run separately.
A file server can serve static pages and images directly, without querying a database on every visit. Only requests that need business rules or dynamic data enter the corresponding endpoint.
🛠 A shared example environment
This course uses Node.js 24.19.0 and Express 5.3.0, with .mjs files and import/export. For Node.js installation, follow Environment Setup (Chinese).
Install Express in the backend-demo practice directory:
npm install express@5.3.0
We first use the built-in HTTP module to understand requests and responses, then use Express for subsequent examples. The framework courses cover the complete Express API and Egg directory conventions. Here, we use them to address practical questions about parameters, permissions, data processing, and deployment.
❓ Questions and Answers
Q1: Must the frontend and backend use different languages?
No. Both the frontend and a Node.js backend can use JavaScript. They differ in where they run, their privileges, and their responsibilities.
Q2: Can the browser connect directly to the application's database?
This is usually inappropriate. Database credentials must not be exposed to the browser. The backend should provide endpoints with validation and access control.
Q3: Is Node.js suitable for every kind of computation?
It is commonly used for network APIs and I/O. Long-running synchronous computation blocks request handling. Use worker threads or a separate computing service instead of putting an expensive loop directly in an endpoint.
🧪 Practice
For “delete my avatar,” list the page interactions, backend checks, and data that must be saved or deleted.
// Write your code or record your verification results here
