DEV Community

Cover image for Understanding MVC pattern in Nodejs

Understanding MVC pattern in Nodejs

Emmanuel Etukudo on January 06, 2021

This is part two of the Test-driven Development with Nodejs, Express, Mongoose & Jest, in part one, we set up our development environment and r...
Collapse
 
betula profile image
Slava Birch • Edited

Great article!
I will allow myself to make little one suggestion to your article for will make it a diamond.
To enable syntax highlighting in a code section, you needs to open your code block as

```javascript
code
```
Its will look likes

const app = exepress();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
algodame profile image
Chiamaka Ojiyi

Very helpful. Will try it out

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

Thank you so much for this input, I will update the article shortly.

Collapse
 
pemo11 profile image
Peter

Great article and a good example with a modern approach. I learned a lot. So thanks for putting all this together.

Few questions though:

Does the updateArticle API really works? I think there is a parameter mismatch between updateArticle() in ArticleService.js and apiUpdateArticle in article.controller.js
new Date() without now() in ArticleService.js?
how is updating an article possible without the _id?`

and what about the views-part?

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

Good news for tech devs!

I built and deployed a CLI to help you architect a Nodejs application using the MVC design pattern & the Layered Architecture. Contributions are welcomed #cli #npm #npmpackage
install using: npm i -g @eetukudo/node-mvc
npmjs.com/package/@eetukudo/node-mvc

Collapse
 
digitalprimeint profile image
Marco Castillo

Emmanuel this is a great post!

I would like to get your honest feedback of my MVC framework. I have several years (6) applying the MVC concept on top of the express module. This is the latest version I made with ES6 best practices.

Dev.to Article
dev.to/digitalprimeint/mvc-framewo...

Documentation:
npmjs.com/package/plugdo-mvc

It could be 10 min of your time!

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

I will install the package and get back.

Collapse
 
727021 profile image
Andrew Schimelpfening

This is a great example of MVC with Node.
What’s the reasoning behind exporting a class with all static methods instead of just exporting the methods themselves?

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

It simply developer preference, The first thing that came to mind was the "Don't Repeat Yourself (DRY)" approach. Exporting each method can as well become verbose. Let me know if you can improve the code. Thanks.

Collapse
 
osairisali profile image
AAH

I think exporting a class with all its methods is quite bulky if we only need one or two of them. Security concern might also exposed. Anyway, it's a good article 👍 Thank you

Collapse
 
tonyscruze profile image
TonySCruze

Very nice article. I will restructure my current project to follow this "layered" structure. I'm trying to become more aware of separating concerns. Hopefully, I'll reap the benefits early on. Thank for the article 🙂

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

Thank you so much @tonyscruze . The first benefit is that you can write testable code on the fly.

Collapse
 
benrochlin profile image
ben-rochlin

This is by far the best overview of MVC architecture in express that I have come across. Seriously, like a year of trying to get my head around it and this overview and actually really concise and clever syntax (like use of static class methods) has really helped me figure this out! thank you so much 🤟🤟💕

Collapse
 
kiraniyerdev profile image
Kiran Iyer

I've just started exploring the MVC architecture, it seems a little overwhelming linking the different folder/file structures to each other. This is a great article to adding a bit more clarity to my confusion but again I've come across a whole new "services" folder and I've not used it in my CRUD todo application.

What I'm uncertain of is, where to begin when creating an app using the MVC concept.
I tend to get confused about which file is to be linked/imported where. Perhaps if you can give me a few pointers that'd be great or maybe point me to another article or perhaps a resource. Thanks

Collapse
 
quyle92 profile image
quyle92 • Edited

Emmanuel this is a great post!
But I would like to ask a question.
If we already had try catch block at Controller, then do we need to implement them again in Service layer? There is a similar case study on a youtube channel name "Laravel Daily" (link https://www.youtube.com/watch?v=0hyRw7zeVcs&t=182s) and at 4:07 the video author (Povilas) said "A Service should just throw an exception". So is it not a good practice to catch exception anywhere else beside Controller?
What do you think about it?
Thank you,
Quy

Collapse
 
thxv3n0lvl profile image
thxv3n0lvl

A reference on how to configure Mongo locally or on cloud will be great, this made me stop reading it as tutorial and switch to a "he's just explaining a pattern" mindset. Good enough though, thanks!

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

Thank you @thxv3n0lvl for the feedback, I will write an article on how to create a MongoDB database on MongoDB-Atlas and managing MongoDB databases with MongoDB compass.

Collapse
 
phongduong profile image
Phong Duong

Great tutorial. Thank you for sharing

Collapse
 
xementor profile image
xementor

It it good writting,
when i am reading i thinked ArticleService.js file is the Controler part of MVC.
but it was not. then ArticaleSErvice.js is in which part of MVC?

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

No, The ArticleService.js is in a separate folder name services, it should be within the root directory of the project. This is because we are using Layered Architecture, there is a need to separate the business logic from the controller.

Collapse
 
esilyzhang profile image
esily

Thank you for sharing, it is very helpful for me to learn MVC.
Can I translate your article into Chinese and allow it to be posted on the HelloGithub(hellogithub.com/) community platform?

Collapse
 
eetukudo_ profile image
Emmanuel Etukudo

Yes, you can. Let me know how it comes out. Best.

Collapse
 
raphydev profile image
Raphael Blehoue

it good writting

Collapse
 
lbian_prasoon profile image
Prasoon Shukla

Where to place middlewares in this project structure?

Collapse
 
ajyku profile image
AjayK • Edited

article.routes.js should be the place for the middleware like

var auth = require('../middleware/auth');
router.put("/article/:id", auth.isAuthenticated(), ArticleCtrl.apiUpdateArticle);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
prasadg profile image
Prasad

Nice article.
Why did you call folder as Services where you have written all database related functions?

Collapse
 
faiqfarzan profile image
Muhammad Farzan Faiq

Ever great article i found in internet for mvc structure in node express. Keep it up bro.

Collapse
 
ajyku profile image
AjayK

Excellent and very helpful. Thanks

Collapse
 
faiqfarzan profile image
Muhammad Farzan Faiq • Edited

Can you please tell me why you are using asyn wait ? because in my point of view code will be execute as synchrounously...!

Collapse
 
ajayvamsi profile image
Ajay Vamsi DNV

Hi,

I have a question, why declare all methods as static, instead of creating a instance of class to access them?

Any specific reason to this?

Thanks,
Ajay Vamsi