MVC with MEAN

Rachna Singhal
3 min readMay 28, 2018

--

Everyone has told you about MVC framework and how it is an industry standard, let me use the simplest language to explain the basics of it for you to write your first MVC application in MEAN.

You must have heard this acronym M.V.C way too many times, What does it stand for? MVC stand for Model, View, Controller. Now the reason to use MVC is to separate logic from db and security. We don’t want our front-end to directly hit the database, because that would expose our database to every client using our application. To avoid this we introduce a middleware called the Controller to interact with the DB aka Model, and send the response to the View.

MODEL

This handles all the data related aspect of the application. This is to get information/data from the database to then be processed by the views and controllers

CONTROLLER

This is an interface between Model and View components, used to process all the business logic and routes (requests), manipulate data retrieved from Model component and then pass the processed data to Views to render the final output.

VIEW

The View is nothing but the beautiful UI you see in the front-end.

This was an overview of what MVC is. Moving forward with our last post (see this for reference), we already have an application, now we would fit our app into MVC framework

Application structure

Since Controllers have a dual role of routing and business logic, it is a good idea to divide this part into two different aspects for more readability. So in our case we will have routes and controllers. The routes folder will have basic routes like

Routes

The routes will just have the declaration of API endpoint which will transfer the flow of control to your controllers. The controllers will talk to Model to retrieve information. Once the information is fetched it responds the processed information back to the requester.

Controllers for user endpoint

The Models are made generic and based on the table/collection name depending on your database. This gives us the flexibility to add a new table/collection just by simply copying a model file and renaming it. For eg.

User Model

The above model will perform all basic operation to a specific table. To add a new table, copy this file, rename it and change the tablename variable in line 1 and you have all basic operations for a new table.

ADVANTAGES

  • This makes your application more robust.
  • In the scenario where you decide to change the database for any reason, all you have to replace is the models folder with the new models folder which will have the new database handling, without touching the logic.
  • This also keeps logic and data separated so as to independently manage each component.
  • We have a separate connection file to connect to the database, in the case of your database changes but the query would remain same, for eg from postgres to mysql, then all you have to change is the way you connect to the database, the rest remains same.
  • There is another file maintained in db_config.json which is if we want to change the location of the database.

This makes your application with a long sightedness.

--

--

Rachna Singhal

Programmer, Sports Enthusiast, Theatre Lover, Blogger, Traveller, bibliophile