Skip to main content

Command Palette

Search for a command to run...

What Happens in the Backend When You Click a Button?

Updated
3 min read
What Happens in the Backend When You Click a Button?

When you click a button like “Sign Up,” “Buy Now,” or “Save Changes,” it feels instant — but that one click starts a whole chain of backend events involving HTTP requests, routes, controllers, and databases.
Let’s walk through the journey.


1. The Click Triggers an HTTP Request

Your browser sends a message to the web server called an HTTP request.
It includes:

  • URL – what resource/action you want (/api/users/register)

  • Method – what you want to do (GET, POST, PUT, DELETE)

  • Headers – extra info (auth token, content type)

  • Body – actual data, e.g.:

{
  "name": "David",
  "email": "david@example.com",
  "password": "mypassword123"
}

2. The Request Hits a Route

The backend server (Node.js, Django, Laravel, etc.) receives the request.
A router checks the URL and method, then directs it to the correct handler.

app.post('/api/users/register', userController.registerUser);

Here, the route listens for POST requests at /api/users/register and sends them to registerUser in the controller.


3. The Controller Runs the Logic

The controller decides what to do with the request data:

  • Validate inputs

  • Process logic (e.g., hash password)

  • Interact with the database

  • Return a response

exports.registerUser = async (req, res) => {
  const { name, email, password } = req.body;
  if (!email || !password)
    return res.status(400).json({ message: "All fields required" });

  const hashedPassword = await bcrypt.hash(password, 10);
  const newUser = await User.create({ name, email, password: hashedPassword });
  res.status(201).json({ message: "User created", user: newUser });
};

4. The Database Handles Data

The controller uses a model (via an ORM like Mongoose or Sequelize) to read or write data.

const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  password: String
});
module.exports = mongoose.model('User', userSchema);

Behind the scenes, this creates an SQL-like query:

INSERT INTO users (name, email, password)
VALUES ('David', 'david@example.com', 'hashedpassword');

5. The Server Responds

Once the work is done, the backend sends an HTTP response:

  • Status code (200 OK, 201 Created, etc.)

  • Body (JSON data)

{
  "message": "User created successfully",
  "user": {
    "id": "1a2b3c",
    "name": "David"
  }
}

6. The Frontend Updates

The browser receives the response and updates the interface with JavaScript (React, Vue, etc.):

const res = await fetch('/api/users/register', { method: 'POST', ... });
const data = await res.json();
alert(data.message);

Summary Flow

[User Clicks Button]
        ↓
[Browser sends HTTP Request]
        ↓
[Backend Route → Controller → Database]
        ↓
[Server sends HTTP Response]
        ↓
[Frontend updates UI]

💡 Takeaway

A simple click is actually a conversation between your browser and the server.
Knowing how requests, routes, controllers, and databases interact helps you debug better and design cleaner, more powerful web apps.