NodeJS & MongoDB API revisited — Following the MVC Pattern

Emrich-Michael Perrier
4 min readSep 3, 2021

--

2 weeks ago I published a blog post that was a tutorial on building a NodeJs/ExpressJs/MongoDB API. I think that provided a really good basis of understanding, however I found a flaw. It didn’t follow the principle of having 1 thing handle 1 action in programming. The routes.js file was messy and contained all the routes and logic for the API. I wanted to fix this and decided to do this following the MVC pattern. The MVC pattern (Model, View, Controller) is popular amongst backend programming. It was first introduced to me in the FlatIron School when I was learning how to use Ruby/Sinatra/Ruby on Rails. To breakdown this patter you have…

Model — The object you are creating with its attributes. An example would be a word model. A word is the “thing” and it has attributes like definition, synonyms, origin, etc…

View — This is what the user sees. It’s the view. Displays the information.

Controller — The logic of the backend. It decides what to do for each action for each HTTP Verb. DELETE controller action will delete the instance of the object. CREATE will make one. GET with id will find a show a specific instance. GET with all with show all instances. UPDATE or PATCH or PUT will update/change the instance attributes that are desired. The controller decides how this will all work.

Here I am going to explain how to setup a MVC style NodeJS/MongoDB API. Hopefully this will also show you why this format is better for readability and organization of code.

File Formatting — routes.js

First you’re only going to have a single routes.js file rather than a route file for each model. Mine isn’t finished yet but it looks something like this:

You can see for each HTTP verb action you have a call to the UsersController which has a corresponding function to be called. Ex: The POST route (ln 14 router.post) calls the store function in the UsersController to create the user. This then leads us to the controller…

File Formatting — Controllers Directory

In your server directory you’re gong to create a “controllers” directory (projectdir/server/controllers). Then you should make a controller for each model so we have a UsersController for the User model.

Here is a snippet of my UsersController. You’re going to create one of these async functions for each Action. This specific “index” function will pull all the users from the database and display their corresponding comments. If the controller has any trouble than the error message will be displayed with the error code status 500 (Which means there is an internal server error which would be the creators fault, not the user).

You’re going to make each of these functions for each action and display the data you want to show.

File Formatting — Models Directory

Just like before you’re going to have a directory for models (projectdir/server/models). Each model file will have the schema and will be the same as I showed in my last post. If you need a refresher it will look something like this:

That’s it!

I hope this quick guide helped! These small changes make your code organized in a better way and help readability! I can’t leave a link to this code repo because it is private but I will leave a link to the repo that helped me understand these concepts here. I’d highly recommend you checking it out! Next I’ll address the model relationships for mongoDB using the same API structure so stay tuned! Hope this helped!

Happy Coding! :D

--

--

Emrich-Michael Perrier
Emrich-Michael Perrier

Written by Emrich-Michael Perrier

Fulltime Frontend Developer, Anime nerd and lifter of heavy weight in competition

No responses yet