- 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));
});