Model-View-Controller(MVC) architecture for Node applications
Last Updated :
07 Jan, 2025
MVC is an acronym for Model-View-Controller. It is a design pattern for software projects. It is used majorly by Node developers and by C#, Ruby, PHP framework users too. In MVC pattern, application and its development are divided into three interconnected parts. The advantage of this is it helps in focusing on a specific part of the application name, the ways information is presented to and accepted from, the user.
It helps in allowing for efficient code reuse and the parallel development of the application. Even if the project structure might look a little different than an ideal MVC structure, the basic program flow in and out the application remains the same. In this post, the program flow between these components of an application will be shown by creating a demo application.
First, lets get through with what these parts of the application mean and what functions they perform.
The MVC architecture provides a structured way to build scalable applications.
Model
Model represents the structure of data, the format and the constraints with which it is stored. It maintains the data of the application. Essentially, it is the database part of the application.
View
View is what is presented to the user. Views utilize the Model and present data in a form in which the user wants. A user can also be allowed to make changes to the data presented to the user. They consist of static and dynamic pages which are rendered or sent to the user when the user requests them.
Controller
Controller controls the requests of the user and then generates appropriate response which is fed to the viewer. Typically, the user interacts with the View, which in turn generates the appropriate request, this request will be handled by a controller. The controller renders the appropriate view with the model data as a response.
So, to sum it up:
- Model is data part.
- View is User Interface part.
- Controller is request-response handler.

MVC architecture
Understanding Node Application
Creating Node Project:
Now, lets get started with the application. npm init is used here to generate a package.json and app.js file.

npm-init
As the name suggests, there are three folders, called models, views, controllers which will help in the mvc architecture implementation. npm is used to install the basic npm packages to get started.

npm-package-install-snip
Project Structure:
The project structure looks like this.

mvc-project-structure snip
Explanation:
- As you can see, there is aroutes folder, which will serve as controllers.
- Then there is a models folder, in which we have a user model.
- A views folder, which have our views with an extension of .handlebars. Be noted that handlebars is a templating engine which means that it has the ability to generate the pages by filling in the templating that we create.
Implementing MVC:
Now, let’s get down to showing how the MVC pattern gets implemented for the process of login and registering as a user in this demo LoginApp.
- Write node app.js to start the application. The application will start if everything is right otherwise try to debug the app using stackoverflow and things like that.
- Open the app in your browser. If you have forked and using my github repo, then navigate to localhost:3000 in your browser and you will see the app running.When you open the app in your browser, your app.js file will say to itself something like :”Oh! The browser has requested localhost:3000/, so lets look for the file to serve if this route is hit. It looks for this code:

App use route appjs
- . It tells the app that use the variable routes if ‘/’ is requested. Then it looks for the routes variable. It finds it in the app.js file here:

Routes requires
- . Now it looks in the gfgIndex.js file in the routes folder of our node app to look for the code to execute when the ‘/’ route is hit. It finds the following code.

GfgIndexjs routes file
- This code basically says that render the index.hanblebars if the user is logged in. To check if the user is logged in, it runs the function ensureAuthenticated. This function basically says that if the user is logged in, render the index.handlebars file else redirect the user to the /users/login route.

GfgUsersjs login route snip
- This code tells the app to render the index.handlebars file when the GET ‘/’ is called. So, now it goes to the views folder and look for the index.handlebars and renders it to the user. This is how the views-controller part of the architecture works in the app. I believe the above program flow is clear to the reader.
- Lets navigate to http://localhost:3000/users/register. So, the app breaks the route into two pieces:/users and /register and asks itself something like “Oh okay! The users wants to see /users route and then /register . The app looks for the ‘/users’ in its app.js file and finds it here.

App use route appjs
- Then it looks for the ‘users’ variable to use when /users path is hit which can be found in the app.js file:

Routes requires
- So, it goes to the gfgUsers.js file in the routes folder and looks for the route /register. Note that the /users/register finds itself in gfgUsers.js file as /register. It asks the browser to render ‘register.handlebars’ file. This is the view-controller arch. implementation going on. Second Part of Registration Now, lets register a new user.

Register a new user snap. image:https://media.geeksforgeeks.org/wp-content/uploads/register-a-new-user-snap.png
- After clicking on submit, the data is taken, POSTed to the ‘/register’ route for processing. This controller validates the incoming data for errors, then creates a new variable called newUser with the User modelled with all the data and the calls save() on it to actually save the data to the user.

New user console logged into the terminal
- After the user is created, the ‘/register’ controller asks the browser to redirect the user to ‘/login’ page. This is the model-view-controller architecture implementation.
You can find the entire code used in this article here. Fork, clone and run.
Similar Reads
MVC (Model View Controller) Architecture Pattern in Android with Example
Developing an android application by applying a software architecture pattern is always preferred by the developers. An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing. It makes the task easy for developers to maintain the softwar
7 min read
Node.js Web Application Architecture
Node.js is a JavaScript-based platform mainly used to create I/O-intensive web applications such as chat apps, multimedia streaming sites, etc. It is built on Google Chromeâs V8 JavaScript engine. Web ApplicationsA web application is software that runs on a server and is rendered by a client browser
3 min read
How Web Works - Web Application Architecture for Beginners
For many decades we are browsing our favorite websites on the internet and getting a quick response whatever we want... but do you ever try to know how each part of the application works together and how is the request being processed behind the scene? If you're a little bit familiar with tech then
10 min read
Explain the architectural structure of Ember.js applications
Ember.js is a JavaScript framework for building web applications. It uses the Model-View-View Model (MVVM) architectural pattern, which separates the application's data model, user interface, and logic into distinct components. Architectural structure: The architecture of an Ember.js application con
5 min read
How to Design a Web Application - A Guideline on Software Architecture
Have you ever tried to prepare Pizza at home? (yes!!! we're talking about your favorite food...) What will happen if you don't make good dough for your pizza base? Surely the whole Pizza base will be spoiled and you can't continue with preparing your favorite dish. Whether you're making a pizza or b
11 min read
What is a Clean Frontend Architecture?
In recent years, front-end development has taken a massive leap. The rise of frameworks and libraries has transformed the way developers approach problems and create exquisite user experiences. All this makes it essential to write good code in order to make the application more modular, scalable, an
12 min read
MVP (Model View Presenter) Architecture Pattern in Android with Example
In the initial stages of Android development, learners do write codes in such a manner that eventually creates a MainActivity class which contains all the implementation logic(real-world business logic) of the application. This approach of app development leads to Android activity gets closely coupl
12 min read
How to Build a Microservices Architecture with NodeJS?
Microservices architecture allows us to break down complex applications into smaller, independently deployable services. Node.js with its non-blocking I/O and event-driven nature is an excellent choice for building microservices. Microservices architecture can involve designing the application as a
3 min read
How to Combine Multiple Node.js Web Applications ?
As web applications become increasingly complex, it's not uncommon for organizations to have multiple Node.js applications serving different purposes. However, managing and integrating these applications can present challenges. In this article, we will explore various approaches and best practices f
3 min read
Explain the Architecture Overview of Angular ?
Angular is a client-side front-end framework developed by a team of developers at Google based on Typescript. It is used for building dynamic and single-page web applications (SPAs). Also, Angular is a versatile framework for building web applications and offers a wide range of features and tools to
7 min read