Node.js Course
Node.js
/
Intermediate

MVC Architecture

Definition

Model-View-Controller. A software design pattern that divides the related program logic into three interconnected elements to separate internal representations of information from the ways information is presented to and accepted from the user.

Explain Like I'm New

A restaurant. 1. The View is the Menu and the Dining Room (what the user sees). 2. The Controller is the Waiter. The user tells the Waiter what they want. The Waiter handles the request. 3. The Model is the Kitchen/Chef. The Waiter gives the order to the Chef. The Chef talks to the database, prepares the data, and hands it back to the Waiter, who carries it out to the View.

Real World Example

In an Express/React app: React is the View. The Express Route is the Controller. Mongoose is the Model. The Route (Controller) validates the HTTP request, calls Mongoose (Model) to fetch the user, and sends the JSON back to React (View).

Common Use Cases

  • •Organizing large codebases
  • •Separation of Concerns

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Which part of MVC interacts directly with the Database?

intermediate

  • What is the main responsibility of the Controller?

Flash Cards

Question

Which part interacts with DB?

Click to reveal answer
Answer

The Model.

Question

Controller responsibility?

Click to reveal answer
Answer

It is the middleman. It receives the HTTP request from the View, validates the inputs, asks the Model for data, and then decides what Response to send back. It shouldn't contain complex database queries itself.