Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active February 2, 2021 05:48
Show Gist options
  • Save sandrabosk/8f7da0d9bb9753c78be866f96221b574 to your computer and use it in GitHub Desktop.
Save sandrabosk/8f7da0d9bb9753c78be866f96221b574 to your computer and use it in GitHub Desktop.

The MVC Pattern

  • Every time a request is made on the server, a Controller handles it.
  • This Controller will communicate with Models.
  • Models will read and write data directly to a database.
  • When the Controller has all information, it can pass that data to the View.
  • The View generates a Response which is a HTML page rendered to a client (in a browser).

Example:

router.get('/product-list', (req, res) => {
  // this CONTROLLER is...
  Product.find() // ... asking for data from the Product MODEL and ...
    .then(productsFromDB => {
      const data = { productsFromDB };
      res.render('products/list', data); //  ... sending a VIEW to the client
    })
    .catch(error => console.log(error));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment